Failure to Obtain Serial Port Data

8 views (last 30 days)
Dillon Fairchild
Dillon Fairchild on 4 Aug 2014
Commented: Geoff Hayes on 6 Aug 2014
I have had issues with returning data from a serial device. The device is to return 40 bytes of data when sent the string "GR" with no terminator. The device was included with some hastily written example code in Visual Basic which sends the "GR" and does manage to return data, however in MATLAB we have been unable to make this happen. We connected another computer running putty through its serial port and were able to determine that both the VB program and our matlab script are sending the correct "GR", leading me to think that the problem is in how we are reading any returned data. The code we are using is similar to
if true
function efield();
s = serial('COM3');
fopen(s);
n = single('GR');
fwrite(s,n);
s.transferstatus
pause(0.025)
A = fread(s)
ret = s.bytesavailable
fclose(s)
delete(s)
clear('s')
end
although we have tried a lot of variations (including using fprintf and fscanf, which I am afraid will not work as the device is not set up to recognize any kind of termination character).
Baud rate, parity, stop bits, port all check out. I'm at a loss, since I have little experience with serial port communication in general, and don't really know where to go from here.
  7 Comments
Dillon Fairchild
Dillon Fairchild on 6 Aug 2014
You are likely correct, as I don't have much experience with serial port communication. However, trying my code with n = uint8('GR') still does not prompt any response. Further discussion with the designers led to them stressing that ONLY 'GR' should be sent; is it possible that any information other than 'GR' is being sent by this code?
Geoff Hayes
Geoff Hayes on 6 Aug 2014
I don't think that anything else should be sent by this code. It seems pretty straightforward and I don't think that there is anything mysterious going on behind the scenes.
A couple of things to check:
Are you sure that the device is ready to receive data? Once you have executed
s = serial('COM3');
fopen(s);
type
get(s,'PinStatus')
This will return a structure with output similar to
ans =
CarrierDetect: 'off'
ClearToSend: 'on'
DataSetReady: 'on'
RingIndicator: 'off'
It may not be the same, this is just what gets returned when I create a serial object. The important point is that the ClearToSend must be on.
-------------
Similar to above, when you type
get(s,'ByteOrder')
you will see littleEndian (given the results that you have written in a previous comment). Does the device expect littleEndian or bigEndian?
-------------
In your earliest post, you mentioned that The device was included with some hastily written example code in Visual Basic which sends the "GR" and does manage to return data.
Does this mean that you have the Visual Basic code? If so, how is it sending the the 'GR' command? What is the data type used, what is the byte order, etc.?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!