arduino-libraries / ArduinoModbus

252 stars 120 forks source link

Can not communicate with ArduinoMega2560 #33

Open hannasy-id opened 4 years ago

hannasy-id commented 4 years ago

I try this library to Arduino Mega 2560, but can not respond, and i try to arduino nano it success.

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

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:
  Serial.begin(9600);

  if (!ModbusRTUServer.begin(1, 9600)) {
    //Serial.println("Failed to start Modbus RTU Server!");
    while (1);
  }

  ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);

}

void loop() {
  ModbusRTUServer.poll();

  // put your main code here, to run repeatedly:
  for (int i = 0; i < numHoldingRegisters; i++) {
    ModbusRTUServer.holdingRegisterWrite(i, 12345); 
  }
}