Incorrect data from fread over TCP

5 views (last 30 days)
Martin
Martin on 5 Jan 2014
Commented: Martin on 8 Jan 2014
I am right now trying to send one value of 55 from a wifi module that is acting as a server to matlab on a client computer. I can send the value to the module from the client and then resend it, I have checked all instances that the correct value is traveling throught the system. I can also see that the correct value returns into the client computer with the help of wireshark. BUT matlab does not display the correct value. I am lost at how to correct this and have now spent the last 8 hours trying to get it to work.
The code on the client:
tcpobj = tcpip('192.168.0.101',51000,'LocalPort',51000,'NetworkRole','client');
set(tcpobj, 'TimeOut', 10);
set(tcpobj, 'ByteOrder', 'littleEndian');
fopen(tcpobj);
fwrite(tcpobj,'C','char'); %Verification code with the wifi modul.
fwrite(tcpobj,55,'uint16') %Data to transport through the system(Client-AP-Wifi-Modul-Wifi-AP-Client).
fread(tcpobj,1,'uint16') %Data that has gone throught the system that should be 55.
Instead of 55 I receive 17207, but according to wireshark the data that reaches the client computer the data is 37 in hex and that corresponds to 55 in decimal form. How does 37 in hex become 17207 in decimal form? Am I missing a small detail here somewhere?
Another small question is why the following line does not work:
fread(tcpobj,1,'uint16',0,'ieee-be')
Only receive the following error: Too many input arguments.

Answers (1)

Walter Roberson
Walter Roberson on 5 Jan 2014
Edited: Walter Roberson on 5 Jan 2014
17207 is hex 0x4337 . 0x43 is decimal 67 which is 'C'. You wrote the 'C' and you did not remove it from the stream before trying to read the 16 bit version of decimal 37.
In
fread(tcpobj,1,'uint16',0,'ieee-be')
SKIP and MACHINEFORMAT are not supported for fread/serial
  3 Comments
Walter Roberson
Walter Roberson on 6 Jan 2014
fread(tcpobj, 1, 'uint8');
I have not worked with tcp objects in MATLAB. If you need to read everything at once then
indata = fread(tcpobj, 'uint8');
value_wanted = typecast(indata(2:3),'uint16');
Martin
Martin on 8 Jan 2014
Thanks once again Walter, I now got it up and running just like I want it! :)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!