eModbus / eModbus

Modbus library for RTU, ASCII and TCP protocols. Primarily developed on and for ESP32 MCUs.

Home Page:https://emodbus.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation and Implementation Discrepancy for Function Codes FC10 and FC0F

bigemfat opened this issue · comments

Hello,

It appears that there might be a discrepancy between the documentation and implementation regarding the handling of Function Codes FC10 and FC0F in the eModbus library. The issue arises from the definitions in ModbusTypeDefs.h and the corresponding functions in ModbusMessage.h.

In ModbusTypeDefs.h, the FCType enum specifies the parameter types associated with each Function Code:

enum FCType : uint8_t {
  FC01_TYPE,         // Two uint16_t parameters (FC 0x01, 0x02, 0x03, 0x04, 0x05, 0x06)
  FC07_TYPE,         // no additional parameter (FCs 0x07, 0x0b, 0x0c, 0x11)
  FC0F_TYPE,         // two uint16_t parameters, a uint8_t length byte and a uint16_t* pointer to array of bytes (FC 0x0f)
  FC10_TYPE,         // two uint16_t parameters, a uint8_t length byte and a uint8_t* pointer to array of words (FC 0x10)
  FC16_TYPE,         // three uint16_t parameters (FC 0x16)
  FC18_TYPE,         // one uint16_t parameter (FC 0x18)
  FCGENERIC,         // for FCs not yet explicitly coded (or too complex)
  FCUSER,            // No checks except the server ID
  FCILLEGAL,         // not allowed function codes
};

According to this enumeration, FC0F should utilize a uint16_t* pointer to an array of bytes, while FC10 should use a uint8_t* pointer to an array of words.

However, when examining the definitions of ModbusMessage::checkData() in ModbusMessage.h, there seems to be a mismatch in the array parameter types:

  // 5. two uint16_t parameters, a uint8_t length byte and a uint8_t* pointer to array of words (FC 0x10)
  static Error checkData(uint8_t serverID, uint8_t functionCode, uint16_t p1, uint16_t p2, uint8_t count, uint16_t *arrayOfWords);
  
  // 6. two uint16_t parameters, a uint8_t length byte and a uint16_t* pointer to array of bytes (FC 0x0f)
  static Error checkData(uint8_t serverID, uint8_t functionCode, uint16_t p1, uint16_t p2, uint8_t count, uint8_t *arrayOfBytes);

Thanks!
Emile

Thanks for hinting me to this. The code is correct, the comments with the type definitions are not. The two comments are swapped, as FC07 is requiring the byte array, whereas FC10 will requre a word array.

I will fix that eventually.