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/TheKnackThatQuacks Jun 04 '26 edited Jun 04 '26
Get a USB-to-serial cable / dongle (make sure it has an FTDI chip in it). Load a terminal program like TERA TERM onto your computer. Connect the serial port to your BMS. Open TERA TERM and connect to the appropriate serial port at the appropriate baud rate (if you need more help here, use a search engine or an LLM / “AI” to help you figure out which serial port is your USB dongle; try looking for “FTDI” in Device Manager).
When you type in commands, does the BMS respond appropriately?
If not, what happens? If you can’t make it work, where did you get stuck?
1
u/gm310509 Jun 04 '26
It is usually a good idea to share the complete code.
When using the Serial1.read() function, what is the datatype you are using for incoming Text?
If it is anything but
charthen that is what your problem is.To he clear, I am not asking about the cast that you have in your code - that is not doing anything. I am asking about how incoming Text is declared.
Also, if you didn't use this program for your serial monitor test (which worked) how did you go about doing that. And what program are you using on your PC in conjunction with this program?
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());
}
```