Modbus value error after using two software serial ports

I use Arduino Nano.

I use two software serial.
I tried to read the modbus value and found that the value is not 0 and cannot be written (it will be overwritten after writing).
The values keep changing all the time.

I don't know what went wrong. Please help me.

This is my code.

#include <ArduinoModbus.h>
#include <SoftwareSerial.h>
SoftwareSerial serial1(2, 3);
SoftwareSerial serial2(7, 8);
const int numCoils = 10;
const int numDiscreteInputs = 10;
const int numHoldingRegisters = 10;
const int numInputRegisters = 10;


void setup() {
  // put your setup code here, to run once:

 serial2.begin(57600);
  while (!serial2)
    ;
 serial2.println("Software Serial 2 OK!");

  serial1.begin(57600);
  while (!serial1)
    ;
  serial2.println("Software Serial 1 OK!");

  Serial.begin(9600);
  while (!Serial)
    ;
  serial2.println("Hardware Serial OK!");

  if (!ModbusRTUServer.begin(1, 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() {
  // put your main code here, to run repeatedly:
  ModbusRTUServer.poll();
}

I wouldn't do that. Better use a controller with enough hardware Serials like Arduino Mega.

Well, this is a solution

Have you tried including the RS485 libraries?

#include <ArduinoRS485.h>

Actually, #include <ArduinoModbus.h> already calls #include <ArduinoRS485.h>

So I don't need to call the header file repeatedly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.