This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

ELK M1 Alarm panel
#3
Hi Admin,

Thank you,  the checksum function works.  I am trying to do the reverse and verify the message received has a valid checksum



Code:
function checksum(str)  local sum = 0  for i = 1, #str do    sum = (sum + string.byte(str,i)) % 256  end  x = 256-sum  return string.format('%02X', 256 - sum) end function reversechk(str, cksum)  local sum = tonumber(cksum,16)  log(sum)  for i=1, #str do    sum= (sum + string.byte(str,i))  end  return string.format('%02X', sum) end local message= '16XK403923611081711000' local chksum = checksum(message) log(chksum) -- chksum is 67 -- so the total message is '16XK40392361108171100067' local isok = reversechk(message,chksum) log(isok)


The reversechk function does not return a 0,  what am doing wrong?  I have the reverse calculation in C code,  maybe you can understand what they doing?

Code:
Calculate checksum on received and transmitted ASCII string Example C code program //INT8U is an 8 bit unsigned integer. INT8U itAscRecBuf[82]; //ASCII receive character buffer INT8U AscHexToBin(INT8U, INT8U *); //ASCII hex to binary conversion INT8U AsciiToHex( INT8U); //Ascii to Hex conversion //Calculate checksum on a received ASCII string, return checksum value. //It should equal 0 if good. INT8U CalcCheckSum(void) {     INT8U i,length, cc;     length = AscHexToBin(2, &itAscRecBuf[0]);     //get length value, first two characters          cc = AscHexToBin(2, &itAscRecBuf[length]); //get checksum value at end of string.     for (i=0;i<length ;i++ )         {             cc += itAscRecBuf[i]; //get string value and add it to checksum         }         return(cc);         //good checksum should equal 0 }     //ascii hex to binary, width 1 or 2     INT8U AscHexToBin(INT8U Width, INT8U * DataPtr) //         {             INT8U aVal; // accumulated value             aVal = AsciiToHex(*DataPtr);             DataPtr++;             if (Width == 2)    //two digits wide, else 1 digit wide                 {                     aVal = aVal << 4;                     aVal += AsciiToHex(*DataPtr);                 }             return(aVal);          }     //Ascii to Hex conversion     INT8U AsciiToHex( INT8U Value )         {             switch ( Value )                 {                     case 'A': return( 10 );                     case 'B': return( 11 );                     case 'C': return( 12 );                     case 'D': return( 13 );                     case 'E': return( 14 );                     case 'F': return( 15 );                     default: return( Value - 0x30 );                 }         }


Thanks,

Roger
Reply


Messages In This Thread
ELK M1 Alarm panel - by rocfusion - 07.08.2017, 17:04
RE: ELK M1 Alarm panel - by admin - 08.08.2017, 08:50
RE: ELK M1 Alarm panel - by rocfusion - 12.08.2017, 23:21
RE: ELK M1 Alarm panel - by admin - 14.08.2017, 06:13
RE: ELK M1 Alarm panel - by rocfusion - 14.08.2017, 17:14
RE: ELK M1 Alarm panel - by admin - 17.08.2017, 06:30
RE: ELK M1 Alarm panel - by rocfusion - 17.08.2017, 07:07
RE: ELK M1 Alarm panel - by rocfusion - 19.08.2017, 23:32
RE: ELK M1 Alarm panel - by rocfusion - 22.08.2017, 06:31
RE: ELK M1 Alarm panel - by admin - 22.08.2017, 06:52

Forum Jump: