Modbus RTU Slave mit SW-Serial
Das Programm läuft an Arduino UNO. UNO hat nur einen seriellen Port, daher ist SW-Serial eine sehr nützliche Funktion, damit mehrere Quasi-UARTs öffnen können. Problem ist, daß es nicht mehreren Kommunikationslösungen funktioniert mit. In diesem Fall fungiert der Arduino Modbus als RTU-Slave und führt gleichzeitig den seriellen Monitor aus.
Verdrahtung
Libraries
- SoftwareSerial.h: https://www.arduino.cc/en/Reference/softwareSerial
Program
#include <ModbusRtu.h> #include <SoftwareSerial.h> #define SSerialTxControl 10 SoftwareSerial mySerial(2, 3); int count = 0; // 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 slave(1); void setup() { Serial.begin(9600); slave.begin( &mySerial, 9600, SSerialTxControl ); slave.start(); } void loop() { count++; au16data[0] = count; delay(100); slave.poll( au16data, 16 ); }