Hi - Can anyone please help
I've used arduino a few times, including Modbus RTU Master a few years ago, but i've got to a dead end using the ArduinoModbus.h for TCP today. Code below - I'm using QModMaster (QModMaster download | SourceForge.net) as my Master on the laptop and the arduino with ethernet shield as the slave.
Problem is, when i get the data back from the Arduino, the values are changing every few cycles of polling them - removing Serial.Begin seemed to fix it for a while, but the gremlin returns - the Coils seem to be OK for now, but the Holding Registers soon get corrupted. Any ideas?? Data capture from QModMaster attached - you can see the values returned changing.. MAny Thanks, Tim
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
IPAddress myAddress(192, 168, 1, 222); // Assign Anything other than the PLC IP Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress gateway(192, 168, 1, 254); //
ModbusTCPServer modbusTCPServer;
EthernetServer EthServer(502);
int id=1; //set the Modbus ID
bool MODsuccess=0; //to check if the server has started
bool ETHsuccess=0; //to check if the server has started
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH); // To disable slave select for SD card; depricated.
Ethernet.begin(mac, myAddress, gateway, gateway);
modbusTCPServer.begin(id); //Start the server
delay(2000); // To provide sufficient time to initialize.
modbusTCPServer.configureCoils(0x00, 10); // configure a single coil at address 0x00
modbusTCPServer.configureHoldingRegisters(0x10, 10); // configure a single holding reg at address 0x00
modbusTCPServer.holdingRegisterWrite(0x10, 43); //sample write to
//Serial.begin(9600);//Get rid of this if there are data errors
}
void loop(){
EthernetClient client = EthServer.available();
modbusTCPServer.accept(client); // let the Modbus TCP accept the connection
modbusTCPServer.poll();
}
Raw Data transfer log.txt (6.39 KB)