failed to concat char str. Help plz

Guys, here is my code block:

//BMS Inittialization
void bmsInit() {
  /* Output report is a string with format <BDWFC;DevSL;Firmware_Ver;deltaT;OPS_Mode;> and this can be considered as a protocol data.
   * the < and > is for enclosing a single message. next are the various states of a specific item/gpio/etc.
   */

  strcat(bmsPayload, "<");

  strcat(bmsPayload, "BDWFC;");
  /*
  for (int i = 1; i < sizeof(devSL); i++) {
      //bms.print(devSL[i], DEC);
      strcat(bmsPayload, devSL[i]);
  }*/
  
  strcat(bmsPayload, devSL);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, fw_ver);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(deltaT, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(opsMode, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");

  strcat(bmsPayload, ">");

  bms.println(bmsPayload);
  memset(bmsPayload, 0, sizeof(bmsPayload));   // Clear contents of Buffer
  
}

and my variables are char bmsPayload[30] and devSL[10]

the output is coming perfectly except that the DevSL is not coming, though the Serial.print() inside for loop commented out, is working fine. devSL is holding a 9 digit value from eeprom (it’s there and it works shen did serial.print for sure.

confused… any help plz??

If you are having to use the following line to print each element of devSL that indicates to me it is binary values and not ASCII. What values does that print yield?

      //bms.print(devSL[i], DEC);

ASCII ‘0’-‘9’ are hex 0x30 - 0x39 or decimal 48 - 57. Also ensure that devSL is nul (’\0’) terminated.

for (int i = 1; Is there something special that the first character is ignored?

TheMemberFormerlyKnownAsAWOL:
for (int i = 1; Is there something special that the first character is ignored?

Whatcha wanna bet that first character is '\0'? Haha

thanks for very fast replies… first of all, I’m not at all using the bms.print(devSL[i], DEC);

Now, for (int i = 0; made as correction but still getting nothing…

strcat(bmsPayload, "<BDWFC;");
  
  for (int i = 0; i < sizeof(devSL); i++) {
      //bms.print(devSL[i], DEC);
      //strcat(bmsPayload, devSL[i]);
  }
  strcat(bmsPayload, devSL);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, fw_ver);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(deltaT, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(opsMode, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");

  strcat(bmsPayload, ">");

  bms.println(bmsPayload);
  memset(bmsPayload, 0, sizeof(bmsPayload));   // Clear contents of Buffer

and output is:

<BDWFC;a;1.5.a;4;1;>

but actually it should be <BDWFC;6513517460;1.5.a;4;1;>

I think you have 2 problems:

  1. devSL[10] should be declared as devSL[11]. If it is 10 digits then you must have an extra location for the terminating nul character ‘\0’
  2. <BDWFC;a;1.5.a;4;1;> is showing 9 unprintable characters. This confirms my theory that devSL elements are binary numbers and not ASCII digits. The last number you expect is a 0. That is acting as your terminating character for the strcat operation.

Again,

ASCII ‘0’-‘9’ are hex 0x30 - 0x39 or decimal 48 - 57. Every element of devSL must be between decimal 48 - 57 or they are NOT valid ASCII digits.

Put the following in place of your for loop:

  for (int i = 0; i < sizeof(devSL); i++) {
      Serial.print(devSL[i], DEC);
      Serial.print(" ");
  }
  Serial.println();

If this is not your output:

54 53 49 51 53 49 55 52 54 48 0

Then you have a problem.

well, code chunks here for clearing confusions and finding the bugs… This is latest till now…

#define deviceSLlength 10

char devSL[deviceSLlength]; //10 digit device serial

char bmsPayload[60];
//Generate device Serial number
void generate_devSL(unsigned int a){
  if (EEPROM.read(0) == '#') {
    for (int i = 0; i < a; i++) {
      devSL[i] = EEPROM.read(i+1);
    }
  } 
  
  else {
    digitalWrite(systemStateLED, HIGH);
    debug.println(F("Generating random Serial since first boot."));
    for (int i = 0; i < a; i++) {
      devSL[i] = TrueRandom.random(9);
      debug.print(devSL[i], DEC);
      EEPROM.update(i+1, devSL[i]);
      delay(10);
    }
    EEPROM.update(0, '#');
    debug.println();
    factoryReset();
    digitalWrite(systemStateLED, LOW);
  }
}
debug.print(F("Hardware Serial: "));
  for (int i = 0; i < sizeof(devSL); i++) {
      debug.print(devSL[i], DEC);
  }
  debug.println();

Above prints: Hardware Serial: 6513517460

And then the function is:

//BMS Inittialization
void bmsInit() {
  /* Output report is a string with format <BDWFC;DevSL;Firmware_Ver;deltaT;OPS_Mode;> and this can be considered as a protocol data.
   * the < and > is for enclosing a single message. next are the various states of a specific item/gpio/etc.
   */

  strcat(bmsPayload, "<BDWFC;");
 

  strcat(bmsPayload, devSL);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, fw_ver);
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(deltaT, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");
  strcat(bmsPayload, itoa(opsMode, itoaCStr, 10));
  memset(itoaCStr, 0, sizeof(itoaCStr));   // Clear contents of Buffer
  strcat(bmsPayload, ";");

  strcat(bmsPayload, ">");

  bms.println(bmsPayload);
  memset(bmsPayload, 0, sizeof(bmsPayload));   // Clear contents of Buffer
}

And this gives: <BDWFC;a;1.5.a;4;1;>

So, I’m just suspecting, devSL is holding bytes from eeprom parhaps? instead of char?

Update: making devSL[11] makes output as:

Hardware Serial: 6513517460-1

OK, so far solved it... Here was the issue:

I was using truerandom function to generate my Serial Numbers. I gave input as TrueRandom.random(10) which was generating values from 0 to 9 of the ASCII table. (well, it generates integer values but putting that into char array was considered as ascii value. [I still need to study it more to figure out why and to make a best use case like using int values directly. But for now purpose served.]

Changing it to TrueRandom.random(49, 58); did the magic and hence it was generating 0-9 decimal's ascii codes. putting it into char was solving the issue.

As said, still need the deep study on how to make it more perfect, but for now, purpose solved.

If I have to keep it like this, then I prefer to generate serial numbers not just Integers of length 10 digits, but alpha-numeric with 10 char length. Like those 1SD34CQX456 type of thing.... rather that I prefer and if you guys can advise on that (surely usign the same methods)

But above all, thanks for your prompt response and some hints, which helped me to solve the issue. (Now have to correct every of the project i ever made with this method... yuk!!)

The other alternative would been to add 0x30 to each byte in devSL. Also you need to make sure devSL[10] has a 0 in it! For the string termination.

FYI, if you had posted your entire sketch help would have come much more quickly because we would have seen how you were populating devSL. This is why the forum guidelines suggest you post your ENTIRE sketch.