Serial port name, specified as a character vector or string. The seriallist function provides a list of available serial ports. You must specify the port to create a serial port object. The port name depends on the platform that the serial port is on.
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
PIC CodeTips on Designing a Protocol
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. *****
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.
O Saki O Saki Re Song (Batla House)“Batla House” is the type of action movie and this film directed by Nikkhil Advani. Batla House is based on the 2008 Batla House encounter & has John Abraham playing DCP Sanjeev Kumar Yadav. The film is listed to release histrionically on 15 August 2019. Audio song download new indian.
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'
Active2 years, 5 months ago
I'm trying to establsih serial communication with an Arduino through USB (running Arch Linux). I can do it in a straightforward way trough a C++ program and using
boost::asio , but recently I installed Matlab and been encoutering some issues. I manage to create the serial object with s0=serial('/dev/ttyACM0') but when I call fopen(s0) I get the following error:
joaocandrejoaocandre
3 Answers
Here is what I did to get serial port communication work in Matlab R2014a on Arch Linux 64 bit:
1a) follow the steps described here: http://www.matlabarduino.org/serial-communication.html:
sudo chmod 777 /dev/ttyACM0 Alternatively, add your user to the group uucp: > sudo gpasswd --add username uucp
sudo nano $MATLABROOT/bin/$ARCH/java.opts --> add: -Dgnu.io.rxtx.SerialPorts=/dev/ttyS0:/dev/ttyUSB0:/dev/ttyACM0
1b) check that the connection works in gtkterm (select port ttyACM0)
2) additionally (critical only for Matlab):
sudo chmod 777 /run/lock /run/lock was symlinked from /var/lock on my distro, so you might have to do this with the latter dir (was 755); alternatively, you can manage access rights to /run/lock/ by ACL.
![]() Matlab Serial Example
How I got to this solution:
sudo strace -p 4668 -f -s100 2>&1 | grep -C3 --color -i -e /dev -e serialports -e uucp -p: process ID second column from > sudo ps -aux | grep -i matlab Then, in Matlab type >> sps=instrhwinfo('serial') (which in my case always returned a structure of empty cell-arrays) and monitor the output of strace. Asme b31.8s free download.
Hope that helps!cheers :)
user5161769user5161769
Matlab Get List Of Serial Ports
By default, only root can use the serial port.
And you can add your id to the serial group 'dialout', so you can use the serial port.
MalvinMalvin
Just make soft link from /dev/ttyACM0 to /dev/ttyS[0-255].
Below Matlab R2017a may face this issue.
Detailed Description can find here:
Hope this can be helped.
Matlab Serial Command
SLKunSLKun
Not the answer you're looking for? Browse other questions tagged c++matlabserial-port or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |