Go Down

Topic: Forwarding serial communication between two hardware serials (Mega 2560)? (Read 605 times) previous topic - next topic

joq3

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:

Code: [Select]
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:
Quote
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?

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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:
https://robotdyn.com/mega-2560-pro-embed-ch340g-atmega2560-16au.html

septillion

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.

Sorry, did my best to show it off in a good way.
Pen(cil) and paper is often even easier and better to understand.

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?

I spoke to RobotDyn, and they confirmed that 5V was going to work with my board:
https://robotdyn.com/mega-2560-pro-embed-ch340g-atmega2560-16au.html
5V will indeed work with your board. Only NOT when you connect it to Vin ;) 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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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 ;) 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!

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).

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.

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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:
https://fasterbikes.eu/en/matching-wires/198-bafang-bbs-programming-cable-usbmatching-wires.html
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.

septillion

This is the type of cable used to program the ebike with:
https://fasterbikes.eu/en/matching-wires/198-bafang-bbs-programming-cable-usbmatching-wires.html
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.

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.

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.

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 ;) 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?

Code: [Select]
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());
  }
}
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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 ;) 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?

Code: [Select]
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());
  }
}


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.

septillion

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.

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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!

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:
Code: [Select]
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).
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

joq3

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:
Code: [Select]
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).

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?

septillion

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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up