Hi,
I am sending serial messages of different sizes from an arduino uno to a teensy 3.6 at 9600 Baud. The Teensy is then reading the first byte of the message, which is representing the data payload, and determins the entire length of the message (6 Bytes default + data payload) aka how many bytes to read from the buffer. After reading the message the teensy is supposed to ackowledge the message. This is where the issue occurred. If I send a message of 8 Bytes in total, the teensy answers in about 170us (picture 1) but if I send more or less data, it takes about 1.2ms to answer(picture 2). This delay is to much for my appication and I cant figure out what the cause coud be. Ive tried different approches of reading the data from the buffer but there has been no change in the timings. Also, both of these messages shown in the picture where send alternating and repeatedly during the same code runtime and the issue is always the same, even after a couple thousand messages. There is no issue with the code other than the timings. The teensy reads the send data correctly every time and anwers accordingly.
I am using the Serial1 channel on the teensy to receive and send the data an I have captured the timings with the az-delivery logic analyzer.
I have shortened the code down to the parts that cause the issue. Its written in VS Code with the PlattformIO extension.
First channel is the message beeing send by the arduino
Second channel is the acknowledgement by the teensy (followed by its own message)
Third is the LED_BUILDIN Pin of the teensy, toggled on when it reads the first byte of the message and toggled off after its finished reading.

picture 1, teensy receives 8 bytes

picture 2, teensy receives 6 bytes

teensy receives 7 bytes

teensy receives 9 bytes
I would appriciate any suggestions.
Thank you!
I am sending serial messages of different sizes from an arduino uno to a teensy 3.6 at 9600 Baud. The Teensy is then reading the first byte of the message, which is representing the data payload, and determins the entire length of the message (6 Bytes default + data payload) aka how many bytes to read from the buffer. After reading the message the teensy is supposed to ackowledge the message. This is where the issue occurred. If I send a message of 8 Bytes in total, the teensy answers in about 170us (picture 1) but if I send more or less data, it takes about 1.2ms to answer(picture 2). This delay is to much for my appication and I cant figure out what the cause coud be. Ive tried different approches of reading the data from the buffer but there has been no change in the timings. Also, both of these messages shown in the picture where send alternating and repeatedly during the same code runtime and the issue is always the same, even after a couple thousand messages. There is no issue with the code other than the timings. The teensy reads the send data correctly every time and anwers accordingly.
I am using the Serial1 channel on the teensy to receive and send the data an I have captured the timings with the az-delivery logic analyzer.
Code:
#define Frame_lenth = 6;
uint16_t bytesRead = 0;
#define BUFFER_SIZE 255
char buffer[BUFFER_SIZE];
void eventListener(void)
{
if (Serial1.available())
{
digitalWriteFast(LED_BUILTIN, HIGH);
buffer[bytesRead] = Serial1.read();
bytesRead++;
}
if (bytesRead >= (Frame_lenth + buffer[0])) // calc num of bytes to be received
{
/* do something with message */
adress = buffer[1];
Flags.frmCmp = 1;
bytesRead = 0;
digitalWriteFast(LED_BUILTIN, LOW);
}
char acknowledge;
void sendAckn(void)
{
if (!Flags.ackmnt) // only acknowledge once
{
acknowledge = (sDeviceAdress & 0xF0) + 0x0A; // OK
Serial1.write(acknowledge);
Serial1.flush(); // wait for transmission to be complete
Flags.frmCmp = 0;
Flags.ackmnt = 1;
resetTimer();
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial1.begin(BAUD);
Serial1.setTimeout(0.1);
M_state = _read;
}
void loop()
{
switch (M_state)
{
case (_read):
eventListener();
if (Flags.frmCmp)
{
M_state = _acknowledge;
}
break;
case (_acknowledge):
startTimer();
if ((adress & 0xF0) == (sDeviceAdress & 0xF0)) // if message was adressed to the teensy
{
sendAckn(); // send acknowledgement
}
break;
/*rest of the code causes no issue*/
}
I have shortened the code down to the parts that cause the issue. Its written in VS Code with the PlattformIO extension.
First channel is the message beeing send by the arduino
Second channel is the acknowledgement by the teensy (followed by its own message)
Third is the LED_BUILDIN Pin of the teensy, toggled on when it reads the first byte of the message and toggled off after its finished reading.

picture 1, teensy receives 8 bytes

picture 2, teensy receives 6 bytes

teensy receives 7 bytes

teensy receives 9 bytes
I would appriciate any suggestions.
Thank you!