Forwarding serial communication between two hardware serials (Mega 2560)?

Hi,

I got a RobotDyn Mega 2560 Pro Mini (Embed). I have started a project where I want to communicate/program my e-bike with the Arduino. But I ran in to issues instantly, when just trying to forward the serial communication via the Arduino.
This is the code I am using:

void setup() {
  Serial2.begin(9600);    // Display DP-C18 @TX16 RX17
  Serial3.begin(9600);    // Bafang Ultra Motor @TX14 RX15
}

void loop() {
  readDisplay();
  readController();
}

void readController() {
  if (Serial3.available()) {
    Serial2.write(Serial3.read());
  }
}

void readDisplay()  {
  if (Serial2.available()) {
    Serial3.write(Serial2.read());
  }
}

As you can see I have baudrate set to 9600, this is because when connecting the bike to my PC via a programming cable to a COM-port I monitored this communication and found out it was 9600 baudrate. I also found out this when monitoring:

StopBits - 0 (1 stop bit)
Parity - 0 (NO_PARITY)
WordLength - 8

That should mean that the default SERIAL_8N1 should work together with baudrate 9600.

Still I get a communication error from the bike itself, which means the communication is not forwarded properly between the motor and display (this is where the arduino is placed, on the TX/RX from the motor to the display).

I have attached an image showing how it is wired.

(Click it to show it in full size).

Am I missing anything obvious here?

And from the info I have been able to find the Green wire is the Motor TX and the Yellow wire is Motor RX. This should mean that the Display TX is yellow and Display RX is green right?

Code looks fine.

Both devices are 3V3? Because you say COM-cable. Can you be more specific? Because a real COM-port on a PC is +/- 15V or there about.

Can't say if you connected it the right way. Especially with a Fritzing mess. But assuming it works fine if you connect yellow to yellow and green to green without the Arduino it seems right for the serial.

BUT if the red wire is really 5V is should NOT go to Vin. Vin can only be used for >7V.

septillion:
Code looks fine.

Both devices are 3V3? Because you say COM-cable. Can you be more specific? Because a real COM-port on a PC is +/- 15V or there about.

Can't say if you connected it the right way. Especially with a Fritzing mess. But assuming it works fine if you connect yellow to yellow and green to green without the Arduino it seems right for the serial.

BUT if the red wire is really 5V is should NOT go to Vin. Vin can only be used for >7V.

When I talked about the COM-cable, this was when I connected my bike to my computer (no Arduino involved). Just to check what baudrate and communication-config it used when talking to the bike.
The programming cable uses a CH340 to communicate with the bike.

Sorry, did my best to show it off in a good way.
Yes, yellow to yellow and green to green works great.
The red wire is 5V output from the bike itself (I take this from the USB-port of the bike).
I spoke to RobotDyn, and they confirmed that 5V was going to work with my board:

joq3:
The programming cable uses a CH340 to communicate with the bike.

That's what I was looking for, aka no real COM and just 5V/3V3 serial.

joq3:
Sorry, did my best to show it off in a good way.

Pen(cil) and paper is often even easier and better to understand.

joq3:
The red wire is 5V output from the bike itself (I take this from the USB-port of the bike).

Okay, but you are sure the serial voltage levels are 3V3?

joq3:
I spoke to RobotDyn, and they confirmed that 5V was going to work with my board:
Mega 2560 PRO (Embed) CH340G/ATmega2560-16AU - Robotdyn

5V will indeed work with your board. Only NOT when you connect it to Vin :wink: Safest option, use the USB to power it. Being it from the PC or the bike doesn't matter as long as you keep the GND connection.

septillion:
That's what I was looking for, aka no real COM and just 5V/3V3 serial.
Pen(cil) and paper is often even easier and better to understand.
Okay, but you are sure the serial voltage levels are 3V3?
5V will indeed work with your board. Only NOT when you connect it to Vin :wink: Safest option, use the USB to power it. Being it from the PC or the bike doesn't matter as long as you keep the GND connection.

No I just assumed the serial voltage is 3V3. How can I check that? Is it possible to measure with a multimeter on the serial and the ground without shorting anything?

Why doesn't it work on VIN? And if I don't use USB to power it, where do I power it with 5V?

Thank you!

Assumptions are the mother of all....

Esiest would be a scope. But does it send data all the time? The serial line should be pulled HIGH when idle so a DMM will probably read close to 5V (or at least above 3V3) when it's 5V serial and below 3V3 when it's that.

But, you also probed it with a CH340. Most breakouts have a selector for 5V or 3V3, does yours as well? If not, do you have a link to the one used? Because that could as well be 5V (which would have killed the circuit if it was 3V3).

joq3:
Why doesn't it work on VIN?

Because that connects to a voltage regulator that DOES expect a higher voltage on it's input than what it tries to regulate to.

joq3:
And if I don't use USB to power it, where do I power it with 5V?

Why not use the USB? USB on the board, USB on the bike, can't be more easy! But if you really want to, connect the 5V to the 5V pin of the board. But be sure to NOT connect the USB to a PC if you do.

Because the schematic in the link you posted shows no separation between the 5V line and the 5V of the USB. So don't connect the USB if you supply voltage to the 5V pin or even Vin.

septillion:
Assumptions are the mother of all....

Esiest would be a scope. But does it send data all the time? The serial line should be pulled HIGH when idle so a DMM will probably read close to 5V (or at least above 3V3) when it's 5V serial and below 3V3 when it's that.

But, you also probed it with a CH340. Most breakouts have a selector for 5V or 3V3, does yours as well? If not, do you have a link to the one used? Because that could as well be 5V (which would have killed the circuit if it was 3V3).
Because that connects to a voltage regulator that DOES expect a higher voltage on it's input than what it tries to regulate to.
Why not use the USB? USB on the board, USB on the bike, can't be more easy! But if you really want to, connect the 5V to the 5V pin of the board. But be sure to NOT connect the USB to a PC if you do.

Because the schematic in the link you posted shows no separation between the 5V line and the 5V of the USB. So don't connect the USB if you supply voltage to the 5V pin or even Vin.

This is the type of cable used to program the ebike with:

Which uses a "USB to UART PL2303TA". And from what I understand that is 3V3?

Aha, I think I understand. But I must have misunderstood the 5V pin. Though that was only an output from the voltage regulator and not power input.
Do you think that could be the issue? That I have been powering it with VIN on 5V, and that makes my Arduino not function properly and forwarding the data?

I pull the power from the USB on the bike, but not from the port itself. I have spliced the wire going to the USB. That's why I rather not use USB.

joq3:
This is the type of cable used to program the ebike with:
FasterBikes.eu | Programming cable for your Bafang Motor.
Which uses a "USB to UART PL2303TA". And from what I understand that is 3V3?

First you say CH340, now it's a PL2303.... But after a quick look it indeed looks like 3V3 or lower. BUT that is just guessing without more specs of that cable / measuring. As they also link to "fixes" it definitely does not use the official PL2303.

joq3:
Aha, I think I understand. But I must have misunderstood the 5V pin. Though that was only an output from the voltage regulator and not power input.

Can be both, but you can only use one power input (5V, Vin or USB) at the same time.

joq3:
Do you think that could be the issue? That I have been powering it with VIN on 5V, and that makes my Arduino not function properly and forwarding the data?

It is possible depending on what happened. If it cause the Arduino to be powered with less then 5V the serial levels are lower as well.

BUT, if you indeed connected USB and Vin (with 5V) it's also possible you blow the voltage regulator. Annoying (but not really a problem) and that will not cause any problem with serial.

joq3:
I pull the power from the USB on the bike, but not from the port itself. I have spliced the wire going to the USB. That's why I rather not use USB.

Splice it to a cut USB cable :wink: But whatever you do, only connect a single power supply.

Thing I just thought about, why don't you echo the data to the computer and see if you see data?

void setup() {
  Serial2.begin(9600);    // Display DP-C18 @TX16 RX17
  Serial3.begin(9600);    // Bafang Ultra Motor @TX14 RX15
  Serial.begin(115200); //Computer, fast :)
}

void loop() {
  readDisplay();
  readController();
}

void readController() {
  if (Serial3.available()) {
    //PC
    Serial.print("C: ")
    Serial.println(Serial3.peek());
    
    //through
    Serial2.write(Serial3.read());
  }
}

void readDisplay()  {
  if (Serial2.available()) {
    //PC
    Serial.print("D: ")
    Serial.println(Serial2.peek());
    
    //through
    Serial3.write(Serial2.read());
  }
}

septillion:
First you say CH340, now it's a PL2303.... But after a quick look it indeed looks like 3V3 or lower. BUT that is just guessing without more specs of that cable / measuring. As they also link to "fixes" it definitely does not use the official PL2303.
Can be both, but you can only use one power input (5V, Vin or USB) at the same time.
It is possible depending on what happened. If it cause the Arduino to be powered with less then 5V the serial levels are lower as well.

BUT, if you indeed connected USB and Vin (with 5V) it's also possible you blow the voltage regulator. Annoying (but not really a problem) and that will not cause any problem with serial.
Splice it to a cut USB cable :wink: But whatever you do, only connect a single power supply.

Thing I just thought about, why don't you echo the data to the computer and see if you see data?

void setup() {

Serial2.begin(9600);    // Display DP-C18 @TX16 RX17
  Serial3.begin(9600);    // Bafang Ultra Motor @TX14 RX15
  Serial.begin(115200); //Computer, fast :slight_smile:
}

void loop() {
  readDisplay();
  readController();
}

void readController() {
  if (Serial3.available()) {
    //PC
    Serial.print("C: ")
    Serial.println(Serial3.peek());
   
    //through
    Serial2.write(Serial3.read());
  }
}

void readDisplay()  {
  if (Serial2.available()) {
    //PC
    Serial.print("D: ")
    Serial.println(Serial2.peek());
   
    //through
    Serial3.write(Serial2.read());
  }
}

Sorry, got it mixed up. I'm very new to this.
Just connected the +5V to the 5V on the Arduino and tried again on the ebike.
Still get communication error from the ebike.

The thing about echoing the data to a PC I have already tried, but instead of a PC I used a HC-05 connected to Serial1 (connected to the HC-05 with an Android device using a terminal).
Sadly it did not show any data at all.

joq3:
Just connected the +5V to the 5V on the Arduino and tried again on the ebike.

Just connect the GND and power the Arduino via the PC for now.

joq3:
The thing about echoing the data to a PC I have already tried, but instead of a PC I used a HC-05 [...]

Jeeeeeezzz, just keep it simple! Just plug the damn thing in your PC, take baby steps and just echo it to the PC.

The only thing I can think of now is that the TX and RX are mixed up on both connections.

When looking at images like this: https://electricbike.com/forum/filedata/fetch?id=77041&d=1540926770
I can see that the Green and Yellow are shown as RXD and TXD on both the controller/motor side and display side. So maybe it is mixed up on my Arduino, and that is why it is not forwarding the data.

Can I try to switch the TX and RX on each serial?
If this is incorrect, could it do any damage to the bike when running the code in my main post?

Thank you!

Tx and Rx can be confusing indeed because it's ambiguous. I rather use Txo (transmit output) and Rxi (receive input).

But I can't help you with the right pins in your setup. Connecting a Rxi to another Rxi is no problem. But a Txo to a Txo can give you a short.

So, like I said, start by echoing to the PC (without BlueTooth bs). What I would do is:

  • Only cut a single data line (yellow or green)
  • Display end to Rx2
  • Controller end to Rx3
  • Run a program like:
void setup() {
  Serial2.begin(9600);    // Display DP-C18 @TX16 RX17
  Serial3.begin(9600);    // Bafang Ultra Motor @TX14 RX15
  Serial.begin(115200); //Computer, fast :)
}

void loop() {
  readDisplay();
  readController();
}

void readController() {
  if (Serial3.available()) {
    Serial.print("C/3: ")
    Serial.println(Serial3.read());
  }
}

void readDisplay()  {
  if (Serial2.available()) {
    Serial.print("D/2: ")
    Serial.println(Serial2.read());
  }
}

Now you should only see data on Serial2 or on Serial3. The one you see data on is connected right (aka the Arduino Rxi is connected to the device Txo).

septillion:
Tx and Rx can be confusing indeed because it's ambiguous. I rather use Txo (transmit output) and Rxi (receive input).

But I can't help you with the right pins in your setup. Connecting a Rxi to another Rxi is no problem. But a Txo to a Txo can give you a short.

So, like I said, start by echoing to the PC (without BlueTooth bs). What I would do is:

  • Only cut a single data line (yellow or green)
  • Display end to Rx2
  • Controller end to Rx3
  • Run a program like:
void setup() {

Serial2.begin(9600);    // Display DP-C18 @TX16 RX17
  Serial3.begin(9600);    // Bafang Ultra Motor @TX14 RX15
  Serial.begin(115200); //Computer, fast :slight_smile:
}

void loop() {
  readDisplay();
  readController();
}

void readController() {
  if (Serial3.available()) {
    Serial.print("C/3: ")
    Serial.println(Serial3.read());
  }
}

void readDisplay()  {
  if (Serial2.available()) {
    Serial.print("D/2: ")
    Serial.println(Serial2.read());
  }
}




Now you should only see data on Serial2 or on Serial3. The one you see data on is connected right (aka the Arduino Rxi is connected to the device Txo).

Good idea, just to verify, but each end of one cable to RX2 and RX3. Should there be any resistors connected to ground on any side? Or just straight through with no resistors?

Straight trough. I (/ you) don't expect voltages higher than 5V, maybe lower but that's fine. Up to around 3V the Serial reception should work. Worst that can happen is that the Rxi that's connected to the Rxi is floating but don't expect serial to really react on that for a small test.

Hey! I'm also working on this, trying to speed up the serial communication between display and controller. Other then joq3, I'm using a Bafang BBSHD + DPC18 Display. When it comes to updating the display's speedometer, it's pretty slow and takes approx. 2-3 seconds.. I'm using pretty much the same code and I've no clue where this huge delay comes from..

void setup() {
  Serial1.begin(1200);    //BBSHD controller    @RX18 TX19
  Serial2.begin(1200);    //Display DP-C18      @TX16 RX17
}

void loop() {
  readDisplay();
  readController();
}

void readDisplay()  {
  if (Serial2.available()) {
    Serial1.write(Serial2.read());
  }
}

void readController() {
  if (Serial1.available()) {
    Serial2.write(Serial1.read());
  }
}