Serial communication using Stream Input/Output blocks receives delayed data
8 次查看(过去 30 天)
显示 更早的评论
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.
The problem:
There are two scenarios.
1) The first one uses the setup as described above and as shown below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1187008/image.png)
Some time after T = 5 s, I expect Serial Port [10] 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1187013/image.png)
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.
Question:
How can I get rid of the "delay" in scenario 1?
0 个评论
采纳的回答
Jan Houska
2022-12-2
Hi Ama,
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
0 个评论
更多回答(2 个)
Jan Houska
2022-11-10
Hi Ama,
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
2022-11-24
Hi Ama,
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!