Tengo problemas para que mi código de comunicación funcione. Ingreso los datos 1, 0, 255, 50 y luego da error (¿no debería esperar hasta que se hayan enviado los 8 bits?), sale con mi mensaje de depuración de BadPacket y StartPacket, y luego imprime el número 49, indicando que Pensé que el primer paquete era 49. ¿Qué está pasando aquí?
Esto es lo que estoy usando:
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;
}
Parece que estás confundiendo los valores ASCII de los números con los propios números.
Si envía un carácter '1' a través del puerto serie, en realidad está enviando el byte 49 (0x31). Es por eso que está viendo el 49 inválido.
Cambie su código para que comprenda ASCII o envíe los bytes sin formato en lugar de los dígitos ASCII.
solojeff
NickHalden
charliehorse55
charliehorse55
charliehorse55