I am trying to establish communication between my ATMega32 and Arduino Mega. My atmega is outputting characters of this format "aAaBaCaD" and repeats it at BAUD 300. What I want my arduino to do, is read the incoming data on Serial1, if it is "a" than read the next byte ie."A" and then only it is printed through Serial port to the computer.
And I am using this code to achieve that
But the out put "some 2 dots on y character" that is repeating on the serial monitor(gibberish).
..of reading a character that isn't there and trying to print 0xff.
Mismatched rates usually result in varied junk.
OP, are you certain that is your code?
I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.
yes I am aware. 0xaa is not ASCII 'a'. I tried all the possibilities of using 'a', 0xaa, other formats, on both the ends(atmega and arduino as sync bit).
I can't see how that code can ever work. As soon as a character is available, you check to see if it is the sync byte. If it is, you check immediately whether another character is available. It won't be, you are sending data at 300 baud so it will take around 33ms for the next character to arrive. You need to wait until each character is available. You'll also need some way of knowing when you have reached the end of the message, so you can go back to waiting for the sync byte.
I thought inserting
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.
cyberhawk9:
I thought inserting
if(Serial1.available()) before data=Serial1.read() would tell arduino to wait until the next byte is available.
You seem to be under the impression that Serial communication is fast; It's not. The second
if(Serial1.available())
will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.
Arrch:
You seem to be under the impression that Serial communication is fast; It's not. The second
if(Serial1.available())
will happen eons before the next serial byte is received. You either need to make it blocking, implement a state machine with some sort of end of packet marker and a buffer to store all of the data, or check for more than one byte is available the first time.
So how do I make it wait till the byte after the sync byte arrives?
if(Serial1.available())
{
sync=Serial1.read();
if(sync==0x02)
{
...
}
else
{
Serial.print("The byte I received was not a sync byte: ");
Serial.println(sync);
}
}
It's also worth noting that Serial.available() doesn't return a boolean, it returns a number of bytes in the buffer. The proper way to write it is:
It is very slow(not main problem as the sensor updates the data once every second, but still the data received is lot slower and jitter is also high ) and the error rate is too high. Is there any programming improvement to reduce the errors and jitter(speed is secondary)