Similar to the topic below I have a Growatt battery and Inverter that communicate with each other using RS485 Modbus RTU protocol.
I am using a slightly modified sketch from that topic but am having a problem with the forwarding.
The Inverter (serial 1) seems to send out a constant message that does not change at all.
07:28:42.869 -> Serial 1
07:28:42.869 -> 7F 7E BE FE BE EA F2
07:28:43.340 -> Transmitted to 2
07:28:43.340 -> Serial 1
07:28:43.340 -> 0
07:28:43.387 -> Transmitted to 2
07:28:43.387 -> Serial 1
07:28:43.387 -> 7F 7E BE FE BE EA F2
07:28:43.857 -> Transmitted to 2
07:28:43.857 -> Serial 1
07:28:43.857 -> 0
07:28:43.857 -> Transmitted to 2
I'm not sure as to why the 0 seems to be a separate message as I suspect it should all be one message. The battery (serial2) does not respond.
If i just sniff the communication when the battery and inverter are directly connected I get this. (note serials are connected the opposite way round in this)
10:37:01.957 -> Serial 2
10:37:01.957 -> 0
10:37:01.957 -> Transmitted to 1
10:37:01.957 -> Serial 2
10:37:01.957 -> 7F 7E BE FE BE EA F2
10:37:02.005 -> Transmitted to 1
10:37:02.052 -> Serial 2
10:37:02.052 -> 0 7F 7E F3 FE B2 2A A2 92 FE 56 FE FE FE D6 B2 9A FE F6 FE D2 BA 3E F6 8A 71 E2 AE FE E2 F2 3A FC FE 56 F4 FC
10:37:02.430 -> Transmitted to 1
10:37:02.430 -> Serial 2
10:37:02.430 -> 0
10:37:02.430 -> Transmitted to 1
10:37:02.478 -> Serial 2
10:37:02.478 -> 7F 7E BE FE BE EA F2
10:37:02.525 -> Transmitted to 1
10:37:02.525 -> Serial 2
10:37:02.525 -> 0 7F 7E F3 FE B2 26 A2 92 FE 56 FE FE FE D6 B2 9A FE F6 FE D2 BA 3E F6 8A 71 32 AE FE E2 F2 3A FC FE 5A FC
10:37:02.951 -> Transmitted to 1
10:37:02.951 -> Serial 2
10:37:02.951 -> 0
10:37:02.951 -> Transmitted to 1
10:37:02.951 -> Serial 2
10:37:02.951 -> 7F 7E BE FE BE EA F2
10:37:02.999 -> Transmitted to 1
10:37:03.047 -> Serial 2
10:37:03.047 -> 0 7F 7E F3 FE B2 26 A2 92 FE 56 FE FE FE D6 B2 9A FE F6 FE D2 BA 3E F6 8A 71 4A 4A FE E2 F2 3A FC FE EE 64 FD
10:37:03.424 -> Transmitted to 1
I have put a sniffer on the output of the forwarded and it does seem to be transmitting the message ok. I have also tried the forwarder with it sending 0 7F 7E BE FE BE EA F2 as a single message.
#include <SPI.h>
#define MESSAGE_GAP_TIMEOUT_IN_MS 5
#define LED_TOGGLE_MSGS 4
unsigned int messageCount = 0;
//byte RecvBuffer[] = {0x0, 0x7F, 0x7E, 0xBE, 0xFE, 0xBE, 0xEA, 0xF2};
byte RecvBuffer1[256];
byte RecvBuffer2[256];
unsigned int recvIndex1 = 0;
unsigned int recvIndex2 = 0;
unsigned long receiveTime1 = 0;
unsigned long receiveTime2 = 0;
unsigned long messageCount1 = 0;
unsigned long messageCount2 = 0;
bool LED_STATUS = false;
unsigned int bpsRecSerial1 = 0;
unsigned int bpsRecSerial2 = 0;
unsigned long lastRefreshSecondTime = 0;
bool timer1Started = false;
bool timer2Started = false;
unsigned long timer1StartMS = 0;
unsigned long timer2StartMS = 0;
const int serial1TPin = 2;
const int serial2TPin = 3;
void setup()
{
// serial setup
Serial.begin(115200); // PC USB output
Serial1.begin(9600); // RS485 module - battery
Serial2.begin(9600); // RS485 module - inverter
// pin setup
pinMode(serial1TPin, OUTPUT); // RS485 serial1 transmit
pinMode(serial2TPin, OUTPUT); // RS485 serial2 transmit
pinMode(LED_BUILTIN, OUTPUT);
// set initial LED status
digitalWrite(LED_BUILTIN, LOW);
}
void WriteToSerial(String message)
{
Serial.print(message);
}
void WriteToSerialLn(String message)
{
Serial.println(message);
}
void serial1Transmit(bool enable)
{
if (enable)
{
digitalWrite(serial1TPin, HIGH);
}
else
{
digitalWrite(serial1TPin, LOW);
}
}
void serial2Transmit(bool enable)
{
if (enable)
{
digitalWrite(serial2TPin, HIGH);
}
else
{
digitalWrite(serial2TPin, LOW);
}
}
void dumpBuffer1toSerial2()
{
if (recvIndex1 > 0)
{
//serial2Transmit(true);
digitalWrite(serial2TPin, HIGH);
Serial2.write(RecvBuffer1, recvIndex1);
Serial.println("Transmitted to 2");
Serial2.flush();
//serial2Transmit(false);
digitalWrite(serial1TPin, LOW);
outputBuffer1toConsole();
}
}
void dumpBuffer2toSerial1()
{
if (recvIndex2 > 0)
{
//serial1Transmit(true);
digitalWrite(serial1TPin, HIGH);
Serial1.write(RecvBuffer2, recvIndex2);
Serial.println("Transmitted to 1");
Serial1.flush();
//serial1Transmit(false);
digitalWrite(serial1TPin, LOW);
outputBuffer2toConsole();
}
}
void outputBuffer2toConsole()
{
if (recvIndex2 > 0)
{
Serial.println("Serial 2");
for (int i = 0; i < recvIndex2; i++)
{
Serial.print(RecvBuffer2[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
void outputBuffer1toConsole()
{
if (recvIndex1 > 0)
{
Serial.println("Serial 1");
for (int i = 0; i < recvIndex1; i++)
{
Serial.print(RecvBuffer1[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
void toggleLED()
{
LED_STATUS = !LED_STATUS;
digitalWrite(LED_BUILTIN, LED_STATUS);
}
void loop()
{
byte received;
// anything recvd on serial1 then store in buffer
if (Serial1.available() > 0)
{
received = Serial1.read();
RecvBuffer1[recvIndex1++] = received;
bpsRecSerial1++;
timer1Started = true;
timer1StartMS = millis();
}
// anything recvd on serial2 then store in buffer
if (Serial2.available() > 0)
{
received = Serial2.read();
RecvBuffer2[recvIndex2++] = received;
bpsRecSerial2++;
timer2Started = true;
timer2StartMS = millis();
}
// check and USB serial input and dump out both serial1 and serial2 for debugging
if (Serial.available() > 0)
{
received = Serial.read();
// transmit on serial 1
serial1Transmit(true);
Serial1.write(received);
serial1Transmit(false);
// transmit on serial 2
serial2Transmit(true);
Serial2.write(received);
serial2Transmit(false);
}
// message received on either interface, dump it out of the other one
if (timer1Started and (millis() - timer1StartMS >= MESSAGE_GAP_TIMEOUT_IN_MS))
{
// kill timer
timer1Started = false;
// increase message count
messageCount1++;
messageCount++;
// dump message
dumpBuffer1toSerial2();
// reset buffer
recvIndex1 = 0;
}
if (timer2Started and (millis() - timer2StartMS >= MESSAGE_GAP_TIMEOUT_IN_MS))
{
// kill timer
timer2Started = false;
// increase message count
messageCount2++;
messageCount++;
// dump message
dumpBuffer2toSerial1();
// reset buffer
recvIndex2 = 0;
}
// toggle LED if required
if (messageCount >= LED_TOGGLE_MSGS)
{
toggleLED();
messageCount = 0;
}
// each second output debugging information
if (millis() - lastRefreshSecondTime >= 1000)
{
lastRefreshSecondTime = millis();
if (true)
{
WriteToSerial("Serial1 (bps): ");
WriteToSerial(String(bpsRecSerial1));
WriteToSerial(" (buffer): ");
WriteToSerial(String(recvIndex1));
WriteToSerial(" (msgs): ");
WriteToSerial(String(messageCount1));
WriteToSerial(", Serial2 (bps): ");
WriteToSerial(String(bpsRecSerial2));
WriteToSerial(" (buffer): ");
WriteToSerial(String(recvIndex2));
WriteToSerial(" (msgs): ");
WriteToSerialLn(String(messageCount2));
}
bpsRecSerial1 = 0;
bpsRecSerial2 = 0;
}
}
