How can I connect MATLAB with MODBUS RTU gas stream analyzer?

3 ビュー (過去 30 日間)
Matthew Inoue
Matthew Inoue 2016 年 2 月 19 日
回答済み: Camilo Cárdenas 2022 年 5 月 16 日
Goal: To send the following message to a MODBUS RTU-enabled X-stream gas analyzer using 16-bit low bit first, using a test message and response to retrieve device serial number (Address 3141-3147, String Output) over RS-232 serial communication port, 16-bit (Low-byte, High-byte) format. The device is an Emerson Process Management XSTREAM X2GP Gas Stream Analyzer that accepts RTU format and either 32-bit Daniel mode or 16-big High or Low (is this same as high byte first or low byte first?).
[Address, Mode, 2xAddress, 2xCoilCount , 2x CRC]
Where:
[ 01, 03, 3141,0007,CRC]
Should I use:
[01 03 49 65 00 07 90 224]
Or
[01 03 31 41 00 07 210 20] ?
Since for the test file, I want to retrieve the serial number, I expect 7 coils to be returned (3141-3147) correct?
,
Then fwrite(s,messagevector) does not find a response from fread(s, 2) within specified timeout. The following connection setup was used in MATLAB to conform with device settings:
opt.serial=COM1;
s=serial(opt.serial);
set(s,BaudRate,9600,DataBits,8,StopBits,1,Parity,even,Timeout,3);
set(s,readasyncmode,continous);
3141 is given as the address code per documentation
Append CRC Code:
N = length(message);
crc = hex2dec('ffff');
polynomial = hex2dec('a001');
for i = 1:N
crc = bitxor(crc,message(i));
for j = 1:8
if bitand(crc,1)
crc = bitshift(crc,-1);
crc = bitxor(crc,polynomial);
else
crc = bitshift(crc,-1);
end
end
end
lowByte = bitand(crc,hex2dec('ff'));
highByte = bitshift(bitand(crc,hex2dec('ff00')),-8);
amsg = message;
amsg(N+1) = lowByte;
amsg(N+2) = highByte;

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 2 月 19 日
My suspicion would be that you should use
[01, 03, typecast(uint16([3141, 7]),'uint8')]
followed by the CRC.
This relies upon the fact that in all currently supported MATLAB versions, the architecture is Intel x86 or x64, both of which are "little-endian", which is the same order that the destination wants.
In decimal this would be [01, 03, 69, 12, 07, 00] followed by the CRC

Matthew Inoue
Matthew Inoue 2016 年 2 月 22 日
編集済み: Walter Roberson 2016 年 2 月 24 日
I tried to implement the code that you suggested using
appendcrc([01, 03, typecast(uint16([3141, 7]),'uint8')])
However, MATLAB returned an error in appendcrc using the bitxor (double mixed with integers must have integer values) which I corrected (maybe?) using:
appendcrc(double([01, 03, typecast(uint16([3141, 7]),'uint8')]))
Unfortunately, no message was received. One of my concerns with the existing code is the device is setup for both 32-bit "Daniel" mode and 16-bit Low and High byte first modes (9001 register vs. 6001-6002, for instance). The message sent 16 bits so I'm not quite clear on if my message is correct.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 2 月 24 日
I will try to return to this when my eyes are more rested.

サインインしてコメントする。


Camilo Cárdenas
Camilo Cárdenas 2022 年 5 月 16 日
Hello,
I am trying to carry out a such communication with a x-stream XEGK-Analyzator.
Therefore I would like to ask you if you have reached it and if you ca share some tips and or codes?
regards!

カテゴリ

Help Center および File ExchangeModbus Communication についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by