/*
i am trying to read pins using modbus discrete input, the code is working but i have to send twice read request then i get the status.
*/
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
const int input1 = 10; // Define your input pin here
void setup() {
Serial.begin(9600);
if (!ModbusRTUServer.begin(1, 9600)) {
Serial.println("Failed to start Modbus RTU Server!");
while (1);
}
pinMode(input1, INPUT_PULLUP); // Set the input pin as INPUT with pull-up resistor
ModbusRTUServer.configureDiscreteInputs(0x00, 1);
}
void loop() {
int packetReceived = ModbusRTUServer.poll();
if(packetReceived) {
int input1Value = digitalRead(input1);
Serial.println(input1Value); // Add this line to print input1Value
ModbusRTUServer.discreteInputWrite(0x00, input1Value);
;
}
}