Skip to content

Return flag from poll() to allow conditional events to trigger #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions examples/RTU/ModbusRTUServerLED/ModbusRTUServerLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ void setup() {

void loop() {
// poll for Modbus RTU requests
ModbusRTUServer.poll();

// read the current value of the coil
int coilValue = ModbusRTUServer.coilRead(0x00);

if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coil value clear, turn LED off
digitalWrite(ledPin, LOW);
int packetReceived = ModbusRTUServer.poll();

if(packetReceived) {
// read the current value of the coil
int coilValue = ModbusRTUServer.coilRead(0x00);

if (coilValue) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coil value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}
}
4 changes: 3 additions & 1 deletion src/ModbusRTUServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ int ModbusRTUServerClass::begin(RS485Class& rs485, int id, unsigned long baudrat
return begin(id, baudrate, config);
}

void ModbusRTUServerClass::poll()
int ModbusRTUServerClass::poll()
{
uint8_t request[MODBUS_RTU_MAX_ADU_LENGTH];

int requestLength = modbus_receive(_mb, request);

if (requestLength > 0) {
modbus_reply(_mb, request, requestLength, &_mbMapping);
return 1;
}
return 0;
}

ModbusRTUServerClass ModbusRTUServer;
2 changes: 1 addition & 1 deletion src/ModbusRTUServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ModbusRTUServerClass : public ModbusServer {
/**
* Poll interface for requests
*/
virtual void poll();
virtual int poll();

private:
RS485Class* _rs485 = &RS485;
Expand Down
4 changes: 3 additions & 1 deletion src/ModbusServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ class ModbusServer {

/**
* Poll for requests
*
* @return 1 on request, 0 on no request.
*/
virtual void poll() = 0;
virtual int poll() = 0;

/**
* Stop the server
Expand Down
4 changes: 3 additions & 1 deletion src/ModbusTCPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void ModbusTCPServer::accept(Client& client)
}
}

void ModbusTCPServer::poll()
int ModbusTCPServer::poll()
{
if (_client != NULL) {
uint8_t request[MODBUS_TCP_MAX_ADU_LENGTH];
Expand All @@ -66,6 +66,8 @@ void ModbusTCPServer::poll()

if (requestLength > 0) {
modbus_reply(_mb, request, requestLength, &_mbMapping);
return 1;
}
}
return 0;
}
2 changes: 1 addition & 1 deletion src/ModbusTCPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ModbusTCPServer : public ModbusServer {
/**
* Poll accepted client for requests
*/
virtual void poll();
virtual int poll();

private:
Client* _client;
Expand Down