Trouble storing Serial Data from Battery Management System

Hi Everyone,

I’m building a large battery for solar energy storage, and trying to use an Arduino Nano to read data from the battery management system through the UART port. So far, I’ve successfully initiated the serial connection with the BMS, sent it a request for information, read that information, and printed it to the serial monitor. No small feat for a rube like myself… Where I’m struggling is getting the BMS output saved to a string (or array) so that i can then process it.

Here’s the code I’ve used to successfully get temperature readings from the BMS:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

int  inByte;       //Variable for incoming serial values

void setup() {
  Serial.begin(115200);     // Open serial communication to computer
  Serial.println();
  Serial.println("Serial Started");
  mySerial.begin(115200);  // Open serial communication to BMS
  delay(1000);
    mySerial.write(0xAA);  //Send temperature readings request to BMS
    mySerial.write(0x1B);
    mySerial.write(0x3F);
    mySerial.write(0x1B);
}

void loop () {
    if (mySerial.available() > 0) {
    inByte = mySerial.read();         //Reads individual values from softwareSerial buffer
    Serial.print(inByte);             //Prints the inByte values to serial monitor
    Serial.print(" ");                //Add a space
    }

The output of this program looks like this (temperature values from three different sensors are highlighted in bold):
14:07:27.192 → Serial Started
14:07:28.178 → 170 27 6 167 1 201 0 208 0 248 147

And here’s the code I’m using to get this data ‘stored’ in an array for further processing (mainly based on this).

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);          // RX, TX

char  inString[100];      //String for incoming serial values

void readData (char *strArray) {
  int i = 0;                            //define index value, set to 0
  if(mySerial.available() > 0) { 
    while(mySerial.available() > 0) {
      strArray[i] = mySerial.read();    //write serial buffer content to the string
      i++;                              //increase index value by 1
    }                  
  }
}

void printData (char *strArray) {
  int i = 0;                            //define index value, set to 0
  if(strArray[i] != 0) { 
    while(strArray[i] != 0) {
      Serial.print( strArray[i] );      //print the n = i value of the string
      i++;                              //increase index value by 1
    }                  
  }
}


boolean isStringEmpty(char *strArray) {  //utility function to know wither an array is empty or not
     if (strArray[0] == 0) {
         return true;
     } else {
         return false;
     }
}


void setup() {
  Serial.begin(115200);                 // Open serial communication to computer
  Serial.println();
  Serial.println("Serial Started");
  mySerial.begin(115200);               // Open serial communication to BMS
  delay(1000);
    mySerial.write(0xAA);               //Send request for temp data to BMS
    mySerial.write(0x1B);
    mySerial.write(0x3F);
    mySerial.write(0x1B);
}

void loop() {
  readData(inString);
  
  if( isStringEmpty(inString) == false) { 
      printData(inString);
      Serial.println();
  }
  delay(5000);
}

I’ve tried about a hundred different variants on this, with absolutely no success. In all cases, I just get a bunch of random characters (mostly backwards question marks…).

Any help would be greatly appreciated!

RESOLVED: Well, I figured out my problem...I was defining the string as a 'char' type, rather than an 'int' type. Simply changing all the instances of 'char' to 'int' in the code above resolved the issue.

Thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.