Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. Calculate checksum on received and transmitted ASCII string
  2. Example C code program
  3. //INT8U is an 8 bit unsigned integer.
  4. INT8U itAscRecBuf[82];
  5. //ASCII receive character buffer
  6. INT8U AscHexToBin(INT8U, INT8U *);
  7. //ASCII hex to binary conversion
  8. INT8U AsciiToHex( INT8U);
  9. //Ascii to Hex conversion
  10. //Calculate checksum on a received ASCII string, return checksum value.
  11. //It should equal 0 if good.
  12. INT8U CalcCheckSum(void)
  13. {
  14.  INT8U i,length, cc;
  15.  length = AscHexToBin(2, &itAscRecBuf[0]);
  16.  //get length value,
  17.  //first two characters
  18.  cc = AscHexToBin(2, &itAscRecBuf[length]);
  19.  //get checksum value
  20.  //at end of string.
  21.  for (i=0;i<length ;i++ )
  22.  {
  23.    cc += itAscRecBuf[i];
  24.      //get string value and add it to
  25.      //checksum
  26.  }
  27.  return(cc);
  28.  //good checksum should equal 0
  29. }
  30.  
  31. //ascii hex to binary, width 1 or 2
  32. INT8U AscHexToBin(INT8U Width, INT8U * DataPtr)
  33. { INT8U aVal;
  34. // accumulated value
  35. aVal = AsciiToHex(*DataPtr);
  36.  DataPtr++;
  37.  if (Width == 2)//two digits wide, else 1 digit wide
  38.  {
  39.    aVal = aVal << 4;
  40.      aVal += AsciiToHex(*DataPtr);
  41.  }
  42.  return(aVal);
  43. }
  44.  
  45. //Ascii to Hex conversion
  46. INT8U AsciiToHex( INT8U Value )
  47. {
  48.  switch ( Value )
  49.  {
  50.      case 'A':
  51.         return( 10 );
  52.      case 'B':
  53.         return( 11 );
  54.      case 'C':
  55.         return( 12 );
  56.      case 'D':
  57.         return( 13 );
  58.      case 'E':
  59.         return( 14 );
  60.      case 'F':
  61.         return( 15 );
  62.      default:
  63.         return( Value - 0x30 );
  64.   }
  65. }
  66.  
  67. /* **************************************************************
  68. To generate the checksum for an ASCII string to be transmitted, clear the checksum value (CC = 0;)
  69. Add each byte of the string to be transmitted to the checksum value
  70. (CC += ASCII byte).
  71. Do a two’s compliment of the checksum (CC = (CC ^ 0xff) + 1;).
  72. Convert the checksum’s upper and lower nibble’s to ASCII hex.
  73. Send a carriage return (0x0D) and line feed (0x0A).
  74. The following is an example C code for sending the checksum after building the initial checksum value.
  75. */
  76.  
  77. //send checksum + carriage return, line feed
  78. //Comm2_Put_Ch() sends the byte out the serial data port.
  79. INT8U NibToAsc(INT8U);
  80. void SendChecksum(INT8U CC)
  81. //enter with checksum value added up
  82. {
  83.   CC = (CC ^ 0xFF) + 1;
  84.     //calculate two's compliment
  85.     Comm2_Put_Ch(NibToAsc(CC >> 4));
  86.     Comm2_Put_Ch(NibToAsc(CC & 0x0F));
  87.     Comm2_Put_Ch(0x0D);
  88.     Comm2_Put_Ch(0x0A);
  89. }
  90.  
  91. INT8U NibToAsc(INT8U Nib)
  92. {
  93.   if (Nib < 0x0A)
  94.       return( (INT8U)(Nib + 0x30));
  95.     else if (Nib <= 0x0F)
  96.       return(Nib + 0x37);
  97.         //converts to 0x0A -> 0x0F hex value
  98.    
  99.     return(0x20); //error
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement