SlideShare a Scribd company logo
Serial Communication between Arduino and 
MATLAB 
By- 
Aman Mangal 
IIT Bombay 
June 6, 2012 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 1/36
Prerequisite 
Serial data transfer in Arduino. Follow the link to learn more- 
1 http://arduino.cc/en/Reference/serial 
2 http://www.ladyada.net/learn/arduino/lesson4.html 
MATLAB programming. To learn more, go to- 
1 http://users.ece.gatech.edu/bonnie/book/TUTORIAL/ 
tut_1.html 
2 http://www.mathworks.in/help/techdoc/matlab_ 
product_page.html 
You should be familiar with MATLAB structures, MATLAB 
objects etc. 
Basic programming concepts. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 2/36
Serial Communication 
Serial means "One after another". 
Serial communication is when we transfer data one bit at a 
time, one right after the other. 
Information is passed back & forth between the computer and 
Arduino by, essentially, setting a pin high or low. 
Just like we turn an LED on and o, we can also send data. 
One side sets the pin and the other reads it. 
MATLAB can read/send the data from/to the serial port of 
the computer and process it. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 3/36
Buer 
It is most important to understand the nature of buer to 
avoid errors later while writing codes. 
There exists a buer between the two events of sending and 
reading the data. 
Say a sensor is streaming back data to your program, more 
frequently than your program reads it. 
Then the data is stored to a list which we call a buer. 
One writes data into it and other reads it, may be with 
dierent speeds. 
Buer are of
nite length. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 4/36
Buer...in detail 
Initially the buer is empty. 
As new data values come in they get added to the bottom of 
the list (most recent data). 
If your program reads a value from the buer, it starts at the 
top of the list (oldest data). 
Once you read a byte of data, it is no longer in the buer. 
The data in the second position on the list moves up to the 
top position 
As soon as the buer is full and more data is sent, the oldest 
data gets discarded to make space for new data. 
You have to be smart enough not to loose data. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 5/36
Buer...example 
We set up the Serial communication between arduino and PC 
with a buer length of 5. 
1 
2 
3 
4 
5 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 6/36
Buer...example 
The value 10 and 6 are transferred respectively. 
1 10 
2 6 
3 
4 
5 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 7/36
Buer...example 
One value is read. The value 10 is the oldest. So it will be 
read
rst. 
1 6 
2 
3 
4 
5 
Note that 10 no longer exists in the buer. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 8/36
Buer...example 
Now, assume that the buer is full. 
1 6 
2 4 
3 5 
4 9 
5 3 
What will happen if we write one more data value, say 12, to 
the buer? 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 9/36
Buer...example 
The buer will look like as follows- 
1 4 
2 5 
3 9 
4 3 
5 12 
Note that the oldest data 6 is discarded. 
All the data is shifted up and new value is added as the last 
entry to the buer. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 10/36
Before we continue... 
We have to connect the Arduino board to the PC. 
Each serial port on the PC is labeled COM1, COM2 etc. 
The Arduino will be given a COM Port Number. You should
gure it out by following these steps- 
1 Right click on My Computer icon and select Manage. 
2 Select Device Manager in the tree view you will see on the 
left side in the new window opened. 
3 Find and select Ports(COM LPT) in the center panel. 
4 There, you will
nd lists of all the ports attached to your 
computer. 
5 Figure out the one you are concerned with. Refresh the 
window if you don't
nd any! 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 11/36
Almost ready... 
Now we are ready for the MATLAB and Arduino serial 
communication. 
We will only focus upon the MATLAB. We will study how can 
we set up serial port objects, send and read data from the 
buer in MATLAB. 
I assume the you already know how to send/read data in 
Arduino. 
Follow the link given in the beginning of the tutorial if you 
wish to learn Serial Communication in Arduino. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 12/36
Styles used throughout the tutorial 
All the MATLAB commands are written in italics and 
preceded by  
The MATLAB output is written in blue color everywhere. 
Use 
 help 'command' 
 doc 'help text' 
to get help in MATLAB for any command used in the tutorial. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 13/36
Setting up Serial Port Object 
We
rst need to create a serial port object. 
Serial port object is just a name given to that serial port so 
that we can use it in later commands. 
 s = serial ('COM1'); 
Serial Port Object : Serial-COM1 
Communication Settings 
Port: COM1 
BaudRate: 9600 
Terminator: 'LF' 
Communication State 
Status: closed 
RecordStatus: o 
Read/Write State 
TransferStatus: idle 
BytesAvailable: 0 
ValuesReceived: 0 
ValuesSent: 0 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 14/36
Setting up Serial Port Object 
This line of command only constructs the object. It does not 
check/setup/initialize the communication. 
This command will still work even if the serial port is not 
connected to any device. 
Many objects can be created for a serial port but only one can 
be connected at a time.(we will see later) 
This shows all the property of the constructed serial port 
object. 
In MATLAB, s is a structure which has all the above 
properties. We can access/modify them using dot(.) operator. 
Note that the Status is closed. It implies that the serial port 
is not connected. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 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 dierent 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 Serial Communication between Arduino and MATLAB 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: o 
Read/Write State 
TransferStatus: idle 
BytesAvailable: 0 
ValuesReceived: 0 
ValuesSent: 0 
Note the new value of BaudRate shown. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 17/36
Properties of Serial port object 
As we have already stated that s is of MATLAB datatype 
called structure(similar to structures in C++). 
There are lots of 
exibility MATLAB provides to change the 
properties of the serial port object. 
The commands are similar to what we have used for 
BaudRate. 
Use following command to list down all these properties and 
their current value- 
 get(s) 
To see the possible values of all these properties, use- 
 set(s) 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 18/36
Setup the connection 
Before you actually write the data to the serial port, you must 
connect to device. 
This is like a JAVA lock. Only one entity can acquire the lock 
at a time. 
Use fopen to acquire the lock and setup the connection. 
 fopen(s) 
Notice the status property of the serial port object- 
 s.Status 
ans = open 
If the lock is already acquired, fopen will give an error. 
To avoid error,
rst check the Status property of the serial 
port object. If it is closed then try to setup the connection. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 19/36
Writing to the Serial Port in MATLAB 
MATLAB can write any kind of data to the serial port binary, 
string, int, 
oat etc. with speci
ed precision. 
We use fwrite or fprintf to write data. 
Transfer an int/
oat array- 
 fwrite(s, vector array, 'precision'); 
The precision speci
es the datatype of the vector array. It can 
be 'int8', 'int16', '
oat32', '
oat64', 'uint8', 'char' etc. 
String- 
 fwrite(s, 'string'); 
You can use fprintf for strings as well- 
 fprintf(s, 'string'); 
You can specify the format in fprintf for a string. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 20/36
Reading from Serial Port in Arduino 
You can follow these steps to read data in Arduino- 
You can choose the kind of data you are expecting, otherwise 
byte dataype can be used. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 21/36
Writing to Serial Port in Arduino 
Use following function to write data in Arduino to the Serial 
Port. 
Note the use of println not print. 
This will be helpful when we will read the data in MATLAB. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 22/36
Reading from Serial Port in MATLAB 
fscanf is used to read the data- 
 data = fscanf(s); 
This will read all the data until it
nds a new line/Terminator. 
That is why we used println instead of print. 
By following this procedure you will be reading all the data 
sent in one command of Serial.println at a time. 
In this case, MATLAB automatically converts data into the 
correct format and stores into the MATLAB variable. 
If there is no data to read, a time out will occur. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 23/36
Reading from Serial Port in MATLAB 
To avoid time out, you can
rst check the BytesAvailable 
property of serial port object just like we did in Arduino- 
 if s.BytesAvailable  0 
data = fscanf(s); 
end 
You can specify the size of the data you want to read in case 
you use Serial.print. 
You have to specify the format of data together with the size 
of the data. 
 fscanf(s, 'format', size); 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 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 doesn't 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. 
Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 25/36

More Related Content

PDF
Matlab Serial Port
PDF
interfacing matlab with embedded systems
PDF
C Programming - Refresher - Part III
PDF
New c sharp3_features_(linq)_part_iii
PDF
PPTX
Matlab for diploma students(1)
DOCX
Bitstuffing
PPTX
Procedures And Functions in Matlab
Matlab Serial Port
interfacing matlab with embedded systems
C Programming - Refresher - Part III
New c sharp3_features_(linq)_part_iii
Matlab for diploma students(1)
Bitstuffing
Procedures And Functions in Matlab

What's hot (20)

PDF
An ntutorial[1]
PPTX
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
PPT
Basics of programming in matlab
PPTX
Introduction to Matlab Scripts
DOC
Advance data structure
PPTX
Programming Environment in Matlab
DOCX
MATLAB BASICS
PPTX
Design of a two pass assembler
ODP
(6) collections algorithms
PPTX
PPT
Matlab Basic Tutorial
PDF
Matlab introduction lecture 1
PPT
Chapter Seven(2)
PPT
Os Reindersfinal
PPTX
C Programming : Arrays and Functions
PDF
Generacion de codigo ensamblado
PPTX
C Programming : Pointers and Strings
PPTX
Matlab Programming Tips Part 1
PPTX
Computer Science Assignment Help
An ntutorial[1]
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Basics of programming in matlab
Introduction to Matlab Scripts
Advance data structure
Programming Environment in Matlab
MATLAB BASICS
Design of a two pass assembler
(6) collections algorithms
Matlab Basic Tutorial
Matlab introduction lecture 1
Chapter Seven(2)
Os Reindersfinal
C Programming : Arrays and Functions
Generacion de codigo ensamblado
C Programming : Pointers and Strings
Matlab Programming Tips Part 1
Computer Science Assignment Help
Ad

Viewers also liked (16)

PPTX
Dissecting BetaBot
PPTX
Presentation1
PPTX
Project 1
PPT
Acuerdo 592
PPTX
Internetový marketing - tvorba webových stránek
PPTX
collin decker unit 1 photography basics
PPTX
plan de estudios 2011
PDF
Computer viruses. - Free Online Library
PPTX
Film Reference
PPTX
Evaluation. Q2. How does your media product represent different social groups?
PPTX
Casting profiles
PDF
新生兒準備物品5大類(食衣住行育樂)
PPT
Unit 4 photography
PPTX
Location recce
PDF
Catalogo Dolciaria Severini - Norcia Pasticceria
PPTX
Presentación ingles kiara unad
Dissecting BetaBot
Presentation1
Project 1
Acuerdo 592
Internetový marketing - tvorba webových stránek
collin decker unit 1 photography basics
plan de estudios 2011
Computer viruses. - Free Online Library
Film Reference
Evaluation. Q2. How does your media product represent different social groups?
Casting profiles
新生兒準備物品5大類(食衣住行育樂)
Unit 4 photography
Location recce
Catalogo Dolciaria Severini - Norcia Pasticceria
Presentación ingles kiara unad
Ad

Similar to Serial comm matlab (20)

PDF
Control of Industrial Pneumatic & Hydraulic Systems using Serial Communicatio...
PDF
Control of Industrial Pneumatic & Hydraulic Systems using Serial Communicatio...
PDF
I2c interfacing raspberry pi to arduino
PDF
[Apostila] programação arduíno brian w. evans
PDF
Ardx eg-spar-web-rev10
PDF
CS150_Project Report
PDF
Ardx experimenters-guide-web
PDF
arduino
PDF
Syed IoT - module 5
PPTX
Fun with arduino
PDF
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
PDF
Arduino experimenters guide hq
PDF
Arduino experimenters guide ARDX
PPTX
Electronz_Chapter_14.pptx
PDF
Start with arduino
PDF
Isa bus nptel
PDF
Lesson-4-Arduino-Programming-dsBasics.pdf
PPTX
Ch_2_8,9,10.pptx
PDF
Lily pad dev_handout
PDF
COMPUTER COMMUNICATION NETWORKS -IPv4
Control of Industrial Pneumatic & Hydraulic Systems using Serial Communicatio...
Control of Industrial Pneumatic & Hydraulic Systems using Serial Communicatio...
I2c interfacing raspberry pi to arduino
[Apostila] programação arduíno brian w. evans
Ardx eg-spar-web-rev10
CS150_Project Report
Ardx experimenters-guide-web
arduino
Syed IoT - module 5
Fun with arduino
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Arduino experimenters guide hq
Arduino experimenters guide ARDX
Electronz_Chapter_14.pptx
Start with arduino
Isa bus nptel
Lesson-4-Arduino-Programming-dsBasics.pdf
Ch_2_8,9,10.pptx
Lily pad dev_handout
COMPUTER COMMUNICATION NETWORKS -IPv4

Serial comm matlab

  • 1. Serial Communication between Arduino and MATLAB By- Aman Mangal IIT Bombay June 6, 2012 Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 1/36
  • 2. Prerequisite Serial data transfer in Arduino. Follow the link to learn more- 1 http://arduino.cc/en/Reference/serial 2 http://www.ladyada.net/learn/arduino/lesson4.html MATLAB programming. To learn more, go to- 1 http://users.ece.gatech.edu/bonnie/book/TUTORIAL/ tut_1.html 2 http://www.mathworks.in/help/techdoc/matlab_ product_page.html You should be familiar with MATLAB structures, MATLAB objects etc. Basic programming concepts. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 2/36
  • 3. Serial Communication Serial means "One after another". Serial communication is when we transfer data one bit at a time, one right after the other. Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. Just like we turn an LED on and o, we can also send data. One side sets the pin and the other reads it. MATLAB can read/send the data from/to the serial port of the computer and process it. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 3/36
  • 4. Buer It is most important to understand the nature of buer to avoid errors later while writing codes. There exists a buer between the two events of sending and reading the data. Say a sensor is streaming back data to your program, more frequently than your program reads it. Then the data is stored to a list which we call a buer. One writes data into it and other reads it, may be with dierent speeds. Buer are of
  • 5. nite length. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 4/36
  • 6. Buer...in detail Initially the buer is empty. As new data values come in they get added to the bottom of the list (most recent data). If your program reads a value from the buer, it starts at the top of the list (oldest data). Once you read a byte of data, it is no longer in the buer. The data in the second position on the list moves up to the top position As soon as the buer is full and more data is sent, the oldest data gets discarded to make space for new data. You have to be smart enough not to loose data. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 5/36
  • 7. Buer...example We set up the Serial communication between arduino and PC with a buer length of 5. 1 2 3 4 5 Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 6/36
  • 8. Buer...example The value 10 and 6 are transferred respectively. 1 10 2 6 3 4 5 Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 7/36
  • 9. Buer...example One value is read. The value 10 is the oldest. So it will be read
  • 10. rst. 1 6 2 3 4 5 Note that 10 no longer exists in the buer. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 8/36
  • 11. Buer...example Now, assume that the buer is full. 1 6 2 4 3 5 4 9 5 3 What will happen if we write one more data value, say 12, to the buer? Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 9/36
  • 12. Buer...example The buer will look like as follows- 1 4 2 5 3 9 4 3 5 12 Note that the oldest data 6 is discarded. All the data is shifted up and new value is added as the last entry to the buer. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 10/36
  • 13. Before we continue... We have to connect the Arduino board to the PC. Each serial port on the PC is labeled COM1, COM2 etc. The Arduino will be given a COM Port Number. You should
  • 14. gure it out by following these steps- 1 Right click on My Computer icon and select Manage. 2 Select Device Manager in the tree view you will see on the left side in the new window opened. 3 Find and select Ports(COM LPT) in the center panel. 4 There, you will
  • 15. nd lists of all the ports attached to your computer. 5 Figure out the one you are concerned with. Refresh the window if you don't
  • 16. nd any! Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 11/36
  • 17. Almost ready... Now we are ready for the MATLAB and Arduino serial communication. We will only focus upon the MATLAB. We will study how can we set up serial port objects, send and read data from the buer in MATLAB. I assume the you already know how to send/read data in Arduino. Follow the link given in the beginning of the tutorial if you wish to learn Serial Communication in Arduino. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 12/36
  • 18. Styles used throughout the tutorial All the MATLAB commands are written in italics and preceded by The MATLAB output is written in blue color everywhere. Use help 'command' doc 'help text' to get help in MATLAB for any command used in the tutorial. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 13/36
  • 19. Setting up Serial Port Object We
  • 20. rst need to create a serial port object. Serial port object is just a name given to that serial port so that we can use it in later commands. s = serial ('COM1'); Serial Port Object : Serial-COM1 Communication Settings Port: COM1 BaudRate: 9600 Terminator: 'LF' Communication State Status: closed RecordStatus: o Read/Write State TransferStatus: idle BytesAvailable: 0 ValuesReceived: 0 ValuesSent: 0 Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 14/36
  • 21. Setting up Serial Port Object This line of command only constructs the object. It does not check/setup/initialize the communication. This command will still work even if the serial port is not connected to any device. Many objects can be created for a serial port but only one can be connected at a time.(we will see later) This shows all the property of the constructed serial port object. In MATLAB, s is a structure which has all the above properties. We can access/modify them using dot(.) operator. Note that the Status is closed. It implies that the serial port is not connected. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 15/36
  • 22. 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 dierent 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 Serial Communication between Arduino and MATLAB 16/36
  • 23. 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: o Read/Write State TransferStatus: idle BytesAvailable: 0 ValuesReceived: 0 ValuesSent: 0 Note the new value of BaudRate shown. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 17/36
  • 24. Properties of Serial port object As we have already stated that s is of MATLAB datatype called structure(similar to structures in C++). There are lots of exibility MATLAB provides to change the properties of the serial port object. The commands are similar to what we have used for BaudRate. Use following command to list down all these properties and their current value- get(s) To see the possible values of all these properties, use- set(s) Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 18/36
  • 25. Setup the connection Before you actually write the data to the serial port, you must connect to device. This is like a JAVA lock. Only one entity can acquire the lock at a time. Use fopen to acquire the lock and setup the connection. fopen(s) Notice the status property of the serial port object- s.Status ans = open If the lock is already acquired, fopen will give an error. To avoid error,
  • 26. rst check the Status property of the serial port object. If it is closed then try to setup the connection. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 19/36
  • 27. Writing to the Serial Port in MATLAB MATLAB can write any kind of data to the serial port binary, string, int, oat etc. with speci
  • 28. ed precision. We use fwrite or fprintf to write data. Transfer an int/ oat array- fwrite(s, vector array, 'precision'); The precision speci
  • 29. es the datatype of the vector array. It can be 'int8', 'int16', ' oat32', ' oat64', 'uint8', 'char' etc. String- fwrite(s, 'string'); You can use fprintf for strings as well- fprintf(s, 'string'); You can specify the format in fprintf for a string. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 20/36
  • 30. Reading from Serial Port in Arduino You can follow these steps to read data in Arduino- You can choose the kind of data you are expecting, otherwise byte dataype can be used. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 21/36
  • 31. Writing to Serial Port in Arduino Use following function to write data in Arduino to the Serial Port. Note the use of println not print. This will be helpful when we will read the data in MATLAB. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 22/36
  • 32. Reading from Serial Port in MATLAB fscanf is used to read the data- data = fscanf(s); This will read all the data until it
  • 33. nds a new line/Terminator. That is why we used println instead of print. By following this procedure you will be reading all the data sent in one command of Serial.println at a time. In this case, MATLAB automatically converts data into the correct format and stores into the MATLAB variable. If there is no data to read, a time out will occur. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 23/36
  • 34. Reading from Serial Port in MATLAB To avoid time out, you can
  • 35. rst check the BytesAvailable property of serial port object just like we did in Arduino- if s.BytesAvailable 0 data = fscanf(s); end You can specify the size of the data you want to read in case you use Serial.print. You have to specify the format of data together with the size of the data. fscanf(s, 'format', size); Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 24/36
  • 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 doesn't 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. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 25/36
  • 37. Format Format is basically C conversion speci
  • 38. cation language. The following formats can be used- Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 26/36
  • 39. Size There are 2 ways- The
  • 40. lling in the matrix takes place columnwise. The
  • 42. lled, then 2nd column and so on. This will keep reading until the vector/matrix of the given size is created. Time out will occur if the available data is not enough to
  • 43. ll the whole vector/matrix. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 27/36
  • 44. BuerSize It is very important not to loose the data while the data gets transferred. As mentioned before, the data gets discarded if the Buer 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 BuerSize in MATLAB according to the need of program using following methods- set(s, 'BuerSize', 1024) %in bytes s.BuerSize = 1024; %in bytes (Similar to BaudRate) Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 28/36
  • 45. 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 doesn't. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 29/36
  • 46. End the Connection This is very important step while Serial Communication. Make sure the execution of this step even in case the data is not transferred. Use fclose to end the connection- fclose(s) Delete the serial port object if you are done with communication. delete(s) clear s So if fopen is executed then fclose must be executed anyhow otherwise it will create problem when you use it next time. You should use try-catch statement to ensure that the serial port is closed before the program ends execution. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 30/36
  • 47. To remember You cannot simultaneously view the data in MATLAB and Arduino serial port because the data can only be read once. The data is removed as soon as it is read
  • 48. rst time. The synchronisation of data transfer is very important while communication. Always check for the property before setting its new value. For example before you use fopen, check whether the serial port is already used. Check the BytesAvailable property before you read the data in MATLAB. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 31/36
  • 49. To remember Be selective while you choose the datatype of the data. It will aect the time taken in communication. Choose an optimum BuerSize. Always close the connection in the end so that we can use the port next time. Close the connection in MATLAB before you upload your code to Arduino. Whenever you setup a new connection, it ushes all the data sent/received earlier. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 32/36
  • 51. nd
  • 52. nds all the possible, existing serial port objects. It returns an array of serial port object. h = instr
  • 53. nd 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. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 33/36
  • 54. INSTRFIND....advanced You can specify the properties of the serial port object you want to search for- out1 = instr
  • 57. nd returns array/vector of serial port object. It is used when you want to retrieve a deleted serial port object. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 34/36
  • 58. More to come We have learnt all the basics of Serial Communication in MATLAB. We can extend this knowledge to make a real time serial communication system. You can make a system which reads the data from a digital pin of Arduino and transfers all the data to MATLAB in real time. Plot the received data on the graph in MATLAB. The pin number may be speci
  • 59. ed by the user in MATLAB. I will present the solution of the problem after a week. Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 35/36
  • 60. Thank you Thanks for your patience while reading the tutorial. I hope you
  • 61. nd it useful. Use MATLAB help to know more about anything we have learnt here. Contact me for any query: [email protected] Aman Mangal, IIT Bombay Serial Communication between Arduino and MATLAB 36/36