Hi everyone,
Happy New year to all. Hope everyone are doing good.As said in the subject line, I'm trying to read Discrete Input status from MODBUS TCP device using Arduino and W5500 module. I done few work around and found some example program for reading the data. I'm attaching the code I used for testing. The digital input status is not been updated in serial monitor.
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
byte ethMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Replace with your Ethernet shield's MAC address
IPAddress ethFixedIp(172, 18, 100, 250); // Set your Ethernet shield's desired fixed IP address
byte modbusMac[] = { 0x00, 0x60, 0xE9, 0x2A, 0x4E, 0x4C }; // Replace with your Modbus device's MAC address
IPAddress modbusIp(172, 18, 100, 242); // Set your Modbus device's IP address
EthernetClient client;
ModbusIP mb;
const int SWITCH_ISTS = 100001; // Replace with your specific Modbus address
void setup() {
Ethernet.init(29); // Most Arduino shields
Serial.begin(9600);
while (!Serial) {
; // Wait for the serial port to connect
}
Serial.println("Initialize Ethernet with fixed IP:");
Ethernet.begin(ethMac, ethFixedIp);
Serial.print(" Fixed IP address: ");
Serial.println(ethFixedIp);
// Print MAC address
Serial.print(" MAC address: ");
uint8_t macAddress[6];
Ethernet.MACAddress(macAddress);
for (int i = 0; i < 6; i++) {
Serial.print(macAddress[i], HEX);
if (i < 5) Serial.print(":");
}
Serial.println();
// Modbus configuration
mb.config(modbusMac, modbusIp);
mb.addIsts(SWITCH_ISTS);
}
void loop() {
mb.task();
int switchState = mb.Ists(SWITCH_ISTS);
Displayvalue(switchState);
// Your Modbus communication code goes here using Modbus.h library
delay(1000);
}
void Displayvalue(int switchState) {
String Printstr = "Discrete Input state is: " + String(switchState);
Serial.println(Printstr);
}
I tested the device with Qmodmaster software and its working fine. Attaching the screenshot of the same. But I'm not able to read in arduino using W5500 module.
Can anyone tell me what's wrong in this code. Any help on this will be greatly appreciated. Thank you in advance
