Lavanya rajan
Well-known member
Dear Team,
I'm trying to read Digital input status from IO Module that transmits data using MODBUS TCP/IP protocol. I used Qmodmaster software to read the data and its working fine. Screenshot attached for the same.
Now I'm trying to read the same digital input using Teensy 4.0 controller along with w5500 (https://protosupplies.com/product/ethernet-module-w5500/)Ethernet module. I understand that Modbus TCP/IP uses port 502 but this module required port no of 1502.
I used this code, but the output is always 1 though digital input status changes. Can any body help me on this
I'm trying to read Digital input status from IO Module that transmits data using MODBUS TCP/IP protocol. I used Qmodmaster software to read the data and its working fine. Screenshot attached for the same.
Now I'm trying to read the same digital input using Teensy 4.0 controller along with w5500 (https://protosupplies.com/product/ethernet-module-w5500/)Ethernet module. I understand that Modbus TCP/IP uses port 502 but this module required port no of 1502.
I used this code, but the output is always 1 though digital input status changes. Can any body help me on this
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
ModbusIP mb;
const int switchPin = 0; // Change this to the actual pin you have connected the switch to
const int SWITCH_ISTS = 0;
const int PORT = 1502;
const int SLAVE_ID = 1;
void setup() {
Serial.begin(9600);
Ethernet.init(29);
byte mac[] = { 0x00, 0x60, 0xE9, 0x2A, 0x4E, 0x4C };
byte ip[] = { 172, 18, 100, 242 }; // Change this to your Arduino's IP address
Ethernet.begin(mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet module was not found - Stopping Test");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
mb.config(mac, ip);
mb.addIsts(SWITCH_ISTS);
}
void loop() {
mb.task();
mb.Ists(SWITCH_ISTS, digitalRead(switchPin));
Displayvalue();
delay(1000);
}
void Displayvalue() {
String Printstr = "Switch state is: " + String(mb.Ists(SWITCH_ISTS));
Serial.println(Printstr);
}