Serial Communication in Matlab V2 - Esposito PDF
Serial Communication in Matlab V2 - Esposito PDF
Serial Communication in Matlab V2 - Esposito PDF
Esposito 2009
Disclaimer: The tutorial is not a replacement for reading the Matlab Documentation on Serial Interfacing;
nor does it conver all the concepts and implementation details of serial communication and the RS-232
protocol. The examples are soley geared toward the types of applications we see in our projects such as
sending commands to control robots, motors, or reading sensors such as GPS, laser scanners, compasses,
etc. Other types of eqpitment may require different techniques not covered here.
Contact:
Associate Professor Joel M. Esposito
[email protected]
Systems Engineering Department
United States Naval Academy,
Annapolis, MD 21402
http://www.usna.edu/Users/weapsys/esposito/
Page 1 of 16
Matlab Serial Communication Tutorial
Esposito 2009
Hardware Devices
A lot of external hardware devices are designed to connect to a PC through a Serial Port. Examples in our
department include Koala Robots, iCreate Robots, Scorbot, Robix Kits; and lots of sensors such as the Sick
or Hokuyo Laser Scanners, the Northstar kits, GPS, Compasses, etc
Traditionally all PC's had a serial port in the back. However, now they are being replaced by USB ports.
"Serial ports" take many forms. For example the Xbee modems create a wireless serial link. Many laptops
don't have a serial port – just USB ports. USB to Serial Converters or Bluetooth Connections can function
as "virtual serial ports", meaning that once set up correctly Matlab just veiws them as additional serrial
ports.
Basic Concepts
• Cabling:
• Serial Message: You litterally send or recive data over this cable on a single pin as a series of
bytes (1 byte = 8 bits or 0 – 255).
o Example: [0] [12] [27] [42] [112]
• Terminators: Just as we use a period in English to dentote the end of a sentence, we use a
"terminator" to indicate the end of a series of bytes that constitute a message.
o The terminator can be anything the sender and receiver agree on but a "carrage return"
(\r) is a common choice.
• Buffer: If you don't understand how a buffer works, you will never understand serial
communication. Say a sensor is streaming back data to your program, more frequently than your
program reads it. On your computer this data gets stored in something called a buffer, until you
decide to read it. Think of a buffer as a list.
o As new data values come in they get added to the bottom of the list (most recent data).
o If your program reads a value from the buffer, it starts at the top of the list (oldest data).
Once you read a byte of data, it is no longer in the buffer; and the data in the second
position on the list moves up to the top position, etc.
o The buffer has a finite length (you set it). This means there is a limit to how long the list
can get. Once the buffer is totally full, what happens when the sensor tries to send new
data to the buffer? The oldest data (top of the list) gets discarded forever, and all the
entries move up, to make room on the bottom of the list for new data.
o If you'rer smart about using the buffer, you can make sure you never miss any data. If
your not smart about it, it is easy to loose data or use old data.
The sensor writes another value (6) to it, Note that the oldest data is in the first position and new data fills
in the buffer from the bottom.
Page 2 of 16
Matlab Serial Communication Tutorial
Esposito 2009
1 10
2 6
3
4
5
1 6
2
3
4
5
x= 10. Note that once we read the value it is no longer in the buffer! Also note that we read the top
element which was the oldest data.
The oldest data, in the top entry, (10) is discarded forever and all the entries shift up 1 spot, to make room
for the new value (4).
• Message Length and Check Sum: It's very possible, especially when you use wireless serial
connections that individual bytes in the message can get lost or garbled. There are a lot of
complex schemes out there to check that this doesn't happen. Here are two common ones that
show up in some serial command interfaces.
o One of the bytes in the message might indicate the length of the message (the total
number of bytes the message should contain).
o Another byte might be a checksum. A checksum is a number computed using a simple
arithmetic formula that can help you determine if the message has been garbled. Here is
an example: CheckSum = 255 – Sum of all data bytes in message.
o On the receiving end you can check that the length and checksum for the message you
received match the actual message. If they don't, you can decide to discard the message
or request new data from the device.
o Example: [4] [253] [1] [1] where 4 is the length (includes length and checksum bytes),
[1] [1] are the data bytes (meaning depends on what the sensor does), and 253 = 255 –
(1+1) is the checksum (does not include itself or length byte).
• Streaming vs. polling:
Page 3 of 16
Matlab Serial Communication Tutorial
Esposito 2009
o Polling a sensor is simple. You send it a message requesting some data each time you
want to take a measurement; then it returns a message containing that data.
o Streaming means that you send the sensor a message to turn it on. Then it begins
sending back measurements as it gets them – usually at a regular interval
Communication Settings
Port: COM1
Page 4 of 16
Matlab Serial Communication Tutorial
Esposito 2009
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Note that this list of parameters and attributes it returns is not exhaustive.
ans =
9600
ans =
19200
This method is cumbersome if you have a lot of things you want to change. A better way to to set them
when you create the Serial Object.
serialPort_new = serial('com1', 'baudrate', 19200, 'terminator', 'CR')
Communication Settings
Port: COM1
BaudRate: 19200
Terminator: 'CR'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
You can list as many properties as you want. The name of the property goes in single quotes (check
spelling) and the value follows (if the value is text then use single quotes)
Page 5 of 16
Matlab Serial Communication Tutorial
Esposito 2009
The Parameters
To see a list of parameters and their current values
get(serialPort)
ByteOrder = littleEndian
BytesAvailable = 0
BytesAvailableFcn =
BytesAvailableFcnCount = 48
BytesAvailableFcnMode = terminator
BytesToOutput = 0
ErrorFcn =
InputBufferSize = 512
Name = Serial-COM1
ObjectVisibility = on
OutputBufferSize = 512
OutputEmptyFcn =
RecordDetail = compact
RecordMode = overwrite
RecordName = record.txt
RecordStatus = off
Status = closed
Tag = GarminGPS
Timeout = 0
TimerFcn =
TimerPeriod = 1
TransferStatus = idle
Type = serial
UserData = []
ValuesReceived = 0
ValuesSent = 0
Note that some values are just numbers, while others can only take on certain values in a list ( ex. 'on' or
'off '). To see a list of all paremeters with valid choices type (note that the curly brace denotes the default
value)
set(serialPort)
Page 6 of 16
Matlab Serial Communication Tutorial
Esposito 2009
Suggestions on Parameters
Some of these you don't really need to change. Others you will want to change.
Always Set
You always have to set this to match what is specified in the documentation that came with your device.
• BaudRate
Always Check
The defaults here are usually OK, but you should check that they match whatever is specified in the device
documentation.
• Terminator (sometimes have to change) 'LF' is linefeed, 'CR' is carrage return, etc
• FlowControl (defaults usually OK)
• Parity (defaults usually OK)
• DataBits (defaults usually OK)
• ByteOrder (more on this later)
Page 7 of 16
Matlab Serial Communication Tutorial
Esposito 2009
• Tag: The tag is like giving the serial port object a nickname. If have a few different serial ports
open this a good way to keep track of them. Example, serialPort is configured to talk with a
garmin GPS.
set(serialPort, 'tag', 'GarminGPS')
• TimeOut: If you try to read data from the serial port and there is no data in the buffer matlab will
keep trying to read for "Timeout" seconds (default 10 sec):
get(serialPort, 'Timeout')
ans =
10
This might really slow down your code. There are ways around this, but if there is no data there
you probably don't want to sit there for 10 seconds, so consider making it smaller. On the other
hand, it does take some time for messages to pass over the wire, so setting it to zero means you
will probably miss a lot of messages.
• InputBufferSize: This specifies how long the buffer is. The default is 512 bytes. That might not
be long enough for your messages. Especially if you think the sensor will be streaming data back
more frequently than you plan on reading the buffer. Remember if the sensor tries to send data
and the buffer is full it will discard some old data and it will be gone forever. On the otherhand,
having an unessecarliy large buffer can be cumbersome.
Example Code
For technical reasons you have to use this systax to properly get rid of it:
delete(serialPort_new)
clear serialPort_new
% creating a new serial object for my GPS (note I can do all this in one
line if I wanted to
serGPS = serial('COM1'); % Define serial port
set(serGPS, 'BaudRate', 4800); % instructions for GPS gave me this
set(serGPS, 'Tag', 'GPS'); % give it a name for my own reference
Page 8 of 16
Matlab Serial Communication Tutorial
Esposito 2009
Page 9 of 16
Matlab Serial Communication Tutorial
Esposito 2009
Now you need to figure out two things from the Serial Command Interface (SCI) that came with your
device:
1. Will you send binary data (bytes) or text (ascii)?
2. What will you send to it?
If your SCIs messages look like a list of numbers (ex: [4][253][1][1])), its probably the first choice. Note
that even though what you send is actually binary, the documentation might list it as numbers between 0
and 255, or hexidecimal numbers.
If your SCIs messages look like a mix of text and numbers (ex: 'MOVE 31'), its probably the second
choice.
Its important to understand that a number, (ex. 98) is not sent as a number. Its actually the asci code for
the characters '9' and '8'.
Example Code
N = serRoomba.BytesAvailable();
while(N~=0)
fread(serRoomba,N);
N = serRoomba.BytesAvailable();
end
Page 10 of 16
Matlab Serial Communication Tutorial
Esposito 2009
ans =
x = 'X'
xPosition = 2.1
y = 'Y'
yPosition = 3.2
t = 'T'
Heading = -0.5
Reading Data
You can use fread to read in data (not text). It can automatically format the data for you. Here is
an example. Say the buffer currently has 2 bytes of data in it
[1], [8]
a = fread(serialObj, 2);
% Will read two bytes and create a vector
a = [1; 8]
If you omit the ,2 the result would be the same -- it will just read until either a terminater is
reached or there is no more data in the buffer
Alternatively, suppose you know those two bytes are used to express a singel 16-bit integer, you
can use
a = fread(serialObj, 1, 'int16')
ans =
264
Page 11 of 16
Matlab Serial Communication Tutorial
Esposito 2009
port = strcat('COM',num2str(ComPortNumber) );
Page 12 of 16
Matlab Serial Communication Tutorial
Esposito 2009
%% IF THERE IS NO DATA?
if (get(serGPS, 'BytesAvailable')==0)
disp('Data not avail yet. Try again or check transmitter.')
return
end
%% IF THERE IS DATA
while (get(serGPS, 'BytesAvailable')~=0)
try
% read until terminator
sentence = fscanf(serGPS, '%s');
Ns = length(sentence);
[prefixRMC,timeRMC,ActiveRMC,lat,latdirRMC,lon,londirRMC,spdKnots,AngleD
eg] = strread(sentence, '%s%f%s%f%s%f%s%f%f', 1, 'delimiter', ',');
catch ERR_MSG
% if something didn't work correctly the error message displays
disp('Error Reading Data! Check Unit')
end
end
Page 13 of 16
Matlab Serial Communication Tutorial
Esposito 2009
% Esposito 9/2008
warning off
a = instrfind('port',comm);
if ~isempty(a)
disp('That com port is in use. Closing it.')
fclose(a)
delete(a)
end
Page 14 of 16
Matlab Serial Communication Tutorial
Esposito 2009
fwrite(serPort,Contrl);
pause(.1)
% light LEDS
fwrite(serPort,[139 25 0 128]);
% set song
fwrite(serPort, [140 1 1 48 20]);
pause(0.05)
% sing it
fwrite(serPort, [141 1])
confirmation = (fread(serPort,4))
pause(.1)
Page 15 of 16
Matlab Serial Communication Tutorial
Esposito 2009
BumpLeft = bin2dec(BmpWheDrps(end-1))
if BumpRight*BumpLeft==1 % center bump sensor is really just
% left AND right at same time
BumpRight =0;
BumpLeft = 0;
BumpFront =1;
else
BumpFront = 0;
end
Wall = fread(serPort, 1) %0 no wall, 1 wall
Page 16 of 16