Skip to content

Commit

Permalink
Add skeleton for SerialLink
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed Nov 19, 2023
1 parent a1aea67 commit 154cd7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -151,6 +156,9 @@ void loop() {
#ifdef DUAL_CAN
send_can2();
#endif
#ifdef SERIAL_LINK_RECEIVER_FROM_BATTERY
send_serial();
#endif
}

// Initialization functions
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 154cd7f

Please sign in to comment.