ModBus协议-功能码-0x01



01 (0x01) Read Coils
This function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The Request PDU specifies the starting address, i.e. the address of the first coil specified, and the number of coils. In the PDU Coils are addressed starting at zero. Therefore coils numbered 1-16 are addressed as 0-15. The coils in the response message are packed as one coil per bit of the data field. Status is indicated as 1= ON and 0= OFF. The LSB of the first data byte contains the output addressed in the query. The other coils follow toward the high order end of this byte, and from low order
to high order in subsequent bytes. If the returned output quantity is not a multiple of eight, the remaining bits in the final data byte will be padded with zeros (toward the high order end of the byte). The Byte Count field specifies the quantity of complete bytes of data.

ModBus协议-功能码-0x01_第1张图片

The status of outputs 27–20 is shown as the byte value CD hex, or binary 1100 1101. Output 27 is the MSB of this byte, and output 20 is the LSB. By convention, bits within a byte are shown with the MSB to the left, and the LSB to the right. Thus the outputs in the first byte are ‘27 through 20’, from left to right. The next byte has outputs ‘35 through 28’, left to right. As the bits are transmitted serially, they flow from LSB to MSB: 20 . . . 27, 28 . . . 35, and so on. In the last data byte, the status of outputs 38-36 is shown as the byte value 05 hex, or binary 0000 0101. Output 38 is in the sixth bit position from the left, and output 36 is the LSB of this byte. The five remaining high order bits are zero filled

分析如下:

Tx:01 01 00 13 00 13
Rx:01 01 03 CD 6B 05

0x13 = 19

[20,38]

[20,27]
[28,35]
[36,38]

[27,20] 27:MSB 20:LSB  0000 0000
[35,28] 35:MSB 28:LSB
[38,36] 38:MSB 36:LSB

串行发射比特时,从LSB向MSB传输: 20...27、 28...35 等等
38是左侧第六个比特位置,输出36是这个字节的LSB。用零填充五个剩余高位比特。
1= ON 和 0= OF


ModBus协议-功能码-0x01_第2张图片


MSB: (Most Significant Bit) 最高有效位,与十进制数字中最左边的一位类似,通常,MSB位于二进制数的最左侧,LSB位于二进制数的最右侧。

LSB:( least significant bit最低有效位,其最低有效位就是拥有最小单位数值的那一位





你可能感兴趣的:(ModBus协议)