MATLAB Answers


Using fprintf and instrument control toolbox with serial connection

Asked by Senaasa on 30 Sep 2013
Latest activity Answered by Walter Roberson
on 30 Sep 2013


I'm trying to send commands to a monochromator (CM110/112) connected to a cpu (RS232). I've successfully connected to the instrument using serial() and also by using the instrument toolbox but I'm having trouble understanding how to send commands to it. The manual gives the following example:

This command moves the monochromator to a selected position. 
To CM110/112: <16> D <High Byte> <Low Byte> [*The D denotes decimal value of the byte is being used)
From CM110/112: <Status Byte>
CM110/112 Action: If valid, move to position ( 256 * <High Byte> ) + <Low Byte> [units].
From CM110/112: <24> D
For example, the command to instruct the monochromator to GOTO the wavelength 250 nm could be sent as the three bytes <16> D <0> D <250> D (if the current units are in nm). Here, <16> D specifies the


Log in to comment.

1 Answer

Answer by Walter Roberson
on 30 Sep 2013
 Accepted Answer

I would advise you not to attempt to use fprintf() for communicating with that device. fprintf() has the undocumented limitation that if it encounters a null byte (value 0) in the string, it will consider the string to have finished and will not send anything beyond that point. There are ways to get around that but they are a pain, and the alternative is not difficult.

The sample command shown could be coded as

fwrite( fid, uint8([16 0 250]) );


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!