Modbus Discrete input read

/*
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);
;
}
}

Format and Put your code between code tags when using this forum !!

There are a couple of errors which will then show up , but not sure if they are important .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.