Serial input from COM device via "Stream Input"-block in Simulink Desktop Real-Time External (SLDRT)

38 views (last 30 days)
Hi,
in short, I would like to imitate the functionality of the "Serial Receive" block (https://de.mathworks.com/help/instrument/serialreceive.html) with the "Stream Input" block (https://de.mathworks.com/help/sldrt/ref/streaminput.html) and don't know how to do it.
Explanation: As part of my project I am forced to run my code in the Simulink Desktop Real-Time External environment. But since the serial blocks are only available in normal mode but not external mode, I require to have some work around this, where the only alternative seems to be the Stream Input block.
Example:
In normal mode I am using the Serial Configuration and Serial Receive blocks as follows:
So one message contains of a starting byte "A" and an end byte "\n". In between there currently is one single value containing of 4 bytes. With this I can display the following function:
When I now try to imitate that behavior with the stream blocks in external mode I am trying to do this:
with the board settings:
With the same data stream as in the above example, the sinus now looks like this:
Does anyone have an idea of what I am doing wrong? One problem is, that Stream Input does not use a starting byte, but just end bytes (never the less I tried to enter 'A' also into the message terminator array).
Furthermore I would like to receive a message containing 3 values and I do not know how to do that either.
Thank you in advance for every hint.
Best regards,
Max
  2 Comments
Maximilian Becker
Maximilian Becker on 19 Aug 2021
Hey @Jan Houska, please excuse me pinging you, but I just remembered that you might have an idea on how to solve this problem, since you are versatile in terms of SLDRT. :)
Maximilian Becker
Maximilian Becker on 20 Aug 2021
I set the terminator byte to be '\n' and removed the header byte 'A' such that the message I send is just 5bytes long including the temrinator. All I can say so far about this set up is that the 'Data Ready' port is giving me the correct response in terms of message frequency while the actual message is still read as in the image above. The problem therefore lies somewhere in the way the message is extracted and formatted, but I am not good at this part. I will stick to the posted solution for the moment.

Sign in to comment.

Accepted Answer

Jan Houska
Jan Houska on 20 Aug 2021
Hi Max,
the Stream Input block is designed for receiving textual (ASCII) data of potentially variable length. Since your data is binary and of fixed length, Packet Input block is the right block for you. The reason why you are getting garbage instead of the correct numeric value is that the Stream Input block is trying to interpret the four binary bytes as ASCII text, which it isn't. Therefore this cannot work.
Good Luck, Jan
  1 Comment
Maximilian Becker
Maximilian Becker on 23 Aug 2021
Hello Jan,
I am very thankful for your knowledge again, since I did not know that there was something else except of binary data. This explains a lot.

Sign in to comment.

More Answers (1)

Maximilian Becker
Maximilian Becker on 20 Aug 2021
Edited: Maximilian Becker on 20 Aug 2021
I still was not able to bring the "Stream Input" block to work. After reading up some information and trying to reproduce the official support thread https://de.mathworks.com/matlabcentral/answers/470722-how-to-use-stream-input-block-for-serial-communication-using-simulink-desktop-real-time I changed the terminator of my message from {'A','\n'} to 'n' which ended up in not receiving anything at all. This continued for almost everything I tried though.
So I came up with a solution: I was streaming a sinusodial function from my Arduino Due to the serial port COM3 at my computer. The message was 6 bytes long, where the first byte is the header, the last byte is the terminator and the 4 bytes in between are a 4byte float value that I have to reconstruct.
Instead of using the Stream Input block to get me all the information at once, I used a formation of enabled subsystems and Packet input blocks, where I am scanning for the header byte to reconstruct the rest of the message.
For this I used the information and given examples from https://github.com/leomariga/Simulink-Arduino-Serial here. Please find his example and my Simulink implementation attached to this answer. I added some noted that should make clear what I have done there and why.
  1 Comment
Maximilian Becker
Maximilian Becker on 20 Aug 2021
I am still interested in finding a solution for the Stream Input block since its original idea seems to be more elegant than my approach. If anyone has a solution for this, please let me know.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!