Thilina's Blog

I might be wrong, but…

Serial PORT MATLAB – Getting Started

Serial port is a physical communication interface which transfer one bit at a time in contrast to parallel port. In the recent past, before USB serial port communication is the most famous communication medium for many computer peripherals such as mouse, keyboard, Etc. Still in many embedded development environments use serial port communication as data transmission interface.

When it comes to my field of interest, where developing embedded systems, mostly signal processing applications, I wanted to visualize and to do some tweaks to processed data to verify whether my application works properly or not. In this case normal serial port desktop applications does not allowed me to visualize what I needed except the application ‘Serial Chart’. Therefore as usual I ended up with a MATLAB solution that is serial port communication with MATLAB. In this article I will share my experience in sending data and receiving data with interrupts in serial port with MATLAB.

First step is to initiate the serial port object. This can be done using following command.

s = serial('COM17');

Note that ‘COM17’ is the port which you connect your serial port to your computer. Next step is to configure the serial port to its expected configuration. Following codes describe the basic setup of configuring the serial port.

s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = @intcon1;
s.OutputEmptyFcn = @instrcallback;
s.BaudRate = 115200;

When the BytesAvailableFcnMode is set to the mode ‘terminator’. It will wait for the termination character to trigger the Bytes Available Event. Otherwise we can set it to the mode ‘byte‘ and and specify the number of bytes which required to receive before triggering the event. This can be set by using the parameter ‘BytesAvailableFcnCount‘.

Configure ‘OutputEmptyFcn‘ to execute a callback function when an output-empty event occurs. An output-empty event is generated when the last byte is sent from the output buffer to the device. The function ‘BytesAvailableFcn‘ is the function which triggers in bytes available. This will be described later in this article.

After configuring the port properties you can open the port for data transmission.

fopen(s);

Writing to the serial port is easy. You can write a number or String to serial port as follows. Note that the char(10) is appended to the string to notify the termination character.

fprintf(s, [num2str(123) char(10)] );
fprintf(s,[ 'ENTC' char(10)] );

After all the operations you need to close the port, Remove the instance and clear it from the memory. You can use following code segment to achieve this step.

fclose(s);
delete(s);
clear s;

Now let’s see how to receive data from serial port in interrupt mode. As you seen in early we have set the Bytes Available function to a function called ‘intcon1’. Now we are going to implement this function. You can assign any name to this function, but make sure the arguments of the function are in following order.

function out = intcon1(val1, obj, eventStruct, val2)

Now we can read the serial port as follows. But first we need to read the get the serial port object. Since we are now working inside a function, our workspace is not the base workspace anymore. Now it is a different one and the base workspace variables are not accessible. So we need evaluate the serial object from base workspace.

s = evalin('base', 's'); %evalin(‘workspace name’, ‘variable name’)

Now we can read the values from serial object.

[A, count] = fscanf(s);

The variable A contains ASCII values of the received bytes and count contains the number of bytes received. First we can convert this into a string so that we can further process this as a number or a string as it is.

str = [];
for i=1:(count-1)
str = [str cast(A(i),'char')];
end

However if you need to (say) update a variable in workspace, you can use following code segment to update it.

assignin('base','rxd',12); %assignin(‘workspace name’, ‘variable’, value)

And that concludes the article on ‘getting started with MATLAB serial port’. You can get more information about this topic by typing

>>doc serial

In MATLAB command window, to access MATLAB help. Thank you very much for reading.

2013 August 25 - Posted by | Biomedical, Electronics, MATLAB | , , , , ,

4 Comments »

  1. Hi, I’m trying to do something similar, interfacing with an RS232 port using matlab 2013, and can’t get the port to do anything more than echo what I put into it.

    Any chance I can ask you a few questions? Cheers

    Comment by Stu | 2013 November 20 | Reply

  2. Impressive, thanks

    Comment by ZJ | 2013 December 22 | Reply

  3. try with this:

    clc
    clear all
    cont=0;
    %fclose(instrfind)

    s=serial(‘COM4′);
    set(s,’BaudRate’,19200,’Databits’,8,’Parity’,’none’,’StopBits’, 1,’FlowControl’, ‘none’,’Terminator’,’CR/LF’);
    set(s, ‘timeout’, 1);

    % while(cont<100000)
    fopen(s);
    datos=[01 03 00 08 00 07 133 202]; %%% here you need the comand to the slave!!! (Deciamal)

    fwrite(s,datos);
    out=fread(s)
    fclose(s)

    Comment by ARTHUR | 2014 March 13 | Reply

  4. hello……
    I want to continuously monitor serial COM port….
    After receiving data on COM port from interfaced device then only I want to do further task…..as like interrupts in 8051.How to do the same.

    Comment by Avadhoot | 2014 November 27 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: