MATLAB Answers


Communicating with a serial port (RS232) without a driver

Asked by Stuart
on 27 Sep 2013
Latest activity Answered by Stuart
on 27 Nov 2013

I'm trying to write an interface for a TE Tech thermocouple and temperature controller for our lab. While I'm an intermediate user of Matlab I've never had to write an instrument driver before. I've already written half the code to control and read from our spectrometer and written the control GUI.

It's connected via an RS232 port to the computer. I can connect to the serial object, but when I try to send it the commands that the manufacturer supplies to query the thermocouple, I don't get anything back. All I can do is open it, see what settings it has (can't change any) and close it again.

My question is, do I need to write it a driver or can I treat it as a serial object directly in the interface code? I need to send it strings such as *000400000000041(etx) to communicate with it

I've trolled the forums the last few days but haven't come up with anything that works. Any suggestions would be greatly appreciated!

  1 Comment

To check, that would be ['*000400000000041' char(3)] ? How exactly are you sending this? How have you configured the serial port?

Log in to comment.

6 Answers

Answer by Stuart
on 27 Nov 2013

For anyone trying to do this I managed to crack it without downloading anything extra:

I needed to change 2 things:

1 FlowControl was set to hardware for some reason, I had to explicitly change it to software

2 Weirdly, although I could see the serial port and communicate with it on hyperterminal ONLY over COM8 I HAD to use COM1 within Matlab to get a reply. This is a pain, as my matlab GUI I've written uses a spectrometer on COM1, so hopefully I don't get a hardware clash on the serial ports. Anyway, that's a separate issue.

Thanks for you help!


Log in to comment.

Answer by Stuart
on 27 Sep 2013

actually I wrote that wrong, it's


I've tried a wide range of commands to send this but I THINK whatever I'm doing isn't correct.

My serial port is configured as per the manufacturer's specs: Baud rate=9600, parity=none, flowcontrol=none, data bits=8, start/stop bits=1

I'm new to this, so I'm probably asking help with something simple.

Latest thing I've tried is to produce a generic driver in the matlab driver editor and use a device object to communicate with the controller, but that hasn't seemed to work either

Any advice?

  1 Comment

Code, please.

Is the stx to act as the terminator, or is there a newline or CR after it?

Please confirm that 00010000000041(stx) corresponds to ['*000400000000041' char(2)] and not (for example) ['' char([0 1 0 0 0 0 hex2dec('41') 2]) ]

Log in to comment.

Answer by Stuart
on 7 Oct 2013

sorry, I'm new to this type of coding. (stx) is a line terminator, just like the * is a line beginning indicator.

I've just heard back from the company who supplied the controller, they've sent me a labview driver that they use in-house. I'm hoping this will fix everything!


Log in to comment.

Answer by Stuart
on 10 Oct 2013

I'm now thinking that I'm not sending the correct signal to the serial port. I need to send it an ascii code, such as the one I showed; ie (stx)00010000000041(etx)

(stx) and (etx) are start/end text indicators

HOW do I actually accomplish this? What, for this example, would I send via fprintf (or something else?) to send this command?

(The drivers I got sent haven't helped)


fprintf(s, '%c00010000000041%c', 2, 3)

Thanks for the response! When I typed it in with the ported connected and opened, the reply I got was: "Error using serial/fprintf MODE must be either 'sync' and 'async' "

I changed it to fprintf(s, '%c00010000000041%c','async') and then 'sync' and all I get back is an echo.

Any tips? Feels like I'm getting closer to getting this working!

I don't know why it would be complaining about the mode, but try

fprintf(s, '%c00010000000041%c', [2, 3])

and if it still complains then

fprintf(s, '%c00010000000041%c', [2, 3], 'sync')

Log in to comment.

Answer by Stuart
on 19 Nov 2013

so it doesn't complain now but what I get is:

out =


Where before and after the number is a little square which I've never seen before. This is frustrating.

(thanks for your help)

  1 Comment

The little squares are appropriate. They are the representation of unprintable characters. The stx and etx are unprintable. If you were to take

out + 0

you would see

2 48 48 48 49 48 (etc)

where 48 is the character number corresponding to '0'

Log in to comment.

Answer by Stuart
on 19 Nov 2013

ah okay, that makes a LOT of sense, cheers.


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!