How can I read hexidecimal data via rs485?

17 views (last 30 days)
I need to read force/torque data from a sensor that uses rs-485 to communicate. So far I've been able to get pretty close but the readings are inconsistent or just nonexistent.
Right now my code looks like this:
------------------------------------------------
% Establish serial connection
FT = serial('COM18','BaudRate',115200);
fopen(FT);
FT.terminator = 'CR/LF';
fprintf(FT,'r'); % serial command that reads instantaneous force/torque data
fgets(FT); % prints force/torque readings in hexidecimal format
fclose(FT);
delete(FT);
-------------------------------------------------
The output for fgets() should look like this:
ans =
200000000FFFFFFFFFFFF0000 % The first digit is a counter. Each 4 characters after that are 4 digit hex readings for %Fx,Fy,Fz,Tx,Ty,Tz which I will convert later.
But right now I'm getting 1 of 3 things when I run it. These will be received in no particular order when running the code multiple times in a row.
  1. ans = ' '
  2. ans = ' Command not found ' % This is the error the sensor returns when an unknown command is sent.
  3. ans = ' 000100010005FFFF00000000 ' % This is close to what I should be getting except for the first 2 characters not showing up.
Is this because I'm using rs-485? I read online that using serialport() will support rs-485 but reading/writing data is limited to certain data types, none of which are hexidecimal.
  2 Comments
James Tursa
James Tursa on 17 Nov 2020
Are you sure you are not simply getting integer values that you can interpret as hexidecimal numbers?
Joey Menicucci
Joey Menicucci on 17 Nov 2020
Yeah, the answer that I mentioned I should be getting was right out of the manual for the sensor. It's in hex format and there's steps they give to convert that into decimal. Just weird that the first two characters come up as unknown.
But the thing that is throwing me off the most is that I'm not getting the same result every time...

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!