Hi! First post here so I suppose I am a bit of a noob with Ignition. At least this is my first Modbus TCP communication with Ignition:
I want to send information of a flowmeter received through the digital input of my Arduino MEGA to Ignition through Modbus TCP protocol. I am using the ModbusIP.h library of Arduino, and I seem to successfully launch my Arduino as the server:
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
// ModbusIP object
ModbusIP mb;
// Ethernet settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 177 };
// Flowmeter settings
const int flowPin = 2; // Digital pin connected to flowmeter
volatile unsigned int pulseCount = 0;
unsigned long lastTime = 0; // Last time flow rate was calculated
float flowRate = 0.0; // Flow rate in liters/min
float calibrationFactor = 477; // Pulses per liter
// Modbus register address
const int flowRateRegister = 0; // Modbus address for flow rate data
// Interrupt service routine to count pulses
void countPulses() {
pulseCount++;
}
void setup() {
// Start serial monitor
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect (only needed for some boards)
}
// Initialize Ethernet and Modbus
Ethernet.begin(mac, ip);
mb.config(mac, ip);
mb.addHreg(flowRateRegister); // Add holding register for flow rate
// Print IP and Port
Serial.println(Ethernet.localIP());
// Initialize flowmeter input
pinMode(flowPin, INPUT_PULLUP); // Use internal pull-up resistor
attachInterrupt(digitalPinToInterrupt(flowPin), countPulses, FALLING);
// Initialize timing
lastTime = millis();
}
void loop() {
// Modbus handling
mb.task();
// Time calculation
unsigned long currentTime = millis();
unsigned long timeDiff = currentTime - lastTime;
// Every second, calculate flow rate
if (timeDiff >= 1000) {
float frequency = pulseCount / (timeDiff / 1000.0); // Pulses per second
flowRate = frequency / calibrationFactor; // Convert to liters/min
pulseCount = 0; // Reset pulse count
lastTime = currentTime;
// Debug output
Serial.println(flowRate, 2); // 2 decimal places
// Update Modbus register
mb.Hreg(flowRateRegister, (uint16_t)(flowRate * 100)); // Send scaled value
}
}
I say successfully because once launched, the ping to the created IP address answers, and NetCat connects to the 502 port.
Nonetheless, when configuring a simple Modbus TCP device into my Ignition I get Disconnected status. The gateway is also located at the same subnet as the Modbus server I create (0). So I really do not see what could be happening here.
Noteworthy: I am using MacOS, hence I sadly do not have access to ModbusPoll and I cannot check if I am properly writing my registers yet. But I suppose that Ignition should connect anyway.
I tried to be as clear as possible, I am glad to further clarify my problem if needed.