测试硬件:arduino nano
arduino mega2560 pro
Arduino版本1.8.13
ArduinoModbus库版本:1.0.3
首先安装ArduinoModbus库
还有一个依赖库
例程
/*
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);
}
}
nano编译后内存占用大致50%
mega2560 pro编译后5%
注意要配合TTL转485进行测试(直接usb串口测试不成功)
必须使用硬件串口Serial