Hi all.
I work on a project which requires UART communication between a Teensy 4.1 and an ESP32-S3. To be flexible and have some kind of delimiters, i choose 170 as START_BYTE and 255 (must be two times in a row) as END_BYTE. In between those delimiters i send the message in the following format: "Code, Value_byte1, Value_byte2". Code is used, to tell the receiving device, to which variable the value should be assigned. The communication should later be happening both ways, but right know, im stuck with the read function on the teensy. Maybe, someone has an idea:
The Teensy code is as follows. Each byte is stored in a vector, to later handle the values and put together the individual bytes to an int16_t. Sadly, in between the delimiters, i dont receive the correct values. The ESP sends the correct data (at least according to the messages printet in the serial monitor).
If someone has an idea why there is this difference, i would very much appreciate the help, i've been trying to find a solution since hours.
Thank you very much
I work on a project which requires UART communication between a Teensy 4.1 and an ESP32-S3. To be flexible and have some kind of delimiters, i choose 170 as START_BYTE and 255 (must be two times in a row) as END_BYTE. In between those delimiters i send the message in the following format: "Code, Value_byte1, Value_byte2". Code is used, to tell the receiving device, to which variable the value should be assigned. The communication should later be happening both ways, but right know, im stuck with the read function on the teensy. Maybe, someone has an idea:
The Teensy code is as follows. Each byte is stored in a vector, to later handle the values and put together the individual bytes to an int16_t. Sadly, in between the delimiters, i dont receive the correct values. The ESP sends the correct data (at least according to the messages printet in the serial monitor).
void ESPcom::refreshMsg()
{
//Check if something is there to read
if (Serial1.available() > 0)
{
while (true)
{
m_received_byte = Serial1.read();
Serial.printf("Read Byte %d: \n", m_received_byte);
/* Serial.printf("Received Byte: %d Value as Serial.write: ", m_received_byte);
Serial.write(m_received_byte);
Serial.printf("\n");*/
if (m_received_byte == START_BYTE && m_read == false)
{
m_read = true;
Serial.printf("Read START_BYTE: %d\n", m_received_byte);
}
//If the read Byte is not 0xFF
else if (m_read && m_received_byte != END_BYTE)
{
m_Messages_received.push_back(m_received_byte);
Serial.printf("Read msg: %d\n", m_received_byte);
}
else if (m_read && m_end_byte_1 == false && m_received_byte == END_BYTE)
{
m_end_byte_1 = true;
}
//Set m_end_byte_1 to false again because this was just a "full" byte. Push it back,
else if (m_read && m_end_byte_1 == true && m_received_byte != END_BYTE)
{
m_Messages_received.push_back(0xFF);
m_ammount_read_bytes++;
Serial.printf("Read msg (m_end_byte1): %d\n", 0xFF);
m_Messages_received.push_back(m_received_byte);
m_ammount_read_bytes++;
Serial.printf("Read msg (m_end_byte1 false): %d\n", m_received_byte);
m_end_byte_1 = false;
}
//If two END_BYTE in a row, set m_read to false
else if (m_read && m_end_byte_1 == true && m_received_byte == END_BYTE)
{
Serial.printf("Read 2x END_BYTE: %d\n", m_received_byte);
m_read = false;
m_end_byte_1 = false;
break;
}
else
{
break;
}
}
}
}
The picture shows, what is send to test the function. START_BYTE (170), code(19), value_byte1(0), value_byte2(100), code(24), value_byte1(1), value_byte2(244), END_BYTE, END_BYTE. Between the delimiters 19, 0, 100, 24 1, 244 is send. The int16_t values are in this case 100 and 500. But the teensy only receives 34, 100, 20 and 244 between the delimiters. Interestingly, the delimiters are read correct.void TeensyCom::write()
{
uart_write_bytes(UART_NUM_1, &m_start_byte, sizeof(m_start_byte));
printf("Message: %d\n", m_start_byte);
uint8_t msg = 0;
for(int i = 0; i < MsgToSend.size() ; i++)
{
msg = MsgToSend.at(i);
uart_write_bytes(UART_NUM_1, &msg, sizeof(msg));
printf("Message: %d\n", MsgToSend.at(i));
}
//Send two (!) END_BYTE so that the teensy can differentiate between normal msg and end byte
uart_write_bytes(UART_NUM_1, &m_end_byte, sizeof(m_end_byte));
printf("Message: %d\n", m_end_byte);
uart_write_bytes(UART_NUM_1, &m_end_byte, sizeof(m_end_byte));
printf("Message: %d\n", m_end_byte);
MsgToSend.clear();
}
If someone has an idea why there is this difference, i would very much appreciate the help, i've been trying to find a solution since hours.
Thank you very much