Skip to content

Modbus RTU library Problem of time consuming when multiple slave on same bus #43

Open
@mktime

Description

@mktime

Hi

I had problem of time consuming of pool() method in my design using this library.

  • when the master is not polling any devices on modbus : 0 to 2ms maximum => Fine for me
  • when the master pool the arduino device : 36-37ms => fine for me
  • when the master pool another address :
ms: 7
ms: 500
ms: 7
ms: 500
ms: 8
ms: 500
ms: 8
ms: 500

So here there is a problem because it block my program for half a second when the master interrogate another device.
Do i need to configure something for to solve this ?
Is the library capable of doing 1 master with several slaves ? I supposed it was possible.

I used an Arduino Nano Every, but now I tried simple way :

  • Regular Arduino MEGA
  • example software "Modbus RTU Server Kitchen Sink" with just adding few lines to measure the time of the Pool() method and sent the time to Serial2 for logging purpose
  • Arduino mega is connected without RS485, direct on a RS232 TTL to a computer.
  • Arduino Master RTU is a Computer software used : QModMaster
    9600bauds 1stop bits, parity none
    Modbus address is 42.

When sending to device 42

[RTU]>Tx > 13:36:22:648 - 2A  03  00  00  00  01  82  11  
[RTU]>Rx > 13:36:22:673 - 2A  03  02  00  00  9C  42  

Everything fine, pool method using 15ms of time.

When sending to device 41 ( who do not exist )

[RTU]>Tx > 13:36:32:289 - 29  03  00  00  00  01  82  22  

Nobody reply, so normal but the pool() method on the arduino adr42 take 7ms and then 500ms.

So this 500ms block the rest of the program in the arduino.

So there is a problem in this library.
That's so strange that nobody had this issue before, it is so simple to encounter.

Hope someone can give me directions because.

/*
  Modbus RTU Server Kitchen Sink

  This sketch creates a Modbus RTU Server and demostrates
  how to use various Modbus Server APIs.

  Circuit:
   - MKR board
   - MKR 485 shield
     - ISO GND connected to GND of the Modbus RTU server
     - Y connected to A/Y of the Modbus RTU client
     - Z connected to B/Z of the Modbus RTU client
     - Jumper positions
       - FULL set to OFF
       - Z \/\/ Y set to OFF

  created 18 July 2018
  by Sandeep Mistry
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

unsigned int cycle;

const int numCoils = 10;
const int numDiscreteInputs = 10;
const int numHoldingRegisters = 10;
const int numInputRegisters = 10;

void setup() {  
  Serial.begin(9600);
  while (!Serial);
  
  Serial2.begin(115200);

  Serial2.println("Modbus RTU Server Kitchen Sink");

  // start the Modbus RTU server, with (slave) id 42
  if (!ModbusRTUServer.begin(42, 9600)) {
    Serial2.println("Failed to start Modbus RTU Server!");
    while (1);
  }

  // configure coils at address 0x00
  ModbusRTUServer.configureCoils(0x00, numCoils);

  // configure discrete inputs at address 0x00
  ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);

  // configure holding registers at address 0x00
  ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);

  // configure input registers at address 0x00
  ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
}

void loop() {
  cycle = millis();

  // Pool for modbus requests
  ModbusRTUServer.poll();

  cycle = millis() - cycle ;
  if ( cycle > 2 )
  {
    Serial2.print("ms: ");
    Serial2.println(cycle);
  }  

  // map the coil values to the discrete input values
  for (int i = 0; i < numCoils; i++) {
    int coilValue = ModbusRTUServer.coilRead(i);

    ModbusRTUServer.discreteInputWrite(i, coilValue);
  }

  // map the holiding register values to the input register values
  for (int i = 0; i < numHoldingRegisters; i++) {
    long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);

    ModbusRTUServer.inputRegisterWrite(i, holdingRegisterValue);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions