MATLAB: Unexpected Results When Interacting with the ATI Axia80-M20 using the Serial Interface

atiaxia80-m20Instrument Control Toolboxserial

I am using the ATI Axia80-M20 force/torque sensor. It uses the rs-485 serial protocol and I'm having problem reading data using the "serial" command in MATLAB. It works fine when I am connecting with the same configuration using PuTTY.
Below is the code I am using:
FT = serial('COM18','BaudRate',115200);
FT.Terminator = 'LF';
fopen(FT);
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 the instrument command "r" from "fgets" we are looking for is a character string in hexidecimal format. I expected something like this:
'200000000FFFFFFFFFFFF0000'
But now I only get one of the three things below returned:
' '
' Command not found '
' ýýüþóëæØÐÇFFFF0000âþóØóëæ '

Best Answer

The instrument's serial communication interface was mainly intended for terminal device applications, where commands (and end-of-line characters) are manually typed one letter at a time, whereas MATLAB serial write functions send all characters in quick succession. A custom fuction writing one character at a time with a pause between characters is needed.
The instrument manual describes only the terminal device communication workflow using PuTTY.
Below are some properties of this particular device that we have observed:
  1. The instrument takes "CR/LF" for read terminator and "CR" for write, instead of "LF".
  2. The instrument cannot take multiple characters at once, it requires pauses between characters.
  3. The instrument echoes back the command characters received.
  4. The instrument does not appear to have consistent behavior, for the same command, sometimes it works, sometimes it returns "command not found"
  5. The output is spread over multiple lines, so you have to do fscanf several times to get the whole output.
From the properties above, the intended use of serial communication of this device is for terminal interaction, where commands and line terminators are typed in one letter at a time.
To correctly use this device, please first connect to the device using the "serial" object as below:
FT = serial('COM18','BaudRate',115200);
FT.Terminator = {'CR/LF','CR'};
fopen(FT)
Then define a function that takes a command string as argument and sends one character at a time to the device.
Since we are sending one character at a time, we use fwrite instead of fprintf.
function writeCommand(obj, cmd)
for i = 1:numel(cmd)
fwrite(obj, cmd(i))
pause(0.25)
end
fwrite(obj, 13)
end
Then use the function defined above to send command to the device and read with fscanf. Note that as mentioned above, the output might spread over multiple lines, so you have to scanf multiple times to read the whole output:
% command to get into "Robot Mode"
% According to section 6.1 on the manual in the link below, first you want to
% make sure you enter "Robot Mode" with instrument command "m" before collecting
% data with instrument command "r".
writeCommand(FT, 'm')
pause(0.5)
% read whole output, you can also define this as a function
while (FT.BytesAvailable > 0)
  fscanf(FT)
end
% command for reading data
writeCommand(FT, 'r')
pause(0.5)
while (FT.BytesAvailable) > 0
fscanf(FT)
end
% you can also send other commands that are mentioned in the manual
% writeCommand(FT, 'h')
Close the connection after use:
fclose(FT)
delete(FT)