fscanf vs fread for reading individual bytes from an RS232 object

12 views (last 30 days)
Hi,
Recently I was trying to communicate with an instrument over RS232 to read some voltages from an ADC channel. With the following code
myData = fscanf(rs232DPCObject,'%c',6);
if (myData(2)*1 == DPC.ExtADA0ReadAddress)
if length(myData) == 6
integerData = (myData(3) + myData(4)*2^8 + myData(5)*2^16 + myData(6)*2^24);
docExtAD0Reading = [docExtAD0Reading typecast(uint32(integerData),'single')];
docExtAD0ReadingBinary = [docExtAD0ReadingBinary ; dec2bin(uint32(integerData),32)];
Most of the time, everything is fine and I read the voltage correctly. However, at some voltage levels some -significant- bits start to flip back and forth between 1 and 0, for example, at some fixed voltage level I read:
docAD0ReadingBinary =
0011111101 0 110010111110110100110
0011111101 0 110010111110110100110
0011111101 0 110011001001100011001
0011111101 1 110010001100011010000
0011111101 1 110010001100011010000
0011111101 0 110011011101000100011
0011111101 0 110011011000000001111
0011111101 0 110010111110110100110
0011111101 0 110010111110110100110
0011111101 1 110010001100011010000
0011111101 0 110011011000000001111
0011111101 0 110011011101000100011
0011111101 1 110010001100011010000
When I replaced
myData = fscanf(rs232DPCObject,'%c',6);
with:
[myData, count, msg] = fread(rs232DPCObject, 6, 'uint8');
The problem was resolved… It seems that the problem is related with fscanf function. Could you please provide an explanation for the source of this problem? Might there be a bug in fscanf or do I use it inappropriately?
Best regards,
Evrim Onur ARI
  1 Comment
dpb
dpb on 14 Jul 2014
I believe only knowing the details for the DUT transmission protocol would allow anybody to address the question. I suspect if it works reliably w/ fread but not w/ fscanf that it isn't actually ASCII and you're having trouble owing to that difference.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!