From ed3d0087b0a6e2902510f332d55a39831257a0fc Mon Sep 17 00:00:00 2001 From: chschloetel Date: Sat, 2 Nov 2024 21:11:07 +0100 Subject: [PATCH] modified: include/ModBusConfig.cpp modified: include/ModBusConfig.h modified: src/main.cpp --- include/ModBusConfig.cpp | 75 ++++++++++++++++++++++++++++++++++++++++ include/ModBusConfig.h | 37 ++++++++++++++++++++ src/main.cpp | 17 +++++++++ 3 files changed, 129 insertions(+) diff --git a/include/ModBusConfig.cpp b/include/ModBusConfig.cpp index e69de29..45a3398 100644 --- a/include/ModBusConfig.cpp +++ b/include/ModBusConfig.cpp @@ -0,0 +1,75 @@ +#include +#include + +/* + 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; +} + diff --git a/include/ModBusConfig.h b/include/ModBusConfig.h index e69de29..b163497 100644 --- a/include/ModBusConfig.h +++ b/include/ModBusConfig.h @@ -0,0 +1,37 @@ +#ifndef MODBUS_CONFIG_H +#define MODBUS_CONFIG_H + +#include + + +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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bec0bca..2ba4c4b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,11 @@ #include #include #include "ModbusSlaveConfigToEEPROM.h" // Inkludiere die Header-Datei für ModBusConfig +//include 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