RS485 Forwarder

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;
  }

}

I am a little vague as to your question, but what i do see is this

    serial1Transmit(true);
    Serial1.write(received);
    serial1Transmit(false);
    // transmit on serial 2
    serial2Transmit(true);
    Serial2.write(received);
    serial2Transmit(false);

You should wait until the byte is transmitted before you switch mode on the transceiver.
2 way transmission on the same bus is tricky to program, while debugging 1 way, make sure there is not this interference.

Thank you. Sorry, what I can't quite understand is that the forwarder appears to send the message received from the inverter on to the battery via the other serial port but the battery does not respond to that message. If I put a separate independent mega and RS485 receiver on the battery bus I can see the message has been transmitted from the forwarder. I was wondering if somehow I am not transmitting the inverter message exactly as it has been received from the inverter.

    digitalWrite(serial2TPin, HIGH);
    Serial2.write(RecvBuffer1, recvIndex1);

have you considered putting a delay in between these 2 lines.

    Serial1.flush();

    digitalWrite(serial1TPin, LOW);

and in between these ?
After the UART buffer gets emptied, it will take at least another millisecond to transmit the data, and you switch the transceivers mode,
The transceiver may throw out a an unintentional '0' by being switched on as well.
I suggest you do not switch the transceiver's mode and do a simple passthrough.
Pretty sure the transceiver switching is the cause.

Thank you i will try tomorrow.

Yeah, let me add something. The sketch, although it looks transparant, it isn't. Everything is divided into functions, just the way it should be, but there are 2 functions depending on direction, and that should really only be one. Also i don't think your serial monitor output actually corresponds to what is being shown, and i am having a real hard time to find the bits of code that are supposed to generate what you have shown us.
I find it in the end, but small sections of the code also take up quite a lot of the screen space i see. It is partly the forum software, but i usually do it like this :

void serial2Transmit(bool enable) {
  if (enable)   {
    digitalWrite(serial2TPin, HIGH);
  }
  else   {
    digitalWrite(serial2TPin, LOW);
  }
}

and don't put comments in between lines.

I dunno, all together, i just found it really hard to read, and that may be part of the issue. Just the Serial passthrough example should be your starting point, to confirm that the hardware is connected and working properly.

I've written a sketch just to relay what the inverter is sending onto the other RS485 battery bus as below.

void setup() {

  Serial.begin(9600);

  Serial1.begin(9600);
  Serial2.begin(9600);

  const int serial1TPin = 2;
  const int serial2TPin = 3;

  pinMode(serial1TPin, OUTPUT); // RS485 serial1 transmit
  pinMode(serial2TPin, OUTPUT); // RS485 serial2 transmit

  digitalWrite(serial1TPin, HIGH);
  digitalWrite(serial2TPin, LOW);
}

void loop() {

  if (Serial1.available()) {      // If anything comes in Serial1 

    Serial2.write(Serial1.read());   // read it and send it out Serial2 

  }

  if (Serial2.available()) {     // If anything comes in Serial2 

    Serial1.write(Serial2.read());   // read it and send it out Serial1 

  }
}

on that bus (Battery) I have the battery connected and another RS485 receiver with the following sketch just to sniff.

#include <SPI.h>

#define MESSAGE_GAP_TIMEOUT_IN_MS 5

bool timer1Started = false;
bool timer2Started = false;
unsigned long timer1StartMS = 0;
unsigned long timer2StartMS = 0;

byte RecvBuffer1[256];
byte RecvBuffer2[256];

unsigned int recvIndex1 = 0;
unsigned int recvIndex2 = 0;

const int serial1TPin = 2;
const int serial2TPin = 3;

void setup()
{
  
  Serial.begin(115200); // PC USB output
  Serial1.begin(9600); // RS485 module - battery
  Serial2.begin(9600); // RS485 module - inverter
  
  pinMode(serial1TPin, OUTPUT); // RS485 serial1 transmit
  pinMode(serial2TPin, OUTPUT); // RS485 serial2 transmit
  digitalWrite(serial1TPin, LOW);
  digitalWrite(serial2TPin, LOW);

}

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 loop()
{
  byte received;

  // anything recvd on serial1 then store in buffer
  if (Serial1.available() > 0)
  {
    received = Serial1.read();
    RecvBuffer1[recvIndex1++] = received;
    timer1Started = true;
    timer1StartMS = millis();
  }
  
  if (timer1Started and (millis() - timer1StartMS >= MESSAGE_GAP_TIMEOUT_IN_MS))
  {
    outputBuffer1toConsole();
    recvIndex1 = 0;
  }


  // anything recvd on serial2 then store in buffer
  if (Serial2.available() > 0)
  {
    received = Serial2.read();
    RecvBuffer2[recvIndex2++] = received;
    timer2Started = true;
    timer2StartMS = millis();
  }
  
  if (timer2Started and (millis() - timer2StartMS >= MESSAGE_GAP_TIMEOUT_IN_MS))
  {
    outputBuffer2toConsole();
    recvIndex2 = 0;
  }
}

the serial output is ;

16:00:00.064 -> Serial 1
16:00:00.064 -> 7F 7E BE FE BE EA F2
16:00:00.583 -> Serial 1
16:00:00.583 -> 7F 7E BE FE BE EA F2
16:00:01.102 -> Serial 1
16:00:01.102 -> 7F 7E BE FE BE EA F2
16:00:01.574 -> Serial 1
16:00:01.574 -> 7F 7E BE FE BE EA F2
16:00:02.092 -> Serial 1
16:00:02.092 -> 7F 7E BE FE BE EA F2
16:00:02.611 -> Serial 1
16:00:02.611 -> 7F 7E BE FE BE EA F2
16:00:03.086 -> Serial 1
16:00:03.086 -> 7F 7E BE FE BE EA F2

So the inverter message is being sent onto the battery RS485 bus but for some reason the battery still does not respond.

Perfect !

But it does respond when you connect them directly ? That is a little weird.
Just to fully exclude a hardware issue, how about you connect RO of the incoming unit, and DI of the outgoing unit, bypassing the MCU's UART, while keeping the transceivers in the proper mode. (just in case you can also just connect 1 DE/!RE to GND and the other to VCC )
What kind of units are you using ? is it the PCB type ones that come 'ready made' ? because if so, you may need to modify them since usually they all have 'terminators' on them, and really only the last slave in the chain should have a terminator.

Sorry for the delay. Will try that. Yes they work fine when connected directly and I can view the communication ok.

This is PCB so will look at removing terminator.
RS485

I did as you suggested and connected RO of the incoming unit, and DI of the outgoing unit, and one DE/RE to ground and the other to + and it still didn't work. looking at the terminators I think I'm correct on leaving them on as it's 2 different bus. I don't have on on the sniffer.
I also tried swapping the receiver and transmitter and still got the same result.

Just trying to picture this in my head. You have an Arduino Mega with an inverter connected to Serial1 via an RS485 module, and a second RS485 module on Serial2 that talks to a battery?

Looking at your first sketch in post #11, you don't appear to be switching the RS485 module(s) from Rx to Tx, or Tx to Rx. If you have the RS485 module that talks to the battery in permanent Tx mode, then the battery can't use the RS485 bus to respond.

Also, if you don't control the RE pin, then you can end up hearing what you are transmitting.

@markd833 Sorry, not sure which post you are referring to as #11 is yours.

Just trying to picture this in my head. You have an Arduino Mega with an inverter connected to Serial1 via an RS485 module, and a second RS485 module on Serial2 that talks to a battery?
Yes.

I then have another mega that is sniffing the battery bus- it does see the communication from the inverter (that came via the other mega forwarder) but nothing back from the battery.

Sorry, somehow my brain transposed 11d and post #7!

I don't think you will see the battery response as your code in post #7 doesn't switch the battery bus RS485 module into receive mode. It's still driving the RS485 bus so your battery response is fighting for control of the bus.

As long as that is the case, there is not much point in looking at the code.

I thought you previously had the sniffer on the bus without the intermediate units, and there was no response from the battery, showing it is a one way street anyway. If that is not the case it is a whole different ball game.

No. The transmitter should not have a terminator. It is the start of the bus, not the end of it.
An extra 120R resistor on the bus at the beginning of it will influence the the reading on the bus beyond it.
The person who designed these units (and got them manufactured) made a mistake. On any bus there should never be more than 1 Terminator and it should only be on the slave farthest away from the master. On a bus used in a 2-way configuration there should not be any. In fact a terminator is normally added to the end of a long line bus to improve reception, but can normally be omitted. On the transmitter there should not be one. It is probably marked as R7, it is a 120R resistor connected between pin 6 & 7 on MAX485, which is the same as output A & B.

Just to verify, you had the sniffer on the bus while only the inverter and the battery were connected ? So if the battery would have responded you would have picked it up ?