Simulink serial transmit is buffering before sending
15 views (last 30 days)
Show older comments
I have a simple simulink model intended to be used for hardware-in-the-loop with an external microcontroller via serial connection. I am using a windows machine and matlab 2023a.
My simulink model is comprised of a Host Serial Receive block and Host Serial Transmit block. It also contains the Host Serial Setup block. The Host Serial Receive block is set to blocking mode, so when starting the simulink model, it waits for a valid data packet on the serial port before advancing to the next timestep, and sends a 100 byte packet back to the microcontroller via the Host Serial Transmit block. In this way, my microcontroller controls the advancing of the model. This all works***
The model is intended to be run with 0.01s timesteps (100Hz). The microcontroller is sending messages to simulink model every 0.01s, so I would expect simulink to send back a message every 0.01s. What is instead happening is that I am seeing ~5 iterations on my microcontroller where the microcontroller's serial port times out several times and doesn't receive any data back from simulink, before seeing ~500 bytes available, or 5 packet's worth of data. Note that my microcontroller's read timeout is set so that simulink has ample time to send something back within a single loop iteration.
So what is happening is I am sending packets to simulink at 100Hz, simulink is receiving all of these packets and advancing at the correct pace I would expect, but I am not getting a packet back from my simulink model every iteration. Instead, sometimes I get none, sometimes I get up to 5 packets. So there is no data loss, but the data seems to be being buffered on the simulink side.
This behavior does not occur if I slow the whole system down to 10hz operation, for example. Every time the microcontroller sends a message, I get a message back from simulink for that loop iteration. There is no buffering of data before it finally comes through to my microcontroller.
I have tried with and without Blocking Mode enabled on the Host Serial Transmit block, which seems to have no effect. My understanding of blocking mode is that it blocks execution of the model (in this case, preventing the serial read block from waiting for the next data packet) until the 100 byte data packet has been sent out. In a way, this is a serial transmit "flush" functionality, but I am not getting it. I need a dedicated serial flush block, but there does not appear to be any implimented within simulink, just the matlab flush() method, which cannot be compiled with simulink.
What is causing this "buffering" behavior? HOW is this happening with the transmit blocking mode enabled? Shouldn't the model be blocked from advancing to the next iteration (where the first thing it does it wait for the serial receive block to get data to advance the sim one timestep) until ALL of the data has been sent by the serial transmit block?
2 Comments
Mark McBroom
on 14 Mar 2024
I would have expected blocking mode on the Transmit block to do the trick.
Answers (0)
See Also
Categories
Find more on Block and Blockset Authoring in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!