emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.
Other
520 stars 186 forks source link

Change byte order #120

Closed l3pond closed 3 years ago

l3pond commented 3 years ago

Hello!

After getting the transducer to work, I'm having a little issue that apparently is byte order of the response.

The request is:

    Request: [01] [03] [0F56] [0002] [270F]
             |    |    |      |      |-> CRC16 (3879)
             |    |    |      |-> Number Of Registers (2)
             |    |    |-> Register Offset (3926 = 43927)
             |    |-> Function Code (3)
             |-> Slave ID (1)

However, it's returning:

    Request: [03] [04] [C8] [DATA] [0304]
             |    |    |    |      |-> CRC16 (1027)
             |    |    |    |->  
             |    |    |-> Byte Count (-56)
             |    |-> Function Code (4)
             |-> Slave ID (3)

The code snippet where the request is made:

#define SLAVE_ID 1
#define FIRST_REG 3926
#define REG_COUNT 2

[...]

void loop() {
  uint16_t res[REG_COUNT];
  uint32_t current_time = millis();
  static uint32_t prev_time = 0;
  static uint32_t prev_task_time = 0;

  if(current_time - prev_time > 1000){
      prev_time = current_time;
      if (!mb.slave()) {    // Check if no transaction in progress
        mb.readHreg(SLAVE_ID, FIRST_REG, res, REG_COUNT, cb); // Send Read Hreg from Modbus Server
      }
  }
  if (current_time - prev_task_time > 5) { //5ms delay
      prev_task_time = current_time;
      mb.task();
  }
}

I had this problem on another project, but the SCADA software had the feature to change the byte order, as show on the image below. Is there any way to do it with this library or should I hard code it?

byte order

l3pond commented 3 years ago

Hello. Managed to fix it by using MODBUSRTU_DEBUG while parsing the responses. Didn't knew that when you're using the debug mode, it prints only data + crc, without the address. That's why things didn't made sense in first place.