initial
This commit is contained in:
@ -0,0 +1,183 @@
|
||||
/*
|
||||
Modbus RTU Client Kitchen Sink
|
||||
|
||||
This sketch creates a Modbus RTU Client and demonstrates
|
||||
how to use various Modbus Client APIs.
|
||||
|
||||
Circuit:
|
||||
- MKR board
|
||||
- MKR 485 shield
|
||||
- ISO GND connected to GND of the Modbus RTU server
|
||||
- Y connected to A/Y of the Modbus RTU server
|
||||
- Z connected to B/Z of the Modbus RTU server
|
||||
- Jumper positions
|
||||
- FULL set to OFF
|
||||
- Z \/\/ Y set to ON
|
||||
|
||||
created 18 July 2018
|
||||
by Sandeep Mistry
|
||||
*/
|
||||
|
||||
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
|
||||
#include <ArduinoModbus.h>
|
||||
|
||||
int counter = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("Modbus RTU Client Kitchen Sink");
|
||||
|
||||
// start the Modbus RTU client
|
||||
if (!ModbusRTUClient.begin(9600)) {
|
||||
Serial.println("Failed to start Modbus RTU Client!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
writeCoilValues();
|
||||
|
||||
readCoilValues();
|
||||
|
||||
readDiscreteInputValues();
|
||||
|
||||
writeHoldingRegisterValues();
|
||||
|
||||
readHoldingRegisterValues();
|
||||
|
||||
readInputRegisterValues();
|
||||
|
||||
counter++;
|
||||
|
||||
delay(5000);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void writeCoilValues() {
|
||||
// set the coils to 1 when counter is odd
|
||||
byte coilValue = ((counter % 2) == 0) ? 0x00 : 0x01;
|
||||
|
||||
Serial.print("Writing Coil values ... ");
|
||||
|
||||
// write 10 Coil values to (slave) id 42, address 0x00
|
||||
ModbusRTUClient.beginTransmission(42, COILS, 0x00, 10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ModbusRTUClient.write(coilValue);
|
||||
}
|
||||
if (!ModbusRTUClient.endTransmission()) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
}
|
||||
|
||||
// Alternatively, to write a single Coil value use:
|
||||
// ModbusRTUClient.coilWrite(...)
|
||||
}
|
||||
|
||||
void readCoilValues() {
|
||||
Serial.print("Reading Coil values ... ");
|
||||
|
||||
// read 10 Coil values from (slave) id 42, address 0x00
|
||||
if (!ModbusRTUClient.requestFrom(42, COILS, 0x00, 10)) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
|
||||
while (ModbusRTUClient.available()) {
|
||||
Serial.print(ModbusRTUClient.read());
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// Alternatively, to read a single Coil value use:
|
||||
// ModbusRTUClient.coilRead(...)
|
||||
}
|
||||
|
||||
void readDiscreteInputValues() {
|
||||
Serial.print("Reading Discrete Input values ... ");
|
||||
|
||||
// read 10 Discrete Input values from (slave) id 42, address 0x00
|
||||
if (!ModbusRTUClient.requestFrom(42, DISCRETE_INPUTS, 0x00, 10)) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
|
||||
while (ModbusRTUClient.available()) {
|
||||
Serial.print(ModbusRTUClient.read());
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// Alternatively, to read a single Discrete Input value use:
|
||||
// ModbusRTUClient.discreteInputRead(...)
|
||||
}
|
||||
|
||||
void writeHoldingRegisterValues() {
|
||||
// set the Holding Register values to counter
|
||||
|
||||
Serial.print("Writing Holding Registers values ... ");
|
||||
|
||||
// write 10 coil values to (slave) id 42, address 0x00
|
||||
ModbusRTUClient.beginTransmission(42, HOLDING_REGISTERS, 0x00, 10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ModbusRTUClient.write(counter);
|
||||
}
|
||||
if (!ModbusRTUClient.endTransmission()) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
}
|
||||
|
||||
// Alternatively, to write a single Holding Register value use:
|
||||
// ModbusRTUClient.holdingRegisterWrite(...)
|
||||
}
|
||||
|
||||
void readHoldingRegisterValues() {
|
||||
Serial.print("Reading Input Register values ... ");
|
||||
|
||||
// read 10 Input Register values from (slave) id 42, address 0x00
|
||||
if (!ModbusRTUClient.requestFrom(42, HOLDING_REGISTERS, 0x00, 10)) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
|
||||
while (ModbusRTUClient.available()) {
|
||||
Serial.print(ModbusRTUClient.read());
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// Alternatively, to read a single Holding Register value use:
|
||||
// ModbusRTUClient.holdingRegisterRead(...)
|
||||
}
|
||||
|
||||
void readInputRegisterValues() {
|
||||
Serial.print("Reading input register values ... ");
|
||||
|
||||
// read 10 discrete input values from (slave) id 42,
|
||||
if (!ModbusRTUClient.requestFrom(42, INPUT_REGISTERS, 0x00, 10)) {
|
||||
Serial.print("failed! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
Serial.println("success");
|
||||
|
||||
while (ModbusRTUClient.available()) {
|
||||
Serial.print(ModbusRTUClient.read());
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
// Alternatively, to read a single Input Register value use:
|
||||
// ModbusRTUClient.inputRegisterRead(...)
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
Modbus RTU Client Toggle
|
||||
|
||||
This sketch toggles the coil of a Modbus RTU server connected via RS485
|
||||
on and off every second.
|
||||
|
||||
Circuit:
|
||||
- MKR board
|
||||
- MKR 485 shield
|
||||
- ISO GND connected to GND of the Modbus RTU server
|
||||
- Y connected to A/Y of the Modbus RTU server
|
||||
- Z connected to B/Z of the Modbus RTU server
|
||||
- Jumper positions
|
||||
- FULL set to OFF
|
||||
- Z \/\/ Y set to ON
|
||||
|
||||
created 16 July 2018
|
||||
by Sandeep Mistry
|
||||
*/
|
||||
|
||||
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
|
||||
#include <ArduinoModbus.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("Modbus RTU Client Toggle");
|
||||
|
||||
// start the Modbus RTU client
|
||||
if (!ModbusRTUClient.begin(9600)) {
|
||||
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 1 second
|
||||
delay(1000);
|
||||
|
||||
// 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 1 second
|
||||
delay(1000);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Modbus RTU Server Kitchen Sink
|
||||
|
||||
This sketch creates a Modbus RTU Server and demonstrates
|
||||
how to use various Modbus Server APIs.
|
||||
|
||||
Circuit:
|
||||
- MKR board
|
||||
- MKR 485 shield
|
||||
- ISO GND connected to GND of the Modbus RTU server
|
||||
- Y connected to A/Y of the Modbus RTU client
|
||||
- Z connected to B/Z of the Modbus RTU client
|
||||
- Jumper positions
|
||||
- FULL set to OFF
|
||||
- Z \/\/ Y set to OFF
|
||||
|
||||
created 18 July 2018
|
||||
by Sandeep Mistry
|
||||
*/
|
||||
|
||||
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
|
||||
#include <ArduinoModbus.h>
|
||||
|
||||
const int numCoils = 10;
|
||||
const int numDiscreteInputs = 10;
|
||||
const int numHoldingRegisters = 10;
|
||||
const int numInputRegisters = 10;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("Modbus RTU Server Kitchen Sink");
|
||||
|
||||
// start the Modbus RTU server, with (slave) id 42
|
||||
if (!ModbusRTUServer.begin(42, 9600)) {
|
||||
Serial.println("Failed to start Modbus RTU Server!");
|
||||
while (1);
|
||||
}
|
||||
|
||||
// configure coils at address 0x00
|
||||
ModbusRTUServer.configureCoils(0x00, numCoils);
|
||||
|
||||
// configure discrete inputs at address 0x00
|
||||
ModbusRTUServer.configureDiscreteInputs(0x00, numDiscreteInputs);
|
||||
|
||||
// configure holding registers at address 0x00
|
||||
ModbusRTUServer.configureHoldingRegisters(0x00, numHoldingRegisters);
|
||||
|
||||
// configure input registers at address 0x00
|
||||
ModbusRTUServer.configureInputRegisters(0x00, numInputRegisters);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// poll for Modbus RTU requests
|
||||
ModbusRTUServer.poll();
|
||||
|
||||
// map the coil values to the discrete input values
|
||||
for (int i = 0; i < numCoils; i++) {
|
||||
int coilValue = ModbusRTUServer.coilRead(i);
|
||||
|
||||
ModbusRTUServer.discreteInputWrite(i, coilValue);
|
||||
}
|
||||
|
||||
// map the holding register values to the input register values
|
||||
for (int i = 0; i < numHoldingRegisters; i++) {
|
||||
long holdingRegisterValue = ModbusRTUServer.holdingRegisterRead(i);
|
||||
|
||||
ModbusRTUServer.inputRegisterWrite(i, holdingRegisterValue);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
Modbus RTU Server LED
|
||||
|
||||
This sketch creates a Modbus RTU Server with a simulated coil.
|
||||
The value of the simulated coil is set on the LED
|
||||
|
||||
Circuit:
|
||||
- MKR board
|
||||
- MKR 485 shield
|
||||
- ISO GND connected to GND of the Modbus RTU server
|
||||
- Y connected to A/Y of the Modbus RTU client
|
||||
- Z connected to B/Z of the Modbus RTU client
|
||||
- Jumper positions
|
||||
- FULL set to OFF
|
||||
- Z \/\/ Y set to OFF
|
||||
|
||||
created 16 July 2018
|
||||
by Sandeep Mistry
|
||||
*/
|
||||
|
||||
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
|
||||
#include <ArduinoModbus.h>
|
||||
|
||||
const int ledPin = LED_BUILTIN;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
Serial.println("Modbus RTU Server LED");
|
||||
|
||||
// start the Modbus RTU server, with (slave) id 1
|
||||
if (!ModbusRTUServer.begin(1, 9600)) {
|
||||
Serial.println("Failed to start Modbus RTU Server!");
|
||||
while (1);
|
||||
}
|
||||
|
||||
// configure the LED
|
||||
pinMode(ledPin, OUTPUT);
|
||||
digitalWrite(ledPin, LOW);
|
||||
|
||||
// configure a single coil at address 0x00
|
||||
ModbusRTUServer.configureCoils(0x00, 1);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// poll for Modbus RTU requests
|
||||
int packetReceived = ModbusRTUServer.poll();
|
||||
|
||||
if(packetReceived) {
|
||||
// read the current value of the coil
|
||||
int coilValue = ModbusRTUServer.coilRead(0x00);
|
||||
|
||||
if (coilValue) {
|
||||
// coil value set, turn LED on
|
||||
digitalWrite(ledPin, HIGH);
|
||||
} else {
|
||||
// coil value clear, turn LED off
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
Modbus RTU Temperature Sensor
|
||||
|
||||
This sketch shows you how to interact with a Modbus RTU temperature and humidity sensor.
|
||||
It reads the temperature and humidity values every 5 seconds and outputs them to the
|
||||
serial monitor.
|
||||
|
||||
Circuit:
|
||||
- MKR board
|
||||
- Winners® Modbus RS485 Temperature and Humidity:
|
||||
https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN
|
||||
- External 9-36 V power Supply
|
||||
- MKR 485 shield
|
||||
- ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-
|
||||
- Power supply V+ connected to V+ sensor
|
||||
- Y connected to A/Y of the Modbus RTU sensor
|
||||
- Z connected to B/Z of the Modbus RTU sensor
|
||||
- Jumper positions
|
||||
- FULL set to OFF
|
||||
- Z \/\/ Y set to ON
|
||||
|
||||
created 8 August 2018
|
||||
by Riccardo Rizzo
|
||||
*/
|
||||
|
||||
#include <ArduinoModbus.h>
|
||||
|
||||
float temperature;
|
||||
float humidity;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("Modbus Temperature Humidity Sensor");
|
||||
// start the Modbus RTU client
|
||||
if (!ModbusRTUClient.begin(9600)) {
|
||||
Serial.println("Failed to start Modbus RTU Client!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// send a Holding registers read request to (slave) id 1, for 2 registers
|
||||
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) {
|
||||
Serial.print("failed to read registers! ");
|
||||
Serial.println(ModbusRTUClient.lastError());
|
||||
} else {
|
||||
// If the request succeeds, the sensor sends the readings, that are
|
||||
// stored in the holding registers. The read() method can be used to
|
||||
// get the raw temperature and the humidity values.
|
||||
short rawtemperature = ModbusRTUClient.read();
|
||||
short rawhumidity = ModbusRTUClient.read();
|
||||
|
||||
// To get the temperature in Celsius and the humidity reading as
|
||||
// a percentage, divide the raw value by 10.0.
|
||||
temperature = rawtemperature / 10.0;
|
||||
humidity = rawhumidity / 10.0;
|
||||
Serial.println(temperature);
|
||||
Serial.println(humidity);
|
||||
}
|
||||
|
||||
delay(5000);
|
||||
}
|
Reference in New Issue
Block a user