Simulink Real-Time data from Ethernet (S-Function implementation)

5 views (last 30 days)
Hi all,
I have the necessity to run a simulink "pseudo real-time" model, which takes as an input a data stream from an UDP socket, at an average of a datagram each 20-50ms. As the real time requirements are not extremely tight, I run the model at fixed step with Real Time Pacer, to have a true execution step of 5-10ms.
I have structured the model architecture like this:
1) The data reception is handled by a C# thread, run independently from the model. The thread is started in the model initialization phase. The thread is responsible for packet buffering and decoding and generates a .NET event every time a full message is received (we implement a custom communication protocol from our hw). The thread listens on the UDP socket.
2) The thread is registered, in the initializaion phase too, to a matlab function which acts as a callback on the .NET event. This function basically copies the message into a vector in the matlab workspace. At the end of the inialization phase, all the objects (thread, socket, etc.) are saved in a structure in the Matlab base workspace too.
3) In the Simulink model, the data input block is realized as an S-Function which grabs the data from the workspace and uses it to update its outputs.
4) In the model, there is a transmission block too. This block, on request, uses one of the objects saved in the workspace to send a TCP message to our device.
Basically everything works fine, but I am asking you if you are aware of a more efficient way of implementing this kind of model. In fact, I would like to push for a faster execution, and I feel that exchanging data through the Matlab workspace is not so efficient. I was thinking for example of changing the S-function with C-mex function, could this improve the situation? Another thing that I was asking myself without success is if there is any possibility to create a RAM buffer directly from Matlab, where the data could be stored and retrieved without passing from the workspace.
Keep in mind that the data reception is asynchronous to the execution of the model. Furthermore, a message could arrive in different datagrams, so the use of a dedicated C# thread that takes care of the decoding for me is essential.
I haven't been able to find any related topic (apart from this) that is close to my situation. But if you do, please point me!
Thank you very much for your (future) help :D,
Best regards, /Giulio

Answers (0)

Community Treasure Hunt

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

Start Hunting!