Serial Port Communication in Matlab

In summary, the serial port on the computer may not be configured correctly, and the data transfer between the devices and the computer may be blocked.
  • #1
Jopi
14
0
Hi,
I have a cooler and a pressure gauge which I need to operate from Matlab. Both devices are connected to the computer via a serial port. I've been using the Matlab Instument Toolbox Test & Measurement tool to try and communicate with these machines. The problem is that I don't get any data from them, and as far as I know, can't send them any data either.
The manual for the cooler can be found http://golem.merate.mi.astro.it/projects/rem/secure/archives/manuals/polar7/GA125004E.pdf" and a description of the RS-232 interface in part 4 of that document. I've set up the baud speed, number of data bits and stop bits, parity and terminator as specified. But if I try to send any commands to the device, I get no reply before the timeout.
I've sent the data in ASCII format and I've tried the different data formats, but I get no response. Can you tell me what is it that I'm doing wrong?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I'd suggest breaking out the oscilloscope, a breadboard, and a spare DB-9 connector with soldered wire connections.

Don't have any of the above? Well, if you have a multimeter with continuity or resistance check, make sure that the connections are wired straight through (i.e. pin 2 on one end goes to pin 2 on the other end, pin 3 goes to pin 3 and pin 5 goes to pin 5). This is the type of cable you want. Some serial cables are actually cross-over (they usually have the same gender on both ends, however), and are wired pin 2 to pin 3.

Take a look at this page for more details (specifically, DTE--your PC--vs. DCE--most peripheral devices):
http://www.taltech.com/TALtech_web/resources/intro-sc.html

Sucks that you need to send out 7 bytes in such an odd parity, however. And this might sound simple, but have you checked that your serial port is configured properly, and that you're using the correct serial port? Again, an oscilloscope may help here, unless you have another serial device that you have that you KNOW works.
 
  • #3
MATLABdude said:
I'd suggest breaking out the oscilloscope, a breadboard, and a spare DB-9 connector with soldered wire connections.

Don't have any of the above? Well, if you have a multimeter with continuity or resistance check, make sure that the connections are wired straight through (i.e. pin 2 on one end goes to pin 2 on the other end, pin 3 goes to pin 3 and pin 5 goes to pin 5). This is the type of cable you want. Some serial cables are actually cross-over (they usually have the same gender on both ends, however), and are wired pin 2 to pin 3.

Take a look at this page for more details (specifically, DTE--your PC--vs. DCE--most peripheral devices):
http://www.taltech.com/TALtech_web/resources/intro-sc.html

Sucks that you need to send out 7 bytes in such an odd parity, however. And this might sound simple, but have you checked that your serial port is configured properly, and that you're using the correct serial port? Again, an oscilloscope may help here, unless you have another serial device that you have that you KNOW works.

Thanks for your reply. I haven't built the setup myself, and I think I should not change it in any way. I know that at least the cooler works, I tried it using the old LabView software. The oscilloscope I have here uses a TCP/IP connection, so I can't try the serial port with that.

I don't have admin rights on this computer (which I definitely should have!), so I can't setup the serial port from Windows settings, but I can configure it in Matlab. But I think that if LabView can override Windows' settings, then I guess Matlab is also able to do that.
 
  • #4
Unfortunately, I mean break out the plain-jane oscilloscope, and look at the waveforms it's putting out, not break out the logic analyzer with serial capture ;-)

But check the cable first, as per my earlier post.
 
  • #5
MATLABdude said:
Unfortunately, I mean break out the plain-jane oscilloscope, and look at the waveforms it's putting out, not break out the logic analyzer with serial capture ;-)

But check the cable first, as per my earlier post.

But I can control and get readings from the cooler with LabView, doesn't that imply that the cable is ok?
 
  • #6
Jopi said:
But I can control and get readings from the cooler with LabView, doesn't that imply that the cable is ok?

Sorry, misread that part. Yes, your cable is okay. Next up, find out which comm port is being used and go from there!
 
  • #7
MATLABdude said:
Sorry, misread that part. Yes, your cable is okay. Next up, find out which comm port is being used and go from there!

I know the pressure gauge uses COM5 and the cooler is connected to COM6. If I click 'Connect' in the Test & Measurement toolbox I can connect to both ports, but Matlab cannot identify any devices. If I try to send a query to either device, I get no response before the timeout.
The datatype for pressure gauge is 1 start bit, 8 data bits, no parity bit and one stop bits. After each message there should be a CR and a LF. I've set the Matlab serial port settings accordingly. I've formatted the commands as strings (%s), I guess this should be okay since the pressure gauge transmits data and receives commands as ASCII strings. If I want to request a pressure reading I send a command PRX followed by a carriage return and a line feed. After receiving the command, the pressure gauge responds with an ACK,CR,LF. The gauge transmits the reading after receiving and enquiry.
I don't know if its the above procedure that I'm doing wrong, or something else. It's difficult to find out without any actual error messages.
 
  • #8
I got the devices working now. The problem with the pressure gauge was that someone had changed the baudrate from the default value and that I had to first send an <ENQ> and the read. The cooler required an <STX> at the beginning of the message, and once I figured out how to send that it began working.
 

1. What is Serial Port Communication in Matlab?

Serial Port Communication in Matlab is a method for transferring data between a computer and an external device, such as a microcontroller or sensor, through a serial connection. It allows for real-time data acquisition and control of external devices using Matlab code.

2. How do I set up serial port communication in Matlab?

To set up serial port communication in Matlab, you will need to first identify the correct serial port on your computer and the baud rate of the device you are connecting to. Then, you can use the serial() function in Matlab to create a serial object and specify the port and baud rate. Finally, you can use the fopen() function to open the serial connection and begin communication.

3. How do I read data from a serial port in Matlab?

To read data from a serial port in Matlab, you can use the fread() function to read a specified number of bytes of data from the serial port. You can also use the fscanf() function to read data in a specific format, such as a string or numerical value. It is important to specify the correct format and number of bytes to read in order to receive accurate data.

4. How do I write data to a serial port in Matlab?

To write data to a serial port in Matlab, you can use the fwrite() function to send a specified number of bytes of data to the port. You can also use the fputs() function to write a string of characters to the port. It is important to specify the correct data format and number of bytes to send in order to ensure proper communication with the external device.

5. Can I use serial port communication in Matlab for real-time applications?

Yes, you can use serial port communication in Matlab for real-time applications. Matlab has built-in functions for reading and writing data in real-time, allowing for fast and efficient communication with external devices. However, the speed of the communication is limited by the baud rate of the serial connection and the processing speed of the computer.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
Back
Top