Simulink/Arduino: serial receive byte stream

Hello,
I am using Simulink to receive serial data via Arduino, which is receiving three bytes (unit8) of data from a 2nd microcontroller. The 3-byte packet includes a startChar and 2 bytes of data, illustrated below.
Occasionally, if I reset the transmitting microcontroller, I notice the receiving bytes get jumbled and the starChar (36) is positioned either in the 2nd or 3rd index of the byte array. As a result this will completely ruin the rest of my model. To fix this I need to keep resetting the tranmistting controller.
What can I do to prevent this from happening?

5 Comments

is the next group of bytes sent correctly or have you lost synchronization?
I think you may be on to something. It may be a problem with the transmitting bytes.
The transmitting microcontroller is another Arduino, which is transmitting the three bytes every 20 Hz in a timer, shown below.
void myHandler() {
myBytes[0] = myInt >> 8;
myBytes[1] = myInt;
Serial1.write('$');
Serial1.write(myBytes[0]);
Serial1.write(myBytes[1]);
}
However, if I reset the controller at the right time, we may be a jumble on the transmitting bytes. Shown below are the bytes sent to the Simulink Arduino. If I reset it at just the right time, this happens:
$⸮
$⸮
$⸮
$⸮
$⸮$⸮
$⸮
$⸮
$⸮
$⸮
$⸮
I need to add some delay or trigger between Arduinos to prevent this.
ir looks to me as if $ is not necessarily invalid as a packet? That in theory you could get even $$$ as a packet?
Is your transmitting microcontroller also a arduino? What is the method of resetting the transmitting microcontroller?
Amrtanshu, yes the transmitting microcontroller is an Arduino.

Sign in to comment.

Answers (1)

Hi,
As far as I understand your problem, you are randomly resetting the transmitting microcontroller which is causing the input data stream to be jumbled up.
A possible work around to this problem can be achieved by adding the subsystem in the attached file between the serial port and the display.
Hope this helps!

Categories

Products

Release

R2020a

Asked:

on 21 Aug 2020

Answered:

on 31 Aug 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!