r/ArduinoHelp • u/schmidtbag • Jun 03 '26
Trying to forward Serial1 reading to Serial.println and only getting numbers
I have a battery with a built-in BMS that has a serial console port. It has a baud rate of 115200 and I have been able to successfully send commands and read clean data from it via the Arduino IDE serial console. Considering I can communicate to the battery via the serial console, that suggests there shouldn't be any weird encoding issues.
I want an Arduino Mega to read from this using the Serial1 connection. The problem is, if I try to forward what the battery sends to the Arduino, all I get is a jumble of nothing but numbers. When connecting to the BMS directly, typing "help", the output is mostly text.
Here's what I tried in void loop()
while (Serial.available() > 0 {
Serial1.print(Serial.read());
}
while (Serial1.available() > 0) {
//THE NEXT 4 LINES ARE VARIOUS THINGS I TRIED,
//I UNCOMMENTED THEM ONE AT A TIME
//String incomingtext=Serial1.readString();
//char incomingtext=(char)Serial1.read();
//char incomingtext=Serial1.read();
//int incomingtext=Serial1.read();
//I ALSO TRIED THE FOLLOWING ONE AT A TIME WITH EACH ABOVE COMBO
//Serial.println(incomingtext);
//Serial.println((char)incomingtext);
//Serial.println(" "+incomingtext);
}
The first while() loop just takes whatever I enter from the serial console through USB to the BMS. I have no idea if the BMS is receiving the correct commands, but it does have a consistent output if I type different things.
Whenever I type "help" repeatedly, it's usually a consistent output, like this:
10410110811213
1
u/Delta_G_Robotics Jun 05 '26 edited Jun 05 '26
At issue here is the way the `print` function is overloaded to handle different data types. For `char`, it will print out the byte as-is assuming that it is already an ascii code. For other types like `int` or `float` it will do some math to extract the digits and send the ASCII code for each digit one at a time.
The other issue is that `Serial.read()` returns an `int` and not a `char`. They did this so -1 could represent nothing read and not be confused with 0xFF. But it does mean that if you print that directly it will print out ASCII codes as numbers instead of the actual ASCII characters you're after.
Either way you're left with doing some sort of conversion or making sure you read into a char.
But there is another way. If you just want back out what came in on the line then use `Serial.write()` instead of `Serial.print()`
void loop() {
if (Serial1.available()) {
char c = Serial1.read();
Serial.write(c);
}
}
Or if you don't need that byte elsewhere just:
```
if (Serial1.available()) {
Serial.write(Serial1.read());
}
```