recommendations for better buffer management and elimination of string usage?

hi, im trying to put together a buffer that deals with character arrays as opposed to the usage of the string library. though i cant quite seem to get the returned value of the CRC32 function to cast correctly and was wondering if i am going about this in the correct fassion?

currently i have some code that generates this output (format is undesirable in this example):

$C:1728067682@{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:27.80,“DEW”:4.75}

checking the CRC32 hash using Python 2.7 with the “binascii” module checked out ok:

Python 2.7.3
>>>import binascii
>>>s = “”"{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:27.80,“DEW”:4.75}"""
>>>binascii.crc32(s) & 0xFFFFFFFF
1728067682L
>>>

This is a sample of the Arduino’s code which creates that output:

unsigned long crc_check(char *s)
{
  unsigned long crc = ~0L;
  while (*s)
    crc = crc_update(crc, *s++);
  crc = ~crc;
  return crc;
}

#define CRC_HEADER "C:"
#define DEW_HEADER "\"DEW\":" 
#define TMP_HEADER "\"TMP\":"
#define RUN_HEADER "\"RUN\":"
#define STE_HEADER "\"STE\":"
#define IDS_HEADER "\"IDS\":"
#define TYP_HEADER "\"TYP\":"
#define BRD_HEADER "\"BRD\":" 
#define UPT_HEADER "\"UPT\":"

void commsTalk() {
  char bigstr[128] = {'\0'};
  char numBuff[16] = {'\0'};

  //START CREATING JSON OBJECTS
  strcat(bigstr, "{");
  
  strcat(bigstr, TYP_HEADER);
  strcat(bigstr, "\"");
  strcat(bigstr, unitType);
  strcat(bigstr, "\",");
  
  strcat(bigstr, IDS_HEADER);
  strcat(bigstr, "\"");
  strcat(bigstr, myID);
  strcat(bigstr, "\",");
  
  strcat(bigstr, RUN_HEADER);  
  strcat(bigstr, itoa(machRunning, numBuff, 10));
  strcat(bigstr, ",");
  
  float LM35tempC = (1.8*(analogRead(lm35pin) / 9.31))+32;
  dtostrf(LM35tempC, -1, 2, numBuff);  
  strcat(bigstr, BRD_HEADER);
  strcat(bigstr, numBuff);
  strcat(bigstr, ",");
  
  dtostrf(SHT11temperature, -1, 2, numBuff);
  strcat(bigstr, TMP_HEADER);
  strcat(bigstr, numBuff);
  strcat(bigstr, ","); 
  
  dtostrf(SHT11dewpoint, -1, 2, numBuff);  
  strcat(bigstr, DEW_HEADER);
  strcat(bigstr, numBuff);
  
  strcat(bigstr, "}");
  //DONE CREATING JSON OBJECTS

  //NOW CREATE A CRC HASH FOR THE JSON
  String JSON = (CRC_HEADER+String(crc_check(bigstr))+'@'+bigstr);
  comms.print("$"+JSON+"\r");
  Serial.println("$"+JSON+"\r");//just printing for debugging purposes
}

So that seems to work,

but when i try changing the bottom portion of that code to something like:

  //NOW CREATE A CRC HASH FOR THE JSON
  itoa(crc_check(bigstr),numBuff,11);
  strcat(bigstr, CRC_HEADER);
  strcat(bigstr, numBuff);
  strcat(bigstr, "\r");
  comms.print(bigstr);
  Serial.println(bigstr);

i get this output:

{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:28.00,“DEW”:4.88}C:21115

Python 2.7.3
>>>import binascii
>>>s = “”"{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:28.00,“DEW”:4.88}"""
>>>binascii.crc32(s) & 0xFFFFFFFF
1682012190L
>>>

a CRC of 21115 is not agreeing with Python.
some others include:
C:16a80
C:31a45 - note the ‘a’??
C:9954

and then i tried this:

  //NOW CREATE A CRC HASH FOR THE JSON
  dtostrf(crc_check(bigstr), -1, 0, numBuff); 
  strcat(bigstr, CRC_HEADER);
  strcat(bigstr, numBuff);
  strcat(bigstr, "\r");
  comms.print(bigstr);
  Serial.println(bigstr);

output:

{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:27.83,“DEW”:4.77}C:3908843000

Python 2.7.3
>>>import binascii
>>>s = “”"{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:27.83,“DEW”:4.77}"""
>>>binascii.crc32(s) & 0xFFFFFFFF
3908843054L
>>>

These two are very close but the last two digits are off. not really sure what would be the best approach for this…

truthfully im not even positive that the way i have it structured is even all that efficient??
and i apologize but I feel that im still pretty novice to programming in C++, so im sure a lot of these questions a very noobish.

Any suggestions are very welcome though!
Happy New Year! and thanks for viewing :slight_smile:

[UPDATE]

  //NOW CREATE A CRC HASH FOR THE JSON
  ltoa(crc_check(bigstr) & 0xFFFFFFFF, numBuff, 10); 
  strcat(bigstr, CRC_HEADER);
  strcat(bigstr, numBuff);
  strcat(bigstr, "\r");
  comms.print(bigstr);
  Serial.println(bigstr);

output:
{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:25.88,“DEW”:4.45}C:-1192923083

Python 2.7.3
>>>import binascii
>>>s = “”"{“TYP”:“mach_link”,“IDS”:“DX2100_test1”,“RUN”:1,“BRD”:229.79,“TMP”:25.88,“DEW”:4.45}"""
>>>binascii.crc32(s) & 0xFFFFFFFF
-1192923083
>>>

The python example starts with """{ and ends with }""" so are you missing a single starting and ending double quote (")

idea: instead ot itoa() [INT TO ASCII] you might need ltoa() [LONG TO ASCII] as the CRC is 32 bit?

dtostrf(crc_check(bigstr), -1, 0, numBuff); is for float so wrong datatype

Riva: The python example starts with """{ and ends with }""" so are you missing a single starting and ending double quote (")

in python you can use triple quotes to express something like a string literal...

basically this normally escaped string: " \"how\" \"are\" \"you?\" "

can be represented as: """ "how" "are" "you?" """

sorry for the confusion.

robtillaart: idea: instead ot itoa() [INT TO ASCII] you might need ltoa() [LONG TO ASCII] as the CRC is 32 bit?

thanks for the suggestion, that definitely solved the issue. i guess ltoa and itoa look so similar i missed it in my search.

Thanks!

megaBlocks:

Riva: The python example starts with """{ and ends with }""" so are you missing a single starting and ending double quote (")

in python you can use triple quotes to express something like a string literal...

basically this normally escaped string: " \"how\" \"are\" \"you?\" "

can be represented as: """ "how" "are" "you?" """

I was thinking along the lines of VB where to have a string enclosed in double quotes you need to start/end it with """ I have managed to avoid python sofar (probably why I don't use my RP) but should maybe learn a bit more. :blush: