Skip to content

Commit

Permalink
Merge pull request #108 from manchoz/opta_support
Browse files Browse the repository at this point in the history
Add Opta support
  • Loading branch information
aentinger authored Jan 16, 2023
2 parents b727094 + 4be4d04 commit 4cc57e8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ jobs:
- fqbn: arduino:mbed_portenta:envie_m7
ethernet: false
nina: false
- fqbn: arduino:mbed_opta:opta
ethernet: true
nina: false

# Make board type-specific customizations to the matrix jobs
include:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Modbus RTU Client Parameters
This sketch is the same as ModbusRTUClientToggle and shows how to set
a few RS485 parameters.
Circuit:
- Arduino Opta
- GND connected to GND of the Modbus RTU server
- A(-) connected to A of the Modbus RTU server
- B(+) connected to B of the Modbus RTU server
*/

#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>

constexpr auto baudrate { 19200 };

// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
//
// MODBUS over serial line specification and implementation guide V1.02
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
constexpr auto bitduration { 1.f / baudrate };
constexpr auto wordlen { 9.6f }; // try also with 10.0f
constexpr auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
constexpr auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };

void setup() {
Serial.begin(9600);
while (!Serial);

Serial.println("Modbus RTU Client Toggle w/ Parameters");

RS485.setDelays(preDelayBR, postDelayBR);

// start the Modbus RTU client in 8E1 mode
if (!ModbusRTUClient.begin(baudrate, SERIAL_8E1)) {
Serial.println("Failed to start Modbus RTU Client!");
while (1);
}
}

void loop() {
// for (slave) id 1: write the value of 0x01, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x01)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}

// wait for 0.5 second
delay(500);

// for (slave) id 1: write the value of 0x00, to the coil at address 0x00
if (!ModbusRTUClient.coilWrite(1, 0x00, 0x00)) {
Serial.print("Failed to write coil! ");
Serial.println(ModbusRTUClient.lastError());
}

// wait for 0.5 second
delay(500);

auto coil = ModbusRTUClient.coilRead(0x02, 0x00);

if (coil < 0) {
Serial.print("Failed to read coil: ");
Serial.println(ModbusRTUClient.lastError());
} else {
Serial.print("Coil: ");
Serial.println(coil);
}

// wait for 0.5 second
delay(500);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ sentence=Use Modbus equipment with your Arduino.
paragraph=Using TCP or RS485 shields, like the MKR 485 Shield. This library depends on the ArduinoRS485 library.
category=Communication
url=https://www.arduino.cc/en/ArduinoModbus/ArduinoModbus
architectures=megaavr,samd,mbed_nano,mbed_portenta
architectures=megaavr,samd,mbed_nano,mbed_portenta,mbed_opta
includes=ArduinoModbus.h
depends=ArduinoRS485

0 comments on commit 4cc57e8

Please sign in to comment.