File Exchange

image thumbnail

RS232 Blockset

version 1.0 (117 KB) by

This blockset allows Simulink to communicate with remote devices using RS232 interface.

23 Ratings



No License

RS232 Blockset is a block library that allows to a simulation model to communicate directly and in real time (using, for example, the RT Blockset also published in MATLAB Central) with a remote device.

Actually, it's possible to use a simple communication protocol based on formatted string (in which simulation data is used to compose the message) using a ANSI C printf-like formatting standard.

12 blocks are actually available:

- RS232_Setup: Initialize and close the serial communication. The settings of the serial communication are selectable using the block panel
Unbuffered Formatted Communication:
- RS232_Read_Format: Reads from the serial port a formatted string (function analogue to the standard-C sscanf function).
- RS232_Write_Format: writes to the serial port a formatted string (function analogue to the standard-C sprintf function).
- RS232_Write_String: writes to the serial port the string indicated as block parameter.
- RS232_Read_Fix_Format: Read fix formatted string from a buffer. This block has been generated specifically to read Flight Simulator's formatted data string.
Buffered Formatted Communication:
- RS232_Create_Buffer: Initialize and allocate an input communication buffer.
- RS232_Read_Buffer: Reads bytes from the serial port, add them to the input buffer and looks into the buffer for a formatted string (function analogue to the standard-C sscanf function), leaving into the buffer all the bytes that doesn't correspond to the searched string (except the ones that are before the found string).
- RS232_Wait_Buffer_Synch: Looks for messages headers into the buffer and returns the "code" of first message in the buffer. Read data from the serial port and add it to the buffer, but doesn't remove any byte from it.
Bynary Formatted Communication:
- RS232_Write_Binary: Write a binary formatted data to the serial output.
Binary Buffered Formatted Communication:
- RS232_Read_Binary_Buffer: This block reads bytes from the serial port, add them to the input buffer and gets, starting at the start of the buffer, formatted data (the format is declared in the format string argument), leaving into the buffer all the bytes that exceeds the read operation.
- RS232_Peek_Binary_Buffer: This block peeks bytes from the serial port, add them to the input buffer and gets, starting at the start of the buffer, formatted data (the format is declared in the format string argument), leaving unmodified the buffer (no bytes are removed from the buffer, eventually some data are added in the case that new data is in the input port).
The interface of this function is the same respect to the RS232_Read_Binary_Buffer block, except that after the read is complete, the read portion of data is NOT removed from the buffer.
- RS232_Synch_Binary_Buffer: Looks for data header into the buffer and returns "1" at the output flag when the header is found.

The library blocks allow to write a simple string, to read and write a formatted string, to buffer strings and read multiple string types, to read and write a fix formatted string and to read and write a binary formatted strings. Some more blockset will be available in the new version, specialized for read and write to the RS232 port using the NMEA protocol or the TAIP (Trimble ASCII) protocol.
The RTW and xPC release of this blockset will be also available in the next future.

Comments and Ratings (38)


Anisha (view profile)

Hi, I'm using the serial port to get data into xPC but I don't think I'm using the right RS232 blocks. How can I get the 18 char hexadecimal string into xPC target?

It works fine in Matlab, and i am using version 2009b.


On R2007b an attempt to include the RS 232 Synch Binary Buffer
generates following error message:

"jar:file:/C:/Matlab2007b/java/jar/toolbox/dastudio.jar!/com/mathworks/toolbox/dastudio/diagView/resources/nag_red.gif Model error Unknown Simulink Block 'built-in/readBinBuffer2' already has a built-in parameter 'mask'. Hence a mask parameter of the same name cannot be added."

I am sure it worked fine with R13.
Any hints how to get it running?


Tanmay (view profile)

Sir could you please tell me where exactly should I save this toolbox so that becomes funtional?

I am using srial port to communicate matlab and a robot so that I need to send following command to robot see the output from the robot where all are in Hexadecimal format:
Could you please help if I can do it through serial port with matlab. Already I have done those with hyper terminal but I need to connect with matlab. I would appreciate if you could answer me. Meanwhile, I use matlab 2010a which Is not compatible with your block, I guess. I would also wondering if you could answer to my questions kindly?


Julius (view profile)

Hello Leonard, I am working on a project to display ECG, SpO2 and CO2 waveforms on the PC using Matlab. I am using RS232 to interface with my PC. Do you have any advice for me on how do i set up a GUI that i able to read the data from RS232 and then display the waveforms on Matlab? thanks for the help.

Carlos Gomez

Hello Leonardo.
I have some problems to open the port, could you send me, some examples or how I can install the blockset and the .dll in matlab.

Hadi Aliakbarian

Yaap. That is what I need too, but how to use it? please provide us a readme.

Carolina Avendaño

Hi, Leonardo, I'm trying to use this blockset, but I've some problems to do it. how can I install or upload this blockset in Simulink... Can you upload more information? please... Thanks...

Daniel Martin

Great help! may I ask you a question? how are the block coded? are they coded in C and then introduced in Simulink? or are they coded in a Simulink Embedded function?

I need to code a complex serial interface in Simulink Embedded functions and I don´t know how to share the serial port object between several blocks. You use a handler in/out. How did you do it (in case you did it in an Embedded functions)?

many many thanks


that is what i need. But how can i install that? There is no readme file. Could you manage it?

john mark

this is the way we con do it.

José Adalberto

Hi, excelent tool, but i need to generate code with Real Time Workshop, is this possible? even with the Matlab 6.5 version?
thank you

Matt w

Hi, i need to use com port 5. is there anyway i can access this port using this block set? thanks in advance.

monika m

hello & thanks 4 this stuff.i have a problem with running this blockset.u know i try it az i send something via com1 & reciving that through com2.but when i want to run its realtime workshop i have an error which says"error using rtw_c"& also "error oqured in sbuild".woud u pleaz help me to solve my simulink problems. thanks 4 your help.

Samira S

I could not send and recieve data via this block set although it has no errors.Is there any comments in order to run that?

Ilija Batas

asher levi

Steven Kolak

Simulink now supports serial RS-232 communication with Simulink blocks provided in the Instrument Control Toolbox.

These client blocks enable Simulink models to communicate with remote applications over the serial interface.

Visit The MathWorks serial web page for more information:

mahmood ababsi

I have try your box. but have got some problem.
I want to receive a data from serial port and want to display it on the GUI.

John Smith

Bas d'Herripon

Works fine, also in real time.
Some detailed examples with format and output would be nice.

piter pam

The RTW and xPC release of this blockset will be also available in the next future

... I am interested by this ...



Hemendra Pandey


Delporte Jean-François

I need to send the string "S76Q00P" on my serial port but When I write this string in the RS232 Write String block I have only in output 'S7'. I think that the problem is that the time between the string 'Q' and '0' is not enough long. How resolve this probleme?

Duong Pham

I can't use it with realtime workshop.
Can any one use it with real time workshop

Minh Tan Duong

Can you show me how does it work? Can you send some examples.

Giuseppe Leonardo Cascella

great work & super documentation,
well done!!!

lin adlina

hi Leonardo. I want your help.I want to know how to connect the dc motor speed controller circuit to the interface circuit using rs232.I hope you can help me.

chan dran

hi leonard,
i want one help, i dont know how to configure with simulink. plz, guide mw how to configure the dll files with simulink

Ehsan Tara

Rajput Ajay

Hi,Leonardo. Thanks for giving such help in matlab but still i am having some problems to receive data in matlab of version 6p5 from my microcontroller and also visual basic.So i would be thankful to u if u provide some guidance or tips regarding this problem.

Rajput Ajay


That is great.Leonardo,would you mind sending me some documents about preparing RS232 GUI in matlab.I want to prepare an interface using matlab.I've an MCU(microcontroller unit) and it has serial port.How can be sent datas to my pc using matlab.

Hector Fregoso

This software is great. The next is directed to the author. Leonardo I am really interested in how to program this type of software in MATLAB. Would you mind sending me a flow diagram of your software?

priya santhanam

Fred Bergen

Excellent! Good Job! Examples are very helpful!

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today