arduino-libraries / ArduinoModbus

250 stars 118 forks source link

Library only working for 1 simultaneous Server on RP2040 using earlephilhower's arduino-pico platform #155

Open pomplesiegel opened 2 weeks ago

pomplesiegel commented 2 weeks ago

Hello! Strange issue for you all:

I have been running code successfully on a Adafruit Feather RP2040 with PlatformIO's standard Raspberry Pi pico platform. However, once I changed over to using earlephilhower's arudino-pico on the same exact board, a strange behavior began:

Everything seems totally fine when testing 1 ModbusRTUServer physical device at a time. However, if the same code is run on two slaves connected on the same bus (for example, with modbus address/id 1 and 2, respectively) I can only read from 1 device. The requests from the second device will time out unless I wait a few seconds to issue the new request.

Here's an illustration of the setup

  1. Adafruit Feather RP2040 connected via Modbus RTU RS485 - (address 1)
  2. Adafruit Feather RP2040 connected via Modbus RTU RS485 - (address 2)
  3. Computer w/ RS485 to USB adapter running python, polling both devices for their holding register values

Result

If I just poll one device at a time, the responses happen within a few milliseconds, and I can poll constantly for days without a comms issue. It is only when two devices are introduced that the issue occurs.

Any thoughts on what could be going on here? Thank you!

MVP main.cpp example code for Adafruit Feather RP2040:

#include <Arduino.h>
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

const int MODBUS_ADDRESS = 1; //change between 1 and 2, per simultaneous device

void setup() 
{
  Serial.begin(115200);

  //slave id 1
  if ( !ModbusRTUServer.begin(MODBUS_ADDRESS,115200,SERIAL_8N1) ) 
  {
    Serial.println("Failed to start Modbus RTU Server!");
    while (1);
  }

  // configure 50 holding registers, starting at address 1
  ModbusRTUServer.configureHoldingRegisters(1,50);
}

void loop() 
{
  // poll for Modbus RTU requests
  if (ModbusRTUServer.poll() )
  {
    Serial.println("Received request!");
  }
} 

platform.ini file for PlatformIO compilation:

[env:adafruit-rp2040]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = adafruit_feather
framework = arduino
board_build.core = earlephilhower

#Selectively uncomment block below for latest tested library version
lib_deps =
    https://github.com/arduino-libraries/ArduinoModbus.git

FYI: For RS485.h to compile I had to make a couple #define and pinmapping changes, which may be necessary depending on your pinout as well for testing

pomplesiegel commented 2 weeks ago

I'm wondering if this could somehow be an issue of a pin being left low/high, hogging the bus? Weird that it eventually resets. Any ideas of some things to try, and can anyone else try this as well to replicate?

pomplesiegel commented 1 week ago

Hi @martinez20m and @aentinger, hope you're both doing well! Based on your past experience with this library, any idea what could be going on here to lead to the differing behavior between platforms on the same exact hardware?

pomplesiegel commented 1 week ago

Hello! FYI, I switched to this library instead (below) and for me it is working much more reliably for the RP2040 on this platform. Thank you!

https://github.com/CMB27/ModbusRTUSlave