1、文档



2、相关问题


2.1、报文解析

  • 描述:

使用wireshark 抓包分析

  • 解决方案:

问题的解决方案

  1. 请求:
  2. 00 C4 00 00 00 06 03 03 00 00 00 04
  3. 事务处理标识 协议标识:tcp 后面的数据长度 设备地址 功能码 起始地址 寄存器数量4
  4. 响应
  5. 00 C4 00 00 00 0B 03 03 08 45 0F 4B AE 00 02 00 03
  6. 事务处理标识 协议标识:tcp 后面的数据长度 设备地址 功能码 报文长度 寄存器0的值 寄存器1的值 寄存器2的值 寄存器3的值

image.png
请求到相应的时间差15ms

2.2、字节数组转为浮点类型

1、data为四个字节的字节数组,类型:Float AB CD AB低位字节 CD高位字节
big-endian:大端,将高序字节存储在起始地址(高位编址)
将最低的字节移到最高位,所以索引0左移24位,1左移16位 依次类推

  1. Float.intBitsToFloat((data[offset] & 255) << 24 |
  2. (data[offset + 1] & 255) << 16 |
  3. (data[offset + 2] & 255) << 8 |
  4. data[offset + 3] & 255);

2、bytes为四个字节的字节数组,类型:Float CD AB AB低位字节 CD高位字节
little-endian by swap:小端交换,将低序字节存储在起始地址(低位编址)

  1. Float.intBitsToFloat(
  2. ((bytes[0] & 0xff) << 8) |
  3. (bytes[1] & 0xff) |
  4. ((bytes[2] & 0xff) << 24) |
  5. ((bytes[3] & 0xff) << 16));

3、bytes为四个字节的字节数组,类型:Float BA DC AB低位字节 CD高位字节
big-endian by swap:大端交换,将高序字节存储在起始地址(高位编址)

  1. public static float toFloatBigEndianBySwap(byte[] bytes) {
  2. return Float.intBitsToFloat(
  3. ((bytes[0] & 0xff) << 16) |
  4. ((bytes[1] & 0xff) << 24) |
  5. (bytes[2] & 0xff) |
  6. ((bytes[3] & 0xff) << 8));
  7. }

4、bytes为四个字节的字节数组,类型:Float DC BA AB低位字节 CD高位字节
little-endian :小端,将低序字节存储在起始地址(低位编址)

  1. public static float toFloatLittleEndian(byte[] bytes) {
  2. return Float.intBitsToFloat(
  3. (bytes[0] & 0xff) |
  4. (bytes[1] & 0xff) << 8 |
  5. (bytes[2] & 0xff) << 16 |
  6. (bytes[3] & 0xff) << 24);
  7. }