Hey everyone,
at the moment im using a Teensy LC Microcontroller to receive data from a jetson Xavier.
The Teensy is supposed to just receive the data and take actions according to that.
At the moment im just using serial.read() to get the data, but this sometimes stops working and also requires the data to be sent fast.
So i added a if(serial.available()) before the serial.read().
And this is where my confusion starts. When the if(serial.available()) my process for recognizing the data just stops working, wich seems odd.
So my question would be what exactly happens when i try to serial:read() without serial data present. And why dies the check for data available have an influence.
I sadly cant add the whole programm as an attachment. But here are the important lines. If i comment in the two lines in the loop i stops working. That meaning it still runs but the data is being read in a way that doesnt fit my structure anymore.
I would appreciate it if anyone has an idea of why this code behaves differently if check if there is data available. Also i would like to know if the serial timeout value has any impact in this program, or if it only used with other functions like serial.readBytes().
You may practice/study the following simple sketch along with Fig-1 to understand the relation between available() and read() methods. The sketch receives data (in the form of ASCII Codes) from the InputBox of the Serial Monitor and then sends it back to the OutputBox of the Serial Monitor.
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available(); //there is at least one char in Buffer
if ( n != 0) //control goes to below as long as 1 character is in Buffer (edit)
{
char y = Serial.read(); //brings-out data from FIFO type Serial Buffer (Fig-1)
Serial.println(y);
}
}
The return value of read is an integer but you might loose information as you read your Serial in a char inByte;
-1 in integer is 0xffff. So if you just use one byte and have any checks on 0xff this might be an issue.
Contrary, if you do an check with Serial.available, you will never get a -1 from Serial.read.
For me this explains possible different behaviours of your unseen code.
In generaI support the hint from @groundFungus - follow the tutorial "Serial Input Basic", try the examples and they will give you an introduction to a reliable Serial communication.
I looked at your code an the picture and one thing that stands out is the second comment, which suggests that there have to be 2 Bytes(chars) in the buffer for it to be read. Am i understanding this correct?
And it still doesnt work how i would i like to. But i figured stuff out. This code correctly differentiates between the two messages but the data in the message is wrong because of more more data byte beeing read than it should. I have confirmed that by setting the led pin to high once a normal data Byte is received and not a special marker and an oscilloscope to count the high peaks. I found that there is always one more data byte being read so 9 instead of 8 or 5 instead of 4. That explains why my old code wasnt working, cause i checked the message position of the endbyte as well.The question is now where the extra byte comes from and why it wasnt read without the if(serial.available()).
To send the data im using termios.h this is some of the send code.
can you post two examples from the serial output your device is really sending?
Displayed in HEX so wie can see really all bytes which get transmitted?
For example with "Coolterm" or another good serial program?
So i solved the problem. After the startmarker is read the program reads another byte. I dont know where it comes from, but just reading that byte into a dummy variable solves the problem and everything works as intended. This is not an optimal solution and theoretically i should find the cause for this. As soon as i have enough time im going to implement all the Teensy functionality on the xavier this this fix is not going to be permanent.