Search code examples
arduinomodbusnrf52

Unable to Communicate with SEN0491 Laser Ranging Sensor via Modbus using Seeed Xiao nRF52840 and ModbusMaster Library


I am facing difficulties while trying to communicate with a SEN0491 Laser Ranging Sensor from DFRobot (Product Page) via Modbus. I am using a Seeed Xiao nRF52840 for the task and the ModbusMaster library to facilitate communication.

My Setup:

Seeed Xiao nRF52840 connected to SEN0491 Laser Ranging Sensor via Serial1. The TX RX lines of the laser are connected to the TX RX lines on the nRF board. Utilizing the ModbusMaster library for Arduino. Have tried different baud rates: 9600, 19200, 38400, 115200 Unsure about the slave device's Modbus address, but have tried 0,1,2.

Code:

#include <ModbusMaster.h>
#include <Adafruit_TinyUSB.h>

ModbusMaster node;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }
  Serial.println("Starting Modbus Master test");

  Serial1.begin(115200);  // Initialize Serial1 for Seeed Xiao nRF52840
  node.begin(2, Serial1);
}

void loop() {
  uint8_t result = node.writeSingleRegister(0, 0x1234);
  if (result != node.ku8MBSuccess) {
    Serial.println("Write failed!");
  }

  result = node.readHoldingRegisters(0, 1);
  if (result != node.ku8MBSuccess) {
    Serial.println("Read failed!");
    Serial.print("Modbus error code: ");
    Serial.println(result);
  }

  delay(500);
}

Problems: I keep receiving failure messages ("Write failed!" and "Read failed!") along with the error code 224. I am uncertain about the slave device's Modbus address and baud rate and have tried scanning through common addresses and rates without success.

If anyone has faced similar issues or has any suggestions for debugging this, I would be highly appreciative.

The modbus command I ideally want to send is the "system recovery" command which is MODADDR 06 00 00 00 01 CRCH CRCL.

When I use a simple Serial1.read() I am able to read the characters the laser is printing in ascii, but I need to modify the settings of the laser, which is why I need to use modbus.


Solution

  • According to the documentation for the library, error 224 (0xE0) means there is no device on your Modbus network to match the addresses you are trying (0, 1 and 2).

    And if you are to believe this forum post:

    The module address is 0x50 (default)

    That seems to be consistent. So try changing MODADDR in your queries to 0x50.

    Lastly, looking at the comments here, the default baud rate should be 115200.