I'm having problems getting my communication code to work. I enter the data 1, 0, 255, 50 and then it errors (shouldn't it wait until 8 bits have been sent?) it exits with my debug message of BadPacket and StartPacket, and then prints the number 49, indicating that it thought the first packet was 49. What's going on here?
This is what I am using:
const uint8_t kACKBit = 6;
const uint8_t kNACKBit = 25;
const uint8_t kStartBit = 1;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if(Serial.available() >= 8)
{
readData();
}
}
void badPacket()
{
//flush the buffer and send a request to resend the data
Serial.flush();
Serial.println("Bad Packet");
Serial.print(kNACKBit);
}
void goodPacket()
{
//Packet good - send an acknowledgement
Serial.println("Good Packet");
Serial.print(kACKBit);
}
void readData()
{
uint8_t startPacket = 10;
startPacket = Serial.read();
if (startPacket != kStartBit)
{
badPacket();
Serial.println("StartPacket");
Serial.println(startPacket, DEC);
return;
}
//check that the address is valid
uint8_t address = Serial.read();
if(address >= 6)
{
badPacket();
Serial.println("address");
return;
}
//read in the RGB values
uint8_t r = Serial.read();
uint8_t g = Serial.read();
uint8_t b = Serial.read();
//read in the duration
uint8_t high_byte = Serial.read();
uint8_t low_byte = Serial.read();
//combine the two values into a single int
uint16_t duration = (high_byte << 8) + low_byte;
//check that it ends in a null teminator
if(Serial.read() != 0)
{
badPacket();
Serial.println("nullterm");
return;
}
//confirmed packet - send ack signal
goodPacket();
return;
}
-
\$\begingroup\$ are you trying to read individual bits there? it's not unheard of, but it would seem like with the arduino, there'd already be a library that would let you work in bytes. \$\endgroup\$JustJeff– JustJeff2011年06月14日 11:13:32 +00:00Commented Jun 14, 2011 at 11:13
-
\$\begingroup\$ Yeah, that is where your confusion is coming from. Serial.available() and Serial.read() are done in whole bytes, not individual bits. That said, I would try Serial.flush() 'ing in your setup fxn because as you stated, if you are in fact only sending 4 bytes {1,0,255,50}, then you should still be in the loop waiting for Serial.available() to reach 8. So it is my belief that you are not sending your data correctly... or at least not the way you think you are. \$\endgroup\$NickHalden– NickHalden2011年06月14日 12:28:44 +00:00Commented Jun 14, 2011 at 12:28
-
\$\begingroup\$ Sorry I meant bytes. A was a bit tired, it was midnight I was trying to get it working before I went to bed. Anyways, when I says Badpacket it also does a Serial.flush and then it will error again after another 4 bytes, not 8. \$\endgroup\$charliehorse55– charliehorse552011年06月14日 14:14:34 +00:00Commented Jun 14, 2011 at 14:14
-
\$\begingroup\$ Just figured it out - although I'm still not sure how the serial protocol works. I was sending the ASCII character that corresponds to the number 1 instead of the plain number 1. I don't know why but I guess when you send ASCII text it takes 2 bytes instead of one? \$\endgroup\$charliehorse55– charliehorse552011年06月14日 14:47:56 +00:00Commented Jun 14, 2011 at 14:47
-
\$\begingroup\$ Ah - when I sent the multi-digit numbers I was sending multiple bytes that explains it. \$\endgroup\$charliehorse55– charliehorse552011年06月14日 15:01:14 +00:00Commented Jun 14, 2011 at 15:01
1 Answer 1
It looks like you're confusing the ASCII values of the numbers with the numbers themselves.
If you send a '1' character over the serial port, you're actually sending the byte 49 (0x31). This is why you're seeing the invalid 49.
Either change your code to understand ASCII, or send the raw bytes instead of ASCII digits.
Explore related questions
See similar questions with these tags.