I assume by MBHAP you mean the header?
From Modbus_Application_Protocol_V1_1b.pdf I get:
MBAP = 7 bytes, for TCP that leaves 253 bytes for PDU, giving total of 260 bytes for ADU.
The mb_rsp_pdu is defined as: (response from slave)
mb_rsp_pdu = {function_code, response_data}, where
function_code = [1 byte] MODBUS function code
response_data = [n bytes] This field is function code dependent and usually
contains information such as variable references,
variable counts, data offsets, sub-function codes, etc.
And then further in the document: (for function code 3)
Response
Function code 1 Byte 0x03
Byte count 1 Byte 2 x N*
Register value N* x 2 Bytes
*N = Quantity of Registers
Now byte count I am assuming is 2xN (qty of registers) since each register is 2 bytes long (16bits), but then that means only the first 2 bytes are used for letting me know how much data is following, i.e. byte #2, but previously it says 7 bytes? And that is why I just do a full readAll() call, even though not best way.
When I use (demo) "Simply Modbus TCP", it shows the registers I want are in numbers 40017 & 40018, with an offset of 40001, so this then means I want registers 16 & 17?
So I set up my data structure as :
block [0] = 0x00; // unique transaction identifier HI BYTE
block [1] = 0x01; // unique transaction identifier LO BYTE
block [2] = 0x00; // protocol identifier HI
block [3] = 0x00; // protocol identifier LO
block [4] = 0x00; // message length i.e. 6 bytes to follow HI
block [5] = 0x06; // message length i.e. 6 bytes to follow LO
block [6] = 0x01; // unit identifier
block [7] = 0x03; // the function code
block [8] = 0x00; // HI byte
block [9] = 0x10; // Start from register mem addr 16
block [10] = 0x00; // Hi byte
block [11] = 0x02; // Read in 2 registers
block [0] = 0x00; // unique transaction identifier HI BYTE
block [1] = 0x01; // unique transaction identifier LO BYTE
block [2] = 0x00; // protocol identifier HI
block [3] = 0x00; // protocol identifier LO
block [4] = 0x00; // message length i.e. 6 bytes to follow HI
block [5] = 0x06; // message length i.e. 6 bytes to follow LO
block [6] = 0x01; // unit identifier
block [7] = 0x03; // the function code
block [8] = 0x00; // HI byte
block [9] = 0x10; // Start from register mem addr 16
block [10] = 0x00; // Hi byte
block [11] = 0x02; // Read in 2 registers
To copy to clipboard, switch view to plain text mode
And the response I get is
block [0] = 0x00;
block [1] = 0x01;
block [2] = 0x00;
block [3] = 0x00;
block [4] = 0x00;
block [5] = 0x07;
block [6] = 0x01;
block [7] = 0x03;
block [8] = 0x04;
block [9] = 0x3D;
block [10] = 0x7E;
block [11] = 0xBF;
block [12] = 0x26;
block [0] = 0x00;
block [1] = 0x01;
block [2] = 0x00;
block [3] = 0x00;
block [4] = 0x00;
block [5] = 0x07;
block [6] = 0x01;
block [7] = 0x03;
block [8] = 0x04;
block [9] = 0x3D;
block [10] = 0x7E;
block [11] = 0xBF;
block [12] = 0x26;
To copy to clipboard, switch view to plain text mode
So it is reading the correct registers, yet 13 bytes are returned, 4 making up the data, block[8] I take it tells me how many bytes are following, [7] is just a return of the function code, remainder back to 0 as in request, except for [5] that (I assume?) again tells me how many bytes are still to follow, i.e. 7, plust the 6 already given ([0]-[6]) = 13.
So I am still a bit confused as to its makeup. block [9] to [12] is the data I want, basically a float point number as single-precision, which is fine to convert.
Possibly one more question, if you aren't already tired of my ignorance, when I do a
qDebug
() <<
QString::number(block
[9],
16);
qDebug
() <<
QString::number(block
[10],
16);
qDebug
() <<
QString::number(block
[11],
16);
qDebug
() <<
QString::number(block
[12],
16);
qDebug() << QString::number(block[9],16);
qDebug() << QString::number(block[10],16);
qDebug() << QString::number(block[11],16);
qDebug() << QString::number(block[12],16);
To copy to clipboard, switch view to plain text mode
it prints out
"3d"
"7e"
"ffffffffffffffbf"
"26"
Why does the 3rd value come out so strange, yet in the debugging (watchpoints) it displays as "bf"?
Again, thanks for your insight. I appreciate your time and help.
Shaun
Bookmarks