Serial Communication Guide Arduino and MATLAB
Serial Communication Guide Arduino and MATLAB
MATLAB
ByAman Mangal
IIT Bombay
June 6, 2012
1/36
Prerequisite
http://arduino.cc/en/Reference/serial
http://www.ladyada.net/learn/arduino/lesson4.html
http://users.ece.gatech.edu/bonnie/book/TUTORIAL/
tut_1.html
http://www.mathworks.in/help/techdoc/matlab_
product_page.html
2/36
Serial Communication
3/36
Buffer
4/36
Buffer...in detail
5/36
Buffer...example
6/36
Buffer...example
10
6
7/36
Buffer...example
8/36
Buffer...example
6
4
5
9
3
What will happen if we write one more data value, say 12, to
the buffer?
9/36
Buffer...example
4
5
9
3
12
10/36
Before we continue...
3
4
11/36
Almost ready...
12/36
13/36
14/36
15/36
BaudRate
It is the rate of transfer of the data in bits/sec.
We can change the BaudRate using the set method as
follows>> set(s, BaudRate, 4800);
>> s.BaudRate = 4800 ;
You can also setup different BaudRate while making the serial
port object as follows>> s = serial(COM1,BaudRate, 4800);
You can verify the change using get method>> get(s, BaudRate)
ans = 4800
The following will also show the similar result>> s.BaudRate
ans = 4800
Aman Mangal, IIT Bombay
16/36
BaudRate
You can also do the following to verify the change>> s
Serial Port Object : Serial-COM1
Communication Settings
Port:
COM1
BaudRate: 4800
Terminator: LF
Communication State
Status:
closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Note the new value of BaudRate shown.
Aman Mangal, IIT Bombay
17/36
18/36
19/36
20/36
You can choose the kind of data you are expecting, otherwise
byte dataype can be used.
21/36
22/36
23/36
24/36
fread in MATLAB
You can also use fread instead of fscanf to read the data
from serial port.
fread reads until the Terminator is seen.
>> data = fread(s)
fread doesnt convert the data in the correct format until you
specify it.
It is better to specify size while using fread.
You also have to specify precision in fread if you want to
specify size.
>> data = fread(s, size, precision)
Try using fwrite & fscanf for writing and reading the data.
25/36
Format
26/36
Size
27/36
BufferSize
It is very important not to loose the data while the data gets
transferred.
As mentioned before, the data gets discarded if the Buffer is
full to accommodate new data.
To avoid loosing data, specify almost equal Baudrates in both
the sytem MATLAB & Arduino.
We have already seen how we can control the BaudRate.
We can specify the BufferSize in MATLAB according to the
need of program using following methodsset(s, BufferSize, 1024)
%in bytes
s.BufferSize = 1024;
%in bytes
(Similar to BaudRate)
28/36
Read/Write State
Each time you transfer the data, it is either receive or sent by
MALTAB.
The ValuesSent & ValuesReceived properties of serial port
object shows these values.
You can always check these values to ensure the correct
amount to data transferred.
You can also optimize your program to use the least possible
size datatype for the data to be transferred.
We have already seen the BytesAvailable property.
TransferStatus shows whether the data transfer is complete.
fprintf blocks the command line to execute other commands
while the data is getting transferred while fwrite doesnt.
29/36
30/36
To remember
31/36
To remember
32/36
INSTRFIND
The function instrfind finds all the possible, existing serial
port objects.
It returns an array of serial port object.
>> h = instrfind
Instrument Object Array
Index:
Type:
Status:
Name:
1
serial
closed
Serial-COM8
2
serial
closed
Serial-COM8
3
serial
closed
Serial-COM1
You can access any of the object using index like h(2)
>> s = h(2);
Now s is similar serial port object which we created in the
beginning of the tutorial.
33/36
INSTRFIND....advanced
You can specify the properties of the serial port object you
want to search for>> out1 = instrfind(Port,COM1);
>> out2 = instrfind(Port,BaudRate,COM2,4800);
Note that instrfind returns array/vector of serial port object.
It is used when you want to retrieve a deleted serial port
object.
34/36
More to come
35/36
Thank you
36/36