How can I read hexidecimal data via rs485?

9 次查看(过去 30 天)
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 个评论
James Tursa
James Tursa 2020-11-17
Are you sure you are not simply getting integer values that you can interpret as hexidecimal numbers?
Joey Menicucci
Joey Menicucci 2020-11-17
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...

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by