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.
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
In MATLAB command window, to access MATLAB help. Thank you very much for reading.