From 154cd7f001cc521b1569dee741574a9ac71b3ef6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 19 Nov 2023 22:50:10 +0200 Subject: [PATCH] Add skeleton for SerialLink --- Software/Software.ino | 31 +++++++++++++++++++++++++++++++ Software/USER_SETTINGS.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/Software/Software.ino b/Software/Software.ino index 358711df..f6403570 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -17,7 +17,9 @@ // Interval settings int intervalUpdateValues = 4800; // Interval at which to update inverter values / Modbus registers +const int interval1 = 1; // Interval for 1ms tasks const int interval10 = 10; // Interval for 10ms tasks +unsigned long previousMillis1ms = 0; unsigned long previousMillis10ms = 50; unsigned long previousMillisUpdateVal = 0; @@ -129,6 +131,9 @@ void loop() { #ifdef DUAL_CAN receive_can2(); #endif +#ifdef SERIAL_LINK_TRANSMITTER_INVERTER + receive_serial(); +#endif // Process if (millis() - previousMillis10ms >= interval10) // Every 10ms @@ -151,6 +156,9 @@ void loop() { #ifdef DUAL_CAN send_can2(); #endif +#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY + send_serial(); +#endif } // Initialization functions @@ -218,6 +226,13 @@ void init_modbus() { pinMode(PIN_5V_EN, OUTPUT); digitalWrite(PIN_5V_EN, HIGH); +#if defined(SERIAL_LINK_RECEIVER_FROM_BATTERY) || defined(SERIAL_LINK_TRANSMITTER_INVERTER) + Serial2.begin(9600); // If the Modbus RTU port will be used for serial link +#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS) +#error Modbus pins cannot be used for Serial and Modbus at the same time! +#endif +#endif + #ifdef BYD_MODBUS // Init Static data to the RTU Modbus handle_static_data_modbus_byd(); @@ -384,6 +399,22 @@ void send_can() { #endif } +void send_serial() { + static unsigned long currentMillis = millis(); + if (currentMillis - previousMillis1ms >= interval1) { + previousMillis1ms = currentMillis; + manageSerialLinkReceiver(); + } +} + +void receive_serial() { + static unsigned long currentMillis = millis(); + if (currentMillis - previousMillis1ms >= interval1) { + previousMillis1ms = currentMillis; + manageSerialLinkTransmitter(); + } +} + #ifdef DUAL_CAN void receive_can2() { // This function is similar to receive_can, but just takes care of inverters in the 2nd bus. // Depending on which inverter is selected, we forward this to their respective CAN routines diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index c05068c6..83c5e617 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -42,5 +42,7 @@ //#define CONTACTOR_CONTROL //Enable this line to have pins 25,32,33 handle automatic precharge/contactor+/contactor- closing sequence //#define PWM_CONTACTOR_CONTROL //Enable this line to use PWM logic for contactors, which lower power consumption and heat generation //#define DUAL_CAN //Enable this line to activate an isolated secondary CAN Bus using add-on MCP2515 controller (Needed for FoxESS inverters) +#define SERIAL_LINK_RECEIVER_FROM_BATTERY //Enable this line to send battery data over Modbus pins to another Lilygo +#define SERIAL_LINK_TRANSMITTER_INVERTER //Enable this line to receive battery data over Modbus pins from another Lilygo #endif