Serial communication using Stream Input/Output blocks receives delayed data
6 views (last 30 days)
As part of a project I am using Simulink to send and receive serial data. The shown setup only concerns the necessary parts for this question. I send the (string) output of a ramp signal with slope 1 every 5 seconds to COM9. This data is instantly forwarded to COM10. I am using the Virtual Serial Port Driver by Electronic Team to emulate the serial ports and forward the received data. Simulink then reads out the data at COM10 with a sample time = 0.05. I use Simulation Pacing in Simulink to ensure simulation time corresponds to wall clock time. I have attached my Simulink script.
There are two scenarios.
1) The first one uses the setup as described above and as shown below.
Some time after T = 5 s, I expect Serial Port  to read "5". Instead, it reads "0", and will continue to lag behind 5 seconds, i.e., at T = 10 s, it will read "5", etc.
2) In the second setup, I do not read COM10 in Simulink, but use PuTTY to open a terminal at baud = 115200.
What happens is that right after pressing Run, COM10 receives a "0", and it will correctly receive "5" at T = 5 s, "10" at T = 10 s, etc. This is the desired behaviour for the simulation in scenario 1.
How can I get rid of the "delay" in scenario 1?
Jan Houska on 2 Dec 2022
the Stream Output block, when run in Connected IO mode like you are running it, has one sample period delay between its input signal and the value sent to its output device. If you don't want this behavior, you can use e.g. a triggered subsystem and inherited sample time for the Stream Output block, as shown in the attached example.
Good Luck, Jan
More Answers (2)
Jan Houska on 10 Nov 2022
you should not use Simulink Pacing together with Simulink Desktop Real-Time. Simulink Desktop Real-Time I/O blocks synchronize to real time by themselves. Please turn off Simulink Pacing because it may interfere with the synchronization performed by the Stream Output block. Please get back here if this does not help.
And, another unrelated tip - you don't need to convert your data to string before sending it via Stream Output block. Instead, to send a number, you can use the %d format specifier in the Stream Output block that converts the number to a string internally, which is more efficient.
Good Luck, Jan
Jan Houska on 24 Nov 2022
in your model, please select DEBUG, Information Overlays, Execution Order. This will tell you in which order your blocks execute. It is quite probable that the input block executes before the output block, because there is no logical connection between them.
Please change the block priorities so that the execution order reverses - right-click on the block, go to Properties, Priority, and set priority for both the blocks in such a way that Output has lower value (say 10) and Input has higher value (say 20) here. Lower number means higher priority, so this should do the trick. Please get back here if this does not help, I'll try to watch the thread more carefully this time ;-).
Good Luck, Jan