Cannot read serial port data with Matlab R2010a
Show older comments
I am trying to write a Matlab script for sampling voltage data from an Agilent 34401A digital multimeter over the serial RS232 connection. I am able to open the connection, write the SCPI commands, but I cannot read any responses from the multimeter. There are never any available bytes, and fscanf, fread, etc. do not find any data. Interestingly, the only way I have been able to receive data is over the Agilent Connection Expert Interactive IO interface. I have also tried the same SCPI commands in Python to no avail, as well as CoolTerm. But since it works in the Agilent software, I know it cannot be the driver, cable, or USB-serial converter I am using.
Here is the setup:
Matlab version R2010a Agilent 34401A DMM Belkin F5U109 USB-serial converter (with driver installed) Windows Vista Basic Agilent IVI/VISA drivers installed for the DMM
Here is the code that I am using to debug this issue:
serialObject = serial('COM4');
set(serialObject, 'BaudRate', 9600);
set(serialObject, 'Parity', 'none');
set(serialObject, 'ReadASyncMode', 'continuous');
set(serialObject, 'StopBits', 1);
set(serialObject, 'Timeout', 10);
set(serialObject, 'RequestToSend', 'off');
set(serialObject, 'FlowControl', 'none');
set(serialObject, 'Terminator', 'LF');
fopen(serialObject);
fprintf(serialObject, '*RST\n');
fprintf(serialObject, '*CLS\n');
fprintf(serialObject, '*IDN?\n');
pause(1);
fscanf(serialObject)
fclose(serialObject);
delete(serialObject);
At the fscanf() part it always times out and never finds data. I have checked this manually using serialObject.bytesAvailable as well.
I googled this problem for hours, finding at least a dozen reports of this issue but as of yet, I have not found a single solution. Can anyone help me with this dilemma? =[[[[[[[[ Thank you in advance.
Answers (8)
Mark
on 27 Apr 2011
Walter Roberson
on 27 Apr 2011
0 votes
stop bits should be 2
When you use fprintf like that, two newlines will be sent: the one you coded specifically and the one that is in the default format of '%s\n'
See also the debugging hints in the first document link.
You might need to turn on DTR.
You might need to change the *CLS to *CLR
Will Reeves
on 4 Jul 2014
Edited: Will Reeves
on 4 Jul 2014
0 votes
I know this issue dates back from 2011 but I had the same problem yesterday and had to dig up the manual - might save someone else some time...
The issue is actually the "DSR" (Data Set Ready) pin (6). I'm not sure exactly what the problem is. It could be that the cable I used doesn't have this pin (and DTR) connected.
Alternatively, Matlab has an option for hardware flow control but only really mentions use of the RTS and CTS pins. The Meter doesn't use this protocol so querying serial_obj.Pinstatus yields all "off".
The Multimeter manual states:
"The multimeter monitors the DSR line to determine when the controller is ready to accept data over the interface. The multimeter monitors the DSR line (pin 6 on the RS-232 connector) before each character is sent. The output is suspended if the DSR line is FALSE. When the DSR line goes TRUE, transmission will resume."
So this is the cause. With no flow control you can send commands fine, but the Meter will never reply unless the DSR line is held "high". Matlab does not allow manual control over this so you'll have to hold it high yourself.
I worked around the problem by rewiring the serial cable so that the DTR (Data Terminal Ready), pin 4 was connected to pin 6 (DSR) and all was fine!
I also complied with the manual:
"To disable the DTR/DSR handshake, do not connect the DTR line and tie the DSR line to logic TRUE. If you disable the DTR/DSR handshake, also select a slower baud rate (300, 600, or 1200 baud) to ensure that the data is transmitted correctly."
by setting the baud rate to 1200...
It might be possible to do some more rewiring to get hardware flow control fully working, but I had what I needed for a quick experiment.
Anang sutawijaya
on 6 Mar 2015
0 votes
hallo my all friend i have problem with serial send this is " Error evaluating registered method 'Start' of MATLAB S-Function 'sserialsb' " please help me to solve this.
Anang sutawijaya
on 6 Mar 2015
0 votes
this is my plan
Anang sutawijaya
on 6 Mar 2015
0 votes

bellili mahjoub
on 3 Apr 2016
0 votes
Hi , I have a problem : someone help me ; how to read the measured mass of an electronic scale and display on GUI (I did MATLAB ) , wearing the electronic scale is RS232 . I have a USB / RS232 which can connect with PC, ( algorithm ) , finally thank you. I made a graphic interface for MATLAB, and I want to automatically display the weighed value on laptop PC when weighing a product.
1 Comment
Walter Roberson
on 5 Apr 2016
Please start a new Question for that topic.
alican kara
on 17 Jun 2020
0 votes
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!