Elapsed: 0 s
- Connect opens a WebSerial Modbus RTU client.
- Apply (in settings) re-opens the client using the current settings,
reusing the same port.
- Start runs a full "runOnce()" test cycle repeatedly.
- Delay below is applied only between cycles.
- If repeated errors: apply interRequestDelay of a few ms
FC 16
bulk-write
64 words
0ā¦63FC 03
bulk-read verifyFC 06
single-write each word (reverse order)FC 03
read back in 8-word chunks verify
FC 04
one-shot read all 64FC 04
8-word chunk reads (8Ć8)FC 04
64 single-word reads ā compare all three results
FC 0F
write-multiple T F T F ⦠patternFC 01
bulk-read verify patternFC 05
toggle each coil individuallyFC 01
8-byte chunk reads verify new pattern
FC 02
one-shot read all 64FC 02
8-byte chunk reads (8Ć8)FC 02
64 single-bit reads ā compare all three results
#include <Arduino.h> // Some AVR cores don't define HAVE_CDCSERIAL even though USBCON exists. // This makes ModbusRTUSlave(Serial_&) available on ATmega32u4 (Leonardo/Micro/Pro Micro). #if defined(USBCON) && !defined(HAVE_CDCSERIAL) #define HAVE_CDCSERIAL #endif #includeconstexpr uint8_t SLAVE_ID = 1; constexpr uint32_t BAUD = 9600; /* 64-word / 64-bit data areas */ uint16_t holdingRegs[64]; uint16_t inputRegs [64]; bool coils [64]; bool discretes [64]; // Force USB-CDC serial (Serial_) on 32u4 ModbusRTUSlave mb(Serial); void setup() { Serial.begin(BAUD); // Don't block forever; browser opens the port when ready unsigned long t0 = millis(); while (!Serial && (millis() - t0) < 2000) {} mb.configureHoldingRegisters(holdingRegs, 64); mb.configureInputRegisters (inputRegs, 64); mb.configureCoils (coils, 64); mb.configureDiscreteInputs (discretes, 64); mb.begin(SLAVE_ID, BAUD, SERIAL_8N1); } void loop() { mb.poll(); }
#include#include constexpr uint8_t SLAVE_ID = 1; constexpr uint32_t BAUD = 9600; uint16_t holdingRegs[64]; uint16_t inputRegs[64]; bool coils[64]; bool discretes[64]; ModbusRTUSlave mb(Serial); void setup() { Serial.begin(BAUD); unsigned long t0 = millis(); while (!Serial && millis() - t0 < 2000) {} delay(100); // helps USB CDC settle mb.begin(SLAVE_ID, BAUD); mb.configureHoldingRegisters(holdingRegs, 64); mb.configureInputRegisters(inputRegs, 64); mb.configureCoils(coils, 64); mb.configureDiscreteInputs(discretes, 64); } void loop() { mb.poll(); }