Why am I not able to communicate with an instrument connected over Serial/RS-232 using FPRINTF in MATLAB?

4 views (last 30 days)
I am trying to communicate with an instrument connected over a Serial (RS-232) connection on my PC. I have tried doing this with HyperTerminal and MATLAB.
The interaction with the device using HyperTerminal has been working fine. I am able to send commands and get responses back both by manually typing the commands at the terminal, and by using the “Send text file” feature, with a text file containing pertinent commands.
However, when I try to communicate with the device through MATLAB by using the FPRINTF command, I receive the issued command back; i.e., the issued command is echoed back to MATLAB after using the FSCANF command. I observe the same behavior when trying more low-level routines, such as FWRITE to send commands, and FREAD to receive the instrument's replies.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
1) Typically, while typing in the data from the HyperTerminal, it is observed that along with the data, a Carriage Return gets appended to the message in addition to a Line Feed (if using the "Send line ends with line feeds" feature in ASCII Setup). Using Serial Port monitoring software, one can observe that the transmitted data from HyperTerminal has 'OD' and 'OA' appended to it, which represent Carriage Return and Line Feed, respectively.
2) However, when you are trying to write data from MATLAB, the data has just the Line Feed appended to it by default. Hence, in order to include the Carriage Return as well, set the Terminator property to a new value as shown in the following example:
set(obj,'Terminator','CR/LF')
3) The echoing process while sending data from MATLAB could also be occurring due to the device being in an error state. Verify if this is true by consulting the device documentation.

More Answers (0)

Products


Release

R2008a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!