The list includes virtual serial ports provided by USB-to-serial devices and Bluetooth Serial Port Profile devices. This provides a list of the serial ports that you have access to on your computer and could use for serial port communication. Can't open serial port on Matlab UNIX. Ask Question Asked 4 years, 11 months ago. Here is what I did to get serial port communication work in Matlab R2014a on Arch Linux 64 bit: 1a). So you can use the serial port. Share improve this answer. Answered Oct 22 '14 at 9:22. The serial port session comprises all the steps you are likely to take when communicating with a device connected to a serial port. These steps are: These steps are: Find your serial ports — Display a list of serial ports on your system using the seriallist function. The serial port object behaves according to the previously configured or default property values. Disconnect and clean up — When you no longer need the serial port object, remove it from the MATLAB ® workspace using the clear command.
Send data to Serial Port in MATLAB
tep=serial(‘COM1’, ‘BaudRate’, 9600);
fopen(tep);
fprintf(tep,’a’);
fclose(tep);
- Now you can see, its too simple, we just set the com port with which we want to communicate and after that we gave the bud rate.
- In the next line, we open our serial port object.
- Now as our serial port is open, we can send any character to it. In this code, I am sending ‘a’ to serial port in MATLAB.
- And finally I closed the serial port, which is very necessary as otherwise when you run this code again, it will start giving error.
- Here’s a bit explanatory code and much more flexible as you can change any property of Serial Port in MATLAB, you want to change.
- Here’s the code:
clc
clear all
close all
disp(‘ Welcome to TEP!!!’);
disp(‘ ‘);
disp(‘ www.TheEngineeringProjects.com’);
disp(‘ ‘);
tep=serial(‘COM1’); % assign serial port object
set(tep, ‘BaudRate’, 9600); % set BaudRate to 9600
set(tep, ‘Parity’, ‘none’); % set Parity Bit to None
set(tep, ‘DataBits’, 8); % set DataBits to 8
set(tep, ‘StopBit’, 1); % set StopBit to 1
%display the properties of serial port object in MATLAB Window
disp(get(tep,{‘Type’,’Name’,’Port’,’BaudRate’,’Parity’,’DataBits’,’StopBits’}));
fopen(tep); % Open Serial Port Object
fprintf(tep,’a’); %Print character ‘a’ to the serial port
disp(‘Charater sent to Serial Port is “a”.’);
fclose(tep); %Close Serial Port Object
- The code is quite self explanatory plus I have also added the comments in the code which will help you in understanding the code but still if you have any problem, then ask in comments.
- Here’s a screen shot of the above code:
- Wen you run this code, you will get a below response in your MATLAB window:
- Till now, we have seen how to send a single character defined in the m file of MATLAB, now let’s make it a bit complex and send user defined data.
- Now before sending the data, we will first ask the user to enter the data, he wants to send to serial port. Tis data could be a single character or could also be a combination of characters.
- In order to do so, we are gonna use Input command in MATLAB and code is as follows:
If your program doesn't close and delete the serial port object correctly, you can use the command shown below to delete all of the serial port objects.
External Links
More on Serial and the PIC: http://hades.mech.northwestern.edu/wiki/index.php/PIC_RS232
MAX232 Data Sheet: http://rocky.digikey.com/WebLib/Texas%20Instruments/Web%20data/MAX232,232I.pdf
Overview of RS232 Protocol: http://en.wikipedia.org/wiki/RS-232
Retrieved from 'http://hades.mech.northwestern.edu/index.php?title=Serial_communication_with_Matlab&oldid=13153'