Go Down

Topic: Sum of ASCII values of characters in a string (Read 1 time) previous topic - next topic

Hello,

My Arduino Mega periodically reads the voltage applied to one of its analog inputs. I then form a string which is to be transmitted out on one of the serial TX pins into another hardware device. Various parameters make up the whole string but I must write a checksum to the end of the string.

To calculate the checksum, I need to add the ASCII code for all characters in the string and then perform calculations. The result of the checksum is then placed at the end of the string and transmitted. An example string could be s0DFFID1234562.7 and I can form this okay and place it in a String variable.

I cannot find how to place the ASCII code for each character into a variable so I can do the number crunching. Have Googled etc but still struggling.

Grumpy_Mike

#1
Jan 26, 2013, 10:26 pm Last Edit: Jan 26, 2013, 10:29 pm by Grumpy_Mike Reason: 1
Use a char* array instead of a string, then you just pick them off one at a time:-

Code: [Select]
char* myArray [] = "This is my string";
sum = int(myArray[0]) + int(myArray[1]) + int(myArray[2]) ......


Well you will use a loop and index but you get the idea.


michinyon

Many applications also use the bitwise exclusive or to form the checksum.  You can run into
issues over whether the char is signed or unsigned  and  how much of the checksum you actually use.

I have data stored in String variables already that I would like to move one character at a time into the array but get the message "cannot convert 'String' to 'char' in assignment". Does that mean that I can never extract a single substring character and assign it to an array location.

liudr

can you just not use String and use char array instead?

#6
Jan 27, 2013, 01:34 am Last Edit: Jan 27, 2013, 01:48 am by Coding Badly Reason: 1
I am working to just use the array but am new to this so solving the problems one by one. I am using an array to hold most of the individual characters that make up the whole command but I am struggling with getting the voltage value into the array as each number being a character. Please see my code below which gets the three digits from the voltage I am reading.

Code: [Select]
 // calculate first voltage digit
 int digit1Int = int(voltage);
 Serial.print("Voltage digit one :");
 Serial.println(digit1Int);
 //Serial.println();
 
 // calculate second voltage digit
 int digit2Int = int(10*(voltage-digit1Int));
 Serial.print("Voltage digit two :");
 Serial.println(digit2Int);
 //Serial.println();  
 
 // calculate third voltage digit
 int digit3Int = int((100*voltage)-(int(voltage*10)*10));
 Serial.print("Voltage digit three :");
 Serial.println(digit3Int);
 
// set array values locations to hold the command
 commandArray[0] = 's';    // s for send sdm
 commandArray[1] = '0';    // command size
 commandArray[2] = 'D';    // command size
 commandArray[3] = '1';    // delay
 commandArray[4] = '9';    // delay
 commandArray[5] = '1';    // destination
 commandArray[6] = '2';    // destination
 commandArray[7] = '3';    // destination
 commandArray[8] = '4';    // destination
 commandArray[9] = '5';    // destination
 commandArray[10] = '6';    // destination
 commandArray[11] = '7';    // destination
 commandArray[12] = '8';    // destination
 commandArray[13] = digit1Int;    // voltage
 commandArray[14] = digit2Int;    // voltage
 commandArray[15] = digit3Int;    // voltage
 
 // print out the values
 for (int loopCntr=0; loopCntr <= 15; loopCntr++)
 {
   Serial.println(commandArray[loopCntr]);
   Serial.println(int(commandArray[loopCntr]));

   
 }


When I run this it does not print the voltage information correctly. Thank you for your help.


Moderator edit: [code] [/code] tags added.

GoForSmoke

#7
Jan 27, 2013, 01:57 am Last Edit: Jan 27, 2013, 02:00 am by GoForSmoke Reason: 1
Code: [Select]
 // calculate first voltage digit

// set array values locations to hold the command
 commandArray[0] = 's';    // s for send sdm
 commandArray[1] = '0';    // command size
 commandArray[2] = 'D';    // command size
 commandArray[3] = '1';    // delay
 commandArray[4] = '9';    // delay
 commandArray[5] = '1';    // destination
 commandArray[6] = '2';    // destination
 commandArray[7] = '3';    // destination
 commandArray[8] = '4';    // destination
 commandArray[9] = '5';    // destination
 commandArray[10] = '6';    // destination
 commandArray[11] = '7';    // destination
 commandArray[12] = '8';    // destination
 commandArray[13] = digit1Int;    // voltage
 commandArray[14] = digit2Int;    // voltage
 commandArray[15] = digit3Int;    // voltage
 


Is commandArray[] ints or chars?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Hello GoForSmoke, please see the code below where the array is char.

void setup() {
  // initialize both serial ports. Serial prints to PC
  Serial.begin(9600);
 
  // initialise the serial port to send data to the radio
  Serial1.begin(9600);
}

void loop() {
  // initialise variables
  String voltageStr;
  String sizeStr;
  int commandSizeInt;
  String commandStr;
  String commandSizeHex;
  String commandSizeStr;
  int loopCounter;
  int commandSubStringInt;
  String commandSubStringString;
  char commandChr;
  char commandArray[20];
 
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
 
  // calculate the actual voltage
  float voltage = sensorValue * (5.0 / 1023.0);
 
  // print out the values above:
  Serial.print("Sensor value 0-1023 :");
  Serial.println(sensorValue);
  Serial.print("Calculated voltage :");
  Serial.println(voltage);
 
  // calculate first voltage digit
  int digit1Int = int(voltage);
  Serial.print("Voltage digit one :");
  Serial.println(digit1Int);
  //Serial.println();
 
  // calculate second voltage digit
  int digit2Int = int(10*(voltage-digit1Int));
  Serial.print("Voltage digit two :");
  Serial.println(digit2Int);
  //Serial.println(); 
 
  // calculate third voltage digit
  int digit3Int = int((100*voltage)-(int(voltage*10)*10));
  Serial.print("Voltage digit three :");
  Serial.println(digit3Int);
 
  // set array values locations to hold the command
  commandArray[0] = 's';    // s for send sdm
  commandArray[1] = '0';    // command size
  commandArray[2] = 'D';    // command size
  commandArray[3] = '1';    // delay
  commandArray[4] = '9';    // delay
  commandArray[5] = '0';    // destination
  commandArray[6] = '8';    // destination
  commandArray[7] = '0';    // destination
  commandArray[8] = '0';    // destination
  commandArray[9] = 'T';    // destination
  commandArray[10] = 'E';    // destination
  commandArray[11] = 'S';    // destination
  commandArray[12] = 'T';    // destination
  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
 
  for (int loopCntr=0; loopCntr <= 15; loopCntr++)
  {
    Serial.println(commandArray[loopCntr]);
    Serial.println(int(commandArray[loopCntr]));
  }

  delay(5000);
 
}

PeterH

How about:

Code: [Select]

    sprintf(commandArray, "s0D1912345678%03d", voltage);


The %03d is replaced with a three digit decimal representation of the value of voltage, padded with leading zeros if necessary.
I only provide help via the forum - please do not contact me for private consultancy.

Thank you to all that have replied. I will experiment with your advice.

michinyon

int voltage=240 ;
char buffer[4] ;
sprintf(buffer,"%03d",voltage);
buffer[3]='\0' ;

// And to calculate the checksum
byte cs=0 ;
char* p=buffer ;
while(*p != '\0' )
{
    cs ^= *p ;
    p++ ;
}

GoForSmoke


Hello GoForSmoke, please see the code below where the array is char.
Code: [Select]

  commandArray[13] = digit1Int;    // voltage
  commandArray[14] = digit2Int;    // voltage
  commandArray[15] = digit3Int;    // voltage
}
[/quote]

Then stuffing 16-bit ints into 8-bit chars is probably not a good idea if you want the answers to make sense.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Thank you to all who have responded with advice and code samples. I have solved the problem using your responses. Best regards.

Go Up