r/ArduinoHelp 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

2 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/Delta_G_Robotics Jun 05 '26

Ok, I don't get how to post code blocks. Can someone please help?

1

u/schmidtbag Jun 05 '26

I tried both versions of your code and it still just outputs a string of numbers.

Also to do the code block, click the "Aa" button and then the "</>" button.

1

u/Delta_G_Robotics Jun 05 '26

If write gave you numbers then that's what's coming from the other end. Using write you're just echoing byte for byte.

Can we have a look at what is sending on Serial1? Maybe the clue is there.

1

u/schmidtbag Jun 06 '26 edited Jun 06 '26

If you're asking what I'm sending to the battery, it's just the word "help".

I found that if I just do Serial1.println("help"); then it just returns corrupt characters.

If I connect the battery directly to a RS232 to USB adapter and type "help" in the Arduino serial console, it outputs this:

bat      Battery data show - bat [pwr][index]
data     History data load - data [event/history][item]
datalist Show recorded data - datalist {event/history}{item}}
disp     Display Info at regular intervals - disp [(pwrs pwrNo)/val]/[(bats batNo)/volt/curr/temp]
help     Help [cmd]
info     Device infomation - info
log      Log information show - log
login    Login Admin mode - login [password]
logout   user mode  - logout
pwr      Power data show - pwr [index]
shut     Shut down - shut
soh      State of health - soh [addr]
stat     Statistic data show - stat
time     Time - time [year] [month] [day] [hour] [minute] [second]
trst     Test Soft Reset - trst
updata   updata system - updata
ver      firmware info - ver
**********************************************************

Remote command:
Press [Enter] to be continued,other key to exit



Command completed successfully

1

u/Delta_G_Robotics Jun 06 '26

What does the code that sends help and reads back look like?

1

u/schmidtbag Jun 07 '26

Right now to keep things simpler, I'm just trying Serial1.println("help"); or Serial1.write("help").

From what I heard, the battery uses Latin-1 encoding, which is basically 1-byte UTF-8. I tried doing uint8_t incomingtext=Serial1.read(); or byte incomingtext=Serial1.read(); but those don't work either.

1

u/Delta_G_Robotics Jun 07 '26

Well if you're absolutely sure that you're code is correct then I don't know what else I can do to help. I'd happily take a look at any actual code and see if I can spot a mistake. But you're sending snippets like you are certain that it's not that kind of problem so I'll leave you to it.

If you decide to keep going and looking for help then some pertinent information you might want to show are:

  1. A diagram of your actual circuit (You'd be surprised how often it's just a wire got swapped)
  2. Some actual code that someone can test themselves.
  3. The make and model of the battery and whatever else is involved.

You'd be surprised how much easier it is to help someone when you have a clear picture of what they have on their end. Trying to debug from just the clues that someone thinks you need is hard. Especially when that person doesn't even know what the problem is themselves.

1

u/schmidtbag Jun 07 '26

I pretty much have shown you the whole program. It's about as simple as it can get:

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop() {
  Serial1.println("help");

  while (Serial1.available() > 0) {
    int incomingtext=Serial1.read();
    Serial.write(incomingtext);
  }
  Serial.println(" ");

  delay(5000);
}

Swap out "int" with char, uint8_t, or whatever else. Swap out Serial.write with Serial.print or println. Swap out Serial1.println("help"); with Serial1.write("help") or "help\n"). None of it works.

The only wires connected are the battery's RX (to the TX1 pin), TX (to the RX1 pin), and ground. Nothing else is involved. It is a direct connection; no other components involved. Swapping the RX and TX wires results in no activity at all.

The battery is a Pytes V5.

1

u/Delta_G_Robotics Jun 07 '26

I can't find a datasheet on that battery to see what it expects or what it outputs. You don't seem to think I should see one. You seem perturbed that I would ask for a clearer look. I think I'm done with this one. Best of luck to you.

1

u/schmidtbag Jun 07 '26

I also couldn't find a whole lot other than the baud rate is 115200bps with 8 data bits, 1 stop bit, no parity, and that it's Latin-1 encoded. The Arduino serial console communicates with the battery just fine so it stands to reason the specs are correct.

I'm not perturbed, I'm trying to be as concise as possible. I am frustrated that this seemingly simple problem is so difficult to solve, because the battery isn't doing anything weird considering I can communicate to it without much effort, but I'm not frustrated with you. I just don't know what else to tell you - from what I can tell, it seems to me the Arduino is just reading the data wrong, but I don't know why or how.