Skip to content

Commit 4807f71

Browse files
committed
Add sketch example of using SoftwareSerial on NANO RS485 industrial board
1 parent 88213a8 commit 4807f71

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

library/MultiModbus/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ sentence=Support multi-Modbus RTU communication via RS485 (RS485-to-UART ICs req
66
paragraph=MultiMobus supports multiple Modbus RTU servers and clients over RS485 on one board. RS485-to-UART ICs are required.
77
category=Communication
88
url=
9-
architectures=megaavr,samd,mbed_nano,mbed_portenta,mbed_opta
9+
architectures=*
1010
includes=MultiModbus.h
1111
depends=RS485
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Author: Kuan-Ting Lai
2+
#include <RS485.h> // https://github.com/RobTillaart/RS485
3+
#include <MultiModbus.h>
4+
#include <SoftwareSerial.h>
5+
6+
// This sketch uses Arduino NANO 485 industrial control board as an example.
7+
// You can find the board's information below:
8+
// https://www.aliexpress.com/item/1005003710057347.html
9+
// Pinout diagram:
10+
//https://ae01.alicdn.com/kf/Sdf186ca8d45649cd8bef91bf5d6d2f16T.jpg
11+
//
12+
// 中文相關訊息
13+
// https://www.ruten.com.tw/item/show?22023652957564
14+
// https://ruten-proteus.blogspot.com/2020/06/1try-vtscada-arduino-nano-485-modbus-rtu.html
15+
16+
#define RS485_BAUD_RATE 19200
17+
#define MODBUS_DEVICE_ID 1
18+
19+
const uint8_t sendPin = -1; // Our board has auto-direction
20+
21+
SoftwareSerial sw_serial(13, 12); // D13/D12 for RX/TX
22+
RS485 rs485(&sw_serial, sendPin);
23+
24+
ModbusRTUServer modbus_server(rs485);
25+
26+
27+
void setup() {
28+
Serial.begin(19200); // Debug
29+
//while (!Serial); // Avoid endless loop when running without USB COM port
30+
31+
sw_serial.begin(RS485_BAUD_RATE);
32+
33+
if (!modbus_server.begin(MODBUS_DEVICE_ID, RS485_BAUD_RATE)) {
34+
Serial.println("Failed to start Modbus RTU Server !");
35+
while (1);
36+
}
37+
38+
// configure a single coil at address 0x00, Modbus client will start to read/write from 0x01
39+
modbus_server.configureCoils(0x00, 1);
40+
41+
Serial.print("MultiModbus is ready! Device ID is ");
42+
Serial.println(MODBUS_DEVICE_ID);
43+
44+
}
45+
46+
47+
void loop() {
48+
int packetReceived;
49+
50+
// poll for Modbus RTU requests
51+
packetReceived = modbus_server.poll();
52+
if(packetReceived) {
53+
int coilValue = modbus_server.coilRead(0x00);
54+
Serial.println(coilValue);
55+
}
56+
}

0 commit comments

Comments
 (0)