Arduino SerialPassthrough doesn't work

Hi, i'm trying to put an ESP32 (later it will be an Arduino) between an ebike controller and its display.

I first tried to listen their communication and it was all ok. The protocol is UART, i think it is at 9600 baud 8N1, but i'm not sure because i can see something readable (but different in from 9600) until 19200 baud. So, the first question is: how can i be sure of the protocol in use?

Then, i tried to put the ESP32 between the two controllers, TX_controller to RX1, RX_controller to TX1, TX_display to RX2, RX_display to TX2. But it doesn't work.

The code i the default SerialPassthrough:

#define RXD1 18
#define TXD1 19
#define RXD2 16
#define TXD2 17


void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  //Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() { //Choose Serial1 or Serial2 as required
  if (Serial1.available()) {      // If anything comes in Serial (USB),
    Serial2.write(Serial1.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (Serial2.available()) {     // If anything comes in Serial1 (pins 0 & 1)
    Serial1.write(Serial2.read());   // read it and send it out Serial (USB)
  }
}

The bike controller is the XC836, while i don't know the display controller.

nikten: i'm trying to put an ESP32 (later it will be an Arduino)

That seems a strange order of things. I imagine it would be easier to start with an Arduino - particularly a Mega which has 4 HardwareSerial ports

I first tried to listen their communication and it was all ok. The protocol is UART, i think it is at 9600 baud 8N1, but i'm not sure because i can see something readable (but different in from 9600) until 19200 baud. So, the first question is: how can i be sure of the protocol in use?

I find that very confusing. It reads as if you intended to write "can't see" and then I wonder what "it was all OK" refers to.

...R

Robin2: I find that very confusing. It reads as if you intended to write "can't see" and then I wonder what "it was all OK" refers to.

This is what i read if i set 9600 baud:

011401020380010104010500640d018600004c37
011401020380010104010500640d018600004c37
011401020380010104010500640d018600004c37
011401020380010104010500640d018600004c37
...

and this is what i read if i set 19200 baud:

06606000181e0006066006660060668006780000e0987ef8
06606000181e0006066006660060668006780000e0987ef8
06606000181e0006066006660060668006780000e0987ef8
06606000181e0006066006660060668006780000e0987ef8
...

I putted my USB to TTL to read with my python script what is the behavior of Arduino (i use a Mega now ;) ). I noticed that when i turn on the display, it sends 0x00 to the arduino. But Arduino don't sends this to controller. Can it be a problem of Serial.available()? That it returns false with a 0x00 incoming?

nikten: Can it be a problem of Serial.available()? That it returns false with a 0x00 incoming?

No.

You need to post the pair of programs that you are talking about so we can see the details.

...R

Sorry, i didn't understand what program should i send to you.

nikten: Sorry, i didn't understand what program should i send to you.

You mentioned a Python program and an Arduino program in Reply #3 - those are what I was referring to.

...R

nikten:
Can it be a problem of Serial.available()? That it returns false with a 0x00 incoming?

If you receive the byte 0x00, Serial.available() will return a non-zero number (i.e. “you’ve got mail”). That shouldn’t be the problem…

Ok, i solved it! Thank you anyway!

byte zero = 0x00;
byte dati1[14];
byte dati2[20];

void setup() {
  Serial1.begin(9600, SERIAL_8N1, 18, 19);
  Serial2.begin(9600, SERIAL_8N1, 16, 17);
}

void loop() {
  bool done = false;
  if(Serial1.available() == 1){
    if(Serial1.read() == zero){
      Serial2.write(zero);
    }
  }
  
  if (Serial1.available()==14) {
    for(int i=0;i<14;i++){
      dati1[i] = Serial1.read();
    }
    Serial2.write(dati1,sizeof(dati1));
  } else if(Serial1.available()>14) {
    while(Serial1.available()){
      Serial1.read();
    }
  }

  if(Serial2.available() == 1){
    if(Serial2.read() == zero){
      Serial1.write(zero);
    }
  }

  if (Serial2.available()==20) {
    for(int i=0;i<20;i++){
      dati2[i] = Serial2.read();
    }
    Serial1.write(dati2,sizeof(dati2));
  } else if(Serial2.available()>20) {
    while(Serial2.available()){
      Serial2.read();
    }
  }

  delay(50);
}