Getting strange chars in Serial output from time to time

I have some code (below) where I am taking a HEX value represented by a char array and converting it to an unsigned long representation of the value. Based on all my debugging, this part of the process works fine, both the generation of the char array and creation of the unsigned long appear to be correct.

I then have a sprintf that generates a buffer that I ouptut with Serial.println(). Sometimes the output is clean and other times I have random characters. Any suggestions why this is occuring?

            // A9 02 0D 00 00 00 03 00 01 00 00 02 00 06 00 00 A0 
       
            char strkal[10] = "";
            sprintf(strkal, "0x%02X%02X%02X", notifydata[4], notifydata[5], notifydata[6]);
            unsigned long statkal = strtol(strkal, NULL, 0);

            char strdist[10] = "";
            sprintf(strdist, "0x%02X%02X", notifydata[7], notifydata[8]);
            unsigned long statdistance = strtol(strdist, NULL, 0);

            char strstep[10] = "";
            sprintf(strstep, "0x%02X%02X%02X", notifydata[9], notifydata[10], notifydata[11]);
            unsigned long statstep = strtol(strstep, NULL, 0);

            char strtime[10] = "";
            sprintf(strtime, "0x%02X%02X", notifydata[12], notifydata[13]);
            unsigned long stattime = strtol(strtime, NULL, 0);
            
            char buff[100] = "";
            sprintf(buff, "Data - Kcal: %lu  Time:%lu  Distance:%lu  Step: %lu", statkal, stattime, statdistance, statstep);
            Serial.println(buff);

Below is a screenshot of what the output looks like: debug

strstep buffer overflow likely. be more generous and make room for the trailing null char

I ran this code with random values and didn't get a single glitch. That tends to indicate that the problem is elsewhere in your code. Perhaps if you provided the rest of the code, someone might spot an error.

void setup()
{
  Serial.begin(115200);
  delay(200);
}

void loop()
{
  byte notifydata[18];

  for (int i = 0; i < 18; i++)
  {
    notifydata[i] = random(256);
  }

  char strkal[10] = "";
  sprintf(strkal, "0x%02X%02X%02X", notifydata[4], notifydata[5], notifydata[6]);
  unsigned long statkal = strtol(strkal, NULL, 0);

  char strdist[10] = "";
  sprintf(strdist, "0x%02X%02X", notifydata[7], notifydata[8]);
  unsigned long statdistance = strtol(strdist, NULL, 0);

  char strstep[10] = "";
  sprintf(strstep, "0x%02X%02X%02X", notifydata[9], notifydata[10], notifydata[11]);
  unsigned long statstep = strtol(strstep, NULL, 0);

  char strtime[10] = "";
  sprintf(strtime, "0x%02X%02X", notifydata[12], notifydata[13]);
  unsigned long stattime = strtol(strtime, NULL, 0);

  char buff[100] = "";
  sprintf(buff, "Data - Kcal: %lu  Time:%lu  Distance:%lu  Step: %lu", statkal, stattime, statdistance, statstep);
  Serial.println(buff);

  delay(500);
}

Why do you go through ASCII conversion and reconversion instead of just taking the values?

uint32_t statkal = (((((uint32_t)notifydata[4]) << 8) + notifydata[5]) << 8) + notifydata[6];

Thanks for the suggestions, Whandall I figured there was likely an easier way but had not had a chance to investigate using bitshifting. I have updated my code below. As an aside, there is a lot of code so was hesitating posting the whole thing.

            // A9 02 0D 00 00 00 03 00 01 00 00 02 00 06 00 00 A0 
       
            uint32_t statkal = (((notifydata[4] << 8) + notifydata[5]) << 8) + notifydata[6];
            uint32_t statdistance = ((notifydata[7] << 8) + notifydata[8]);
            uint32_t statstep = (((notifydata[9] << 8) + notifydata[10]) << 8) + notifydata[11];
            uint32_t stattime = ((notifydata[12] << 8) + notifydata[13]);
            
            char buff[100] = "";
            sprintf(buff, "Data - Kcal: %lu  Time:%lu  Distance:%lu  Step: %lu", statkal, stattime, statdistance, statstep);
            Serial.println(buff);

As an aditional test, I actually impelmented the test suggested by Johnwasser. This yielded similar results. Therefore at this point I don't believe I have an issue with my code, and that some other influence is impacting this.

            // A9 02 0D 00 00 00 03 00 01 00 00 02 00 06 00 00 A0 
            byte notifydatab[18];

            for (int i = 0; i < 18; i++)
            {
              notifydatab[i] = random(256);
            }

            uint32_t statkal = (((notifydatab[4] << 8) + notifydatab[5]) << 8) + notifydatab[6];
            uint32_t statdistance = ((notifydatab[7] << 8) + notifydatab[8]);
            uint32_t statstep = (((notifydatab[9] << 8) + notifydatab[10]) << 8) + notifydatab[11];
            uint32_t stattime = ((notifydatab[12] << 8) + notifydatab[13]);
            
            char buff[100] = "";
            sprintf(buff, "Data - Kcal: %lu  Time:%lu  Distance:%lu  Step: %lu", statkal, stattime, statdistance, statstep);
            Serial.println(buff);

Not sure if this helps at all, but I made a further edit to the code to use a Serial.write as well.


            uint32_t statkal = (((notifydata[4] << 8) + notifydata[5]) << 8) + notifydata[6];
            uint32_t statdistance = ((notifydata[7] << 8) + notifydata[8]);
            uint32_t statstep = (((notifydata[9] << 8) + notifydata[10]) << 8) + notifydata[11];
            uint32_t stattime = ((notifydata[12] << 8) + notifydata[13]);
            
            char buff[100] = "";
            sprintf(buff, "Data - Kcal: %lu  Time:%lu  Distance:%lu  Step: %lu", statkal, stattime, statdistance, statstep);
            Serial.println(buff);
            Serial.write(buff);
            Serial.println();

As you can see, there is no consistency here, buffer used in both the println and the write are the same.

Another indication that the problem is elsewhere in your code. Good luck finding it.

Thanks all again for your help. Got to love issues like this, I went through the line with

sprintf(buff, "Data - Kcal: %lu Time: %lu Distance: %lu Step: %lu", statkal, stattime, statdistance, statstep);

and double checked spacing. It turns out there was some kind of special character (not sure what it was) between the Time: %lu and Distance: %lu. Now the issue is resolved!!!!

You had fullwidth colons (U+FF1A) instead of ASCII colons :. For some reason the serial monitor did not show them correctly.

Hi,
Have you tried a lower serial baud rate?
Have you tried another USB cable?
There are USB cables then there are other USB cables.

Tom... :grinning: :+1: :coffee: :australia:

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