72 lines
1.9 KiB
Arduino
72 lines
1.9 KiB
Arduino
|
/*
|
||
|
Modbus RTU Server Kitchen Sink
|
||
|
|
||
|
This sketch creates a Modbus RTU Server and demonstrates
|
||
|
how to use various Modbus Server APIs.
|
||
|
|
||
|
Circuit:
|
||
|
- MKR board
|
||
|
- MKR 485 shield
|
||
|
- ISO GND connected to GND of the Modbus RTU server
|
||
|
- Y connected to A/Y of the Modbus RTU client
|
||
|
- Z connected to B/Z of the Modbus RTU client
|
||
|
- Jumper positions
|
||
|
- FULL set to OFF
|
||
|
- Z \/\/ Y set to OFF
|
||
|
|
||
|
created 18 July 2018
|
||
|
by Sandeep Mistry
|
||
|
*/
|
||
|
|
||
|
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
|
||
|
#include <ArduinoModbus.h>
|
||
|
|
||
|
const int numCoils = 10;
|
||
|
const int numDiscreteInputs = 10;
|
||
|
const int numHoldingRegisters = 10;
|
||
|
const int numInputRegisters = 10;
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
while (!Serial);
|
||
|
|
||
|
Serial.println("Modbus RTU Server Kitchen Sink");
|
||
|
|
||
|
// start the Modbus RTU server, with (slave) id 42
|
||
|
if (!ModbusRTUServer.begin(42, 9600)) {
|
||
|
Serial.println("Failed to start Modbus RTU Server!");
|
||
|
while (1);
|
||
|
}
|
||
|
|
||
|
// configure coils at address 0x00
|
||
|
ModbusRTUServer.configureCoils(0x00, numCoils);
|
||
|
|
||
|
// configure discrete inputs at address 0x00
|
||
|
ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);
|
||
|
|
||
|
// configure holding registers at address 0x00
|
||
|
ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
|
||
|
|
||
|
// configure input registers at address 0x00
|
||
|
ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
// poll for Modbus RTU requests
|
||
|
ModbusRTUServer.poll();
|
||
|
|
||
|
// map the coil values to the discrete input values
|
||
|
for (int i = 0; i < numCoils; i++) {
|
||
|
int coilValue = ModbusRTUServer.coilRead(i);
|
||
|
|
||
|
ModbusRTUServer.discreteInputWrite(i, coilValue);
|
||
|
}
|
||
|
|
||
|
// map the holding register values to the input register values
|
||
|
for (int i = 0; i < numHoldingRegisters; i++) {
|
||
|
long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);
|
||
|
|
||
|
ModbusRTUServer.inputRegisterWrite(i, holdingRegisterValue);
|
||
|
}
|
||
|
}
|