modified: include/ModBusConfig.cpp
modified: include/ModBusConfig.h modified: src/main.cpp
This commit is contained in:
parent
c0391afc25
commit
ed3d0087b0
@ -0,0 +1,75 @@
|
||||
#include <ModBusConfig.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
/*
|
||||
Konstruktor: Der Konstruktor initialisiert die Modbus-ID und die Baudrate auf Standardwerte (1 und 9600),
|
||||
falls die EEPROM-Werte nicht gesetzt sind (d.h. wenn sie 0xFF sind).
|
||||
*/
|
||||
ModBusConfig::ModBusConfig() {
|
||||
// Optional: Initialisieren Sie die EEPROM-Werte, falls erforderlich
|
||||
if (EEPROM.read(MODBUS_ID_ADDRESS) == 0xFF) {
|
||||
SetModbusID(STANDARD_MODBUS_ID); // Setze Standard ID
|
||||
}
|
||||
if (EEPROM.read(BAUD_RATE_ADDRESS) == 0xFF) {
|
||||
SetBaudRate(STANDARD_MODBUS_BAUDRATE); // Setze Standard Baudrate
|
||||
}
|
||||
}
|
||||
|
||||
void ModBusConfig::SetModbusID(int ID) {
|
||||
if (ID >= 0 && ID <= MAX_MODBUS_ID_ADDRESS) { // Überprüfung, dass ID im gültigen Bereich liegt
|
||||
unsigned long currentTime = millis();
|
||||
if (currentTime - lastWriteTimeID >= WRITE_INTERVAL) { // Only write if enough time has passed
|
||||
EEPROM.put(MODBUS_ID_ADDRESS, ID);
|
||||
lastWriteTimeID = currentTime;
|
||||
}
|
||||
//EEPROM.write(MODBUS_ID_ADDRESS, ID);
|
||||
} else {
|
||||
Serial.println("Ungültige Modbus-ID. ID muss zwischen 0 und 255 liegen.");
|
||||
}
|
||||
}
|
||||
|
||||
void ModBusConfig::ResetModbusID() {
|
||||
unsigned long currentTime = millis();
|
||||
if (currentTime - lastWriteTimeID >= WRITE_INTERVAL) { // Only write if enough time has passed
|
||||
SetModbusID(STANDARD_MODBUS_ID); // Reset to standard ID
|
||||
lastWriteTimeID = currentTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ModBusConfig::SetBaudRate(int BAUDRATE) {
|
||||
int dividedBaudrate = BAUDRATE / 100;
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
// Überprüfung, dass Baudrate im gültigen Bereich liegt
|
||||
if (BAUDRATE > 0 && BAUDRATE <= MAX_BAUD_RATE) {
|
||||
if (dividedBaudrate != EEPROM.read(BAUD_RATE_ADDRESS)) {
|
||||
if (currentTime - lastWriteTimeBaud >= WRITE_INTERVAL) {
|
||||
EEPROM.put(BAUD_RATE_ADDRESS, dividedBaudrate);
|
||||
lastWriteTimeBaud = currentTime;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial.println("Ungültige Baudrate. Baudrate muss zwischen 0 und 115200 liegen.");
|
||||
}
|
||||
}
|
||||
|
||||
void ModBusConfig::ResetBaudRate() {
|
||||
unsigned long currentTime = millis();
|
||||
if (currentTime - lastWriteTimeBaud >= WRITE_INTERVAL) {
|
||||
SetBaudRate(STANDARD_MODBUS_BAUDRATE); // Reset to standard Baud rate
|
||||
lastWriteTimeBaud = currentTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int ModBusConfig::GetModbusID() {
|
||||
int MbID = EEPROM.read(MODBUS_ID_ADDRESS);
|
||||
return MbID;
|
||||
}
|
||||
|
||||
int ModBusConfig::GetBaudRate() {
|
||||
int Baudrate = EEPROM.read(BAUD_RATE_ADDRESS);
|
||||
return Baudrate;
|
||||
}
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
#ifndef MODBUS_CONFIG_H
|
||||
#define MODBUS_CONFIG_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
class ModBusConfig {
|
||||
public:
|
||||
ModBusConfig(); // Konstruktor ohne Parameter
|
||||
void SetModbusID(int ID);
|
||||
void SetBaudRate(int BAUDRATE);
|
||||
void ResetModbusID(); // Method to reset Modbus ID
|
||||
void ResetBaudRate(); // Method to reset Baud Rate
|
||||
int GetModbusID();
|
||||
int GetBaudRate();
|
||||
|
||||
private:
|
||||
/*
|
||||
In diesem Beispiel wird Adresse 0 für die Modbus-ID und Adresse 2 für die Baudrate verwendet.
|
||||
Diese Adressen sind willkürlich gewählt und sollten nicht mit anderen Daten im EEPROM überlappen.
|
||||
*/
|
||||
static const int MODBUS_ID_ADDRESS = 0; // Speicheradresse für Modbus-ID
|
||||
static const int BAUD_RATE_ADDRESS = 2; // Speicheradresse für Baudrate
|
||||
|
||||
static const int MAX_BAUD_RATE = 38400; // Maximal unterstützte Baudrate
|
||||
static const int MAX_MODBUS_ID_ADDRESS = 254; // Maximal unterstützte Baudrate
|
||||
|
||||
static const int STANDARD_MODBUS_ID = 22;
|
||||
static const int STANDARD_MODBUS_BAUDRATE = 9600;
|
||||
|
||||
unsigned long lastWriteTimeID = 0; // Last write time for Modbus ID
|
||||
unsigned long lastWriteTimeBaud = 0; // Last write time for Baud rate
|
||||
const unsigned long WRITE_INTERVAL = 120000; // 2 minutes in milliseconds
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
17
src/main.cpp
17
src/main.cpp
@ -3,7 +3,11 @@
|
||||
#include <ModbusSlave.h>
|
||||
#include <SHT2x.h>
|
||||
#include "ModbusSlaveConfigToEEPROM.h" // Inkludiere die Header-Datei für ModBusConfig
|
||||
//include <ModBusConfig.h> wenn aktiviert wird, dann probleme
|
||||
|
||||
// -------------------------
|
||||
// Definitions
|
||||
// -------------------------
|
||||
// Statische Definition von Modbus-ID und Baudrate
|
||||
#define SLAVE_ID 8
|
||||
#define SERIAL_BAUDRATERS485 9600
|
||||
@ -13,6 +17,9 @@
|
||||
#define RS485_CTRL_PIN 8
|
||||
#define OutPutPin 9
|
||||
|
||||
// -------------------------
|
||||
// Global Variables
|
||||
// -------------------------
|
||||
// Register Arrays für Modbus
|
||||
uint8_t ReadCoilRegister[] = {0, 1, 2};
|
||||
uint8_t output_pins[] = {0,1,2,3};
|
||||
@ -32,6 +39,10 @@ Modbus slave(SERIAL_PORTRS485, SLAVE_ID, RS485_CTRL_PIN);
|
||||
Modbus slave(SERIAL_PORTRS485, SLAVE_ID);
|
||||
#endif
|
||||
|
||||
// -------------------------
|
||||
// Function Definitions
|
||||
// -------------------------
|
||||
|
||||
/*
|
||||
Instanz der ModBusConfig-Klasse
|
||||
Aktuell keine Verwendung, eine Testimplementierung, nur Ausgabezwecke
|
||||
@ -200,6 +211,12 @@ uint8_t fReadHoldingRegister(uint8_t fc, uint16_t address, uint16_t length) {
|
||||
return STATUS_OK;
|
||||
}
|
||||
|
||||
|
||||
// -------------------------
|
||||
// Setup and Loop Functions
|
||||
// -------------------------
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(RS485_CTRL_PIN, OUTPUT);
|
||||
digitalWrite(RS485_CTRL_PIN, LOW); // RS485-Empfang aktivieren
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user