First commit

This commit is contained in:
marcel
2023-12-29 13:49:44 +01:00
commit 7d38f2cb37
261 changed files with 38553 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
/**
@file Lamp.ino
Modbus-Arduino Example - Lamp (Modbus Serial)
Copyright (C) 2023 Pascal JEAN aka epsilonrt
Copyright (C) 2014 André Sarmento Barbosa
https://github.com/epsilonrt/modbus-serial
*/
#include <ModbusSerial.h>
// Used Pins
const int LedPin = 13; // Change that to match your led
const int TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.
const byte SlaveId = 10;
// Modbus Registers Offsets (0-9999)
const int Lamp1Coil = 0;
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;
// ModbusSerial object
ModbusSerial mb (MySerial, SlaveId, TxenPin);
void setup() {
MySerial.begin (Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
// MySerial.begin (Baudrate, MB_PARITY_EVEN);
mb.config (Baudrate);
mb.setAdditionalServerData ("LAMP"); // for Report Server ID function (0x11)
// Set LedPin mode
pinMode (LedPin, OUTPUT);
// Add Lamp1Coil register - Use addCoil() for digital outputs
mb.addCoil (Lamp1Coil);
}
void loop() {
// Call once inside loop() - all magic here
mb.task();
// Attach LedPin to Lamp1Coil register
digitalWrite (LedPin, mb.Coil (Lamp1Coil));
}

View File

@@ -0,0 +1,83 @@
/**
@file LampDimmer.ino
Modbus-Arduino Example - Lamp dimmer (Modbus Serial)
Copyright (C) 2023 Pascal JEAN aka epsilonrt
https://github.com/epsilonrt/modbus-serial
*/
#include <ModbusSerial.h>
// Used Pins
const int LedPin = 3; // Must be PWM output !
const int TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.
const byte SlaveId = 11; // our Modbus slave address (1-247)
// Modbus Registers Offsets (0-9999), PDU adressing ! from the data model we have to add 1
const int Lamp1Coil = 0;
const int Lamp1Hreg = 0;
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;
// ModbusSerial object
ModbusSerial mb (MySerial, SlaveId, TxenPin);
// Lamp status variables
bool lampOn = true;
word lampDimming = 128;
void setup() {
MySerial.begin (Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
// MySerial.begin (Baudrate, MB_PARITY_EVEN);
mb.config (Baudrate);
mb.setAdditionalServerData ("LDIMMER"); // for Report Server ID function (0x11)
// Set LedPin
pinMode (LedPin, OUTPUT);
mb.addCoil (Lamp1Coil, lampOn);
mb.addHreg (Lamp1Hreg, lampDimming);
// The led flashes 20 times to signal the end of setup,
// then it is set to its default state.
for (int i = 0; i < 20; i++) {
digitalWrite (LedPin, HIGH);
delay (50);
digitalWrite (LedPin, LOW);
delay (100);
}
analogWrite (LedPin, (lampOn ? lampDimming : 0));
}
void loop() {
// Call once inside loop() - all magic here
mb.task();
if (mb.Coil (Lamp1Coil) != lampOn) {
// If coil was modified
lampOn = mb.Coil (Lamp1Coil);
if (lampOn) {
analogWrite (LedPin, lampDimming);
}
else {
analogWrite (LedPin, 0);
}
}
if ( (mb.Hreg (Lamp1Hreg) & 0x00FF) != lampDimming) {
// If holding register was modified (only LSB will used)
lampDimming = (mb.Hreg (Lamp1Hreg) & 0x00FF);
if (lampOn) {
analogWrite (LedPin, lampDimming);
}
}
}

View File

@@ -0,0 +1,54 @@
/**
@file Servo.ino
Copyright (C) 2023 Pascal JEAN aka epsilonrt
Copyright (C) 2014 André Sarmento Barbosa
https://github.com/epsilonrt/modbus-serial
Use Servo lib
This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor.
https://registry.platformio.org/libraries/arduino-libraries/Servo
*/
#include <ModbusSerial.h>
#include <Servo.h>
// Used Pins
const int servoPin = 9;
const int TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.
const byte SlaveId = 12;
// Modbus Registers Offsets (0-9999)
const int ServoHreg = 0;
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;
// ModbusSerial object
ModbusSerial mb (MySerial, SlaveId, TxenPin);
// Servo object
Servo servo;
void setup() {
MySerial.begin (Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
// MySerial.begin (Baudrate, MB_PARITY_EVEN);
mb.config (Baudrate);
mb.setAdditionalServerData ("SWITCH"); // for Report Server ID function (0x11)
// Attaches the servo pin to the servo object
servo.attach (servoPin);
// Add ServoHreg register - Use addHreg() for analog outpus or to store values in device
mb.addHreg (ServoHreg, 127);
}
void loop() {
//Call once inside loop() - all magic here
mb.task();
//Attach switchPin to SWITCH_ISTS register
servo.write (mb.Hreg (ServoHreg));
delay (15);
}

View File

@@ -0,0 +1,46 @@
/**
@file Switch.ino
Modbus-Arduino Example - Switch (Modbus Serial)
Copyright (C) 2023 Pascal JEAN aka epsilonrt
Copyright (C) 2014 André Sarmento Barbosa
https://github.com/epsilonrt/modbus-serial
*/
#include <ModbusSerial.h>
// Used Pins
const int SwitchPin = 3;
const int TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.
const byte SlaveId = 13;
// Modbus Registers Offsets (0-9999)
const int SwitchIsts = 0;
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;
// ModbusSerial object
ModbusSerial mb (MySerial, SlaveId, TxenPin);
void setup() {
MySerial.begin (Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
// MySerial.begin (Baudrate, MB_PARITY_EVEN);
mb.config (Baudrate);
mb.setAdditionalServerData ("SWITCH"); // for Report Server ID function (0x11)
// Set SwitchPin mode
pinMode (SwitchPin, INPUT);
// Add SwitchIsts register - Use addIsts() for digital inputs
mb.addIsts (SwitchIsts);
}
void loop() {
//Call once inside loop() - all magic here
mb.task();
//Attach SwitchPin to SwitchIsts register
mb.Ists (SwitchIsts, digitalRead (SwitchPin));
}

View File

@@ -0,0 +1,55 @@
/**
@file TempSensor.ino
Modbus-Arduino Example - TempSensor (Modbus Serial)
Copyright (C) 2023 Pascal JEAN aka epsilonrt
Copyright (C) 2014 André Sarmento Barbosa
https://github.com/epsilonrt/modbus-serial
*/
#include <ModbusSerial.h>
// Used Pins
const int SensorPin = A0;
const int TxenPin = -1; // -1 disables the feature, change that if you are using an RS485 driver, this pin would be connected to the DE and /RE pins of the driver.
const byte SlaveId = 14;
// Modbus Registers Offsets (0-9999)
const int SensorIreg = 0;
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
const unsigned long Baudrate = 38400;
// ModbusSerial object
ModbusSerial mb (MySerial, SlaveId, TxenPin);
long ts;
void setup() {
MySerial.begin (Baudrate); // works on all boards but the configuration is 8N1 which is incompatible with the MODBUS standard
// prefer the line below instead if possible
// MySerial.begin (Baudrate, MB_PARITY_EVEN);
mb.config (Baudrate);
mb.setAdditionalServerData ("TEMP_SENSOR"); // for Report Server ID function (0x11)
// Add SensorIreg register - Use addIreg() for analog Inputs
mb.addIreg (SensorIreg);
ts = millis();
}
void loop() {
// Call once inside loop() - all magic here
mb.task();
// Read each two seconds
if (millis() > ts + 2000) {
ts = millis();
// Setting raw value (0-1024)
mb.Ireg (SensorIreg, analogRead (SensorPin));
}
}