开发环境:arduino IDE 2.0.3
库测试,选用库自带的simple_slave例程
源码
/**
* Modbus slave example 1:
* The purpose of this example is to link a data array
* from the Arduino to an external device.
*
* Recommended Modbus Master: QModbus
* http://qmodbus.sourceforge.net/
*/
#include <ModbusRtu.h>
// data array for modbus network sharing
uint16_t au16data[16] = {
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus slave(1,Serial,0); // this is slave @1 and RS-232 or USB-FTDI
void setup() {
Serial.begin( 19200 ); // baud-rate at 19200
slave.start();
}
void loop() {
slave.poll( au16data, 16 );
}
编译下载后,使用软件连接测试:
Modbus Poll
连接成功后,鼠标右键
设置命令和显示
QModbus连接测试
RMMS连接测试