Matlab List Serial Ports

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.


Hello friends, hope you all are having fun and enjoying life. In today’s post we are gonna see how to send data to serial port in MATLAB. Its a requested tutorial, asked by a follower and after giving him the code, I thought to share it on our blog so that others could also get benefit from it. We have discussed serial port many times and have seen how to communicate with it using different software but we haven’t yet discussed how to send data to serial port in MATLAB. So, in today’s post I am gonna share the complete code for sending data to serial port in MATLAB.Serial port is most common way of communication, we can send or receive data using serial port. Normally, in engineering projects there’s a need to send or receive data from microcontrollers to computer and in such projects, we used serial communication as its easy and quite quick in communication.

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:

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
data = input(‘Enter character: ‘, ‘s’); %Ask user to Enter character
fprintf(tep,data); %Print character ‘a’ to the serial port
disp(‘Charater sent to Serial Port is:’);
disp(data);
fclose(tep); %Close Serial Port Object

  • The screenshot of code is as follows: (I am adding the screen shot of code because its colored and thus helps better in understanding the code)
  • Now when you run this m file, you will get results as shown in below figure and now you can see I have sent my blog url via serial port in Matlab.

Matlab Serial Command

  • I think we have played enough with sending data via serial port in MATLAB, now you can send any data via serial port in MATLAB, for example you can also create an infinite loop and keep on sending data to serial port.
  • That’s all for today, in the coming post I will show how to receive data via serial terminal in MATLAB, so stay tuned and also subscribe us via email to get these exciting tutorials straight in your mailbox. Take care. 🙂


JLCPCB – Prototype 10 PCBs for $2 (For Any Color)

China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w

Category: MATLABBy Syed Zain Nasir8 Comments

Author: Syed Zain Nasir

https://www.theengineeringprojects.com/I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Overview

Matlab has a 'serial' function that allows it to communicate through a serial port. This project is to establish serial port connection with the PIC microcontroller and demonstrate bidirectional communication between the PIC and a Matlab program. For demonstration purposes, the PIC will send digital potentiometer readings to Matlab as well as receive keystrokes from the Matlab user to light up LEDs on its circuit board.

A USB to RS232 adapter and level shifter chip were used to connect the computer to the PIC. In this lab, we used a cheap cable found at http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=220199148938&ih=012&category=41995&ssPageName=WDVW&rd=1


**Important! DO NOT connect the serial Rx/Tx lines DIRECTLY to the PIC!!!**

A level shifter chip is necessary to convert the high and low logic voltages from the desktop computer (+12V/-5V) to (+5V,0V) for the PIC. A standard RS232 connection is called a DB9 connector and follows the pin diagram shown here: http://www.aggsoft.com/rs232-pinout-cable/serial-cable-connections.htmThis cable requires 1 driver installation as included on the mini-cd. To install this driver, you must first plug in the USB cable, and run the installation program located on the CD corresponding to the model on the USB Cable (<CDROM>:HL-232-340HL-340.exe). This driver is also available online at this link: http://129.105.69.13/pic/usb_drivers/HL-340_USB_serial_drivers_WinXP/ . To configure the Matlab script to connect to the proper serial port, use the device manager (Right click My Computer->manage) and expand the section 'Ports (COM & LPT)'. Make a note of the COM port number corresponding to 'USB-SERIAL CH340' as listed in this section. In our program, our serial port was COM4. A picture is shown below of how to get this information in the device manager.

COM Port Lookup - Device Manager


A female DB9 connector was wired to our level shifter to convert the voltages, with the level shifter connected to our PIC. The female DB9 connector is used so no wires need to be directly soldered to the serial cable. Refer to the Circuit section for details on this connection.

The PIC was programmed with our C code as shown below. Our program was designed to read a potentiometer through the PIC's ADC (Analog to Digital Converter) port and transmit the digitized readings over the serial cable to the PC (upon request). In Matlab, if a users sends data to the PIC by entering a character, the PIC responds with the current potentiometer reading and the last received byte from the PC. The PIC is also programmed to display the character received from the PC on its LED array (D register) as a 8-bit ASCII number. The programs can easily be modified to create any custom protocol, but are designed to show simple 2-way communication between Matlab and the PIC.

Circuit

The wiring diagram for serial communication is shown below. There are three basic components in this setup. The potentiometer serves as an analog input to the PIC, which is converted to a digital signal through the PIC's analog to digital converter pin. The MAX232N level converter provides bidirectional voltage shifting for digital communication between the PIC and PC (read more about this chip and level conversion on the RS232 wiki here). Finally, the female DB-9 connector allows the circuit to connect to the PC's serial port.

Circuit Diagram for Serial Communication between PIC and PC

The connections to the female DB-9 adapter are shown below. These wires are soldered to the cup-side of the adapter, not directly to the serial cable. Our DB-9 adapter is pictured below and follows the given connections:

Closeup of DB-9 Connector

Our final circuit is pictured below.

Image of wiring for serial communication between PIC 18F4520 and PC

Matlab List Serial Ports In Linux

PIC Code

Tips on Designing a Protocol

Matlab Serial Port Read

A good way to start or debug your program is to use the PIC-C Serial Port Monitor (Tools Tab->Serial Port Monitor). This will allow you to send and receive raw data over the serial port, which is much easier for understanding why a protocol isn't behaving correctly. You can also use HyperTerminal on windows XP (Start->Programs->Accessories->Communications->HyperTerminal), although in our testing this appeared to be less stable than the PIC-C Compiler's monitor. Note for both programs, you will have to configure which serial port to monitor using the same method described in the Overview.


Be sure to close all other programs accessing the serial ports (PIC-C/Hypterm etc.) if you are having difficulty opening the port in MATLAB.

Matlab Code

**** THIS MATLAB CODE SHOULD BE UPDATED TO USE 'fread(s,1)' instead of 'fscanf(s)'. fread(s,1) reads 1 bitwise value at a time (opposed to an ascii value). Without this change, matlab can only read 8 bit ASCII characters and will reject a subset of the values between 0 and 255. *****

Matlab list all serial ports

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'