Home AMX User Forum NetLinx Studio

Help with "Pima" Alarm System CRC

Hello,
I have a costumer that is installing "Pima Hunter Pro" alarm system.
Now the commands are pretty simple but the CRC is killing me...
The guys from pima sent me a demo for the CRC calculation in C (attached) and i tried to convert what they sent to the Netlinx language. I get a result but not what it should be...

define_function char[2] fuCrcCalc(char strOutgoing[300])
stack_var integer iTotalLength
stack_var long iCrc
stack_var integer iByteCounter
stack_var integer iBitCounter
stack_var long iDataByte
{
iTotalLength = length_array(strOutgoing)
for (lviByteCounter=1;lviByteCounter<=lviTotalLength;lviByteCounter++)
{
iDataByte = strOutgoing[lviByteCounter]
for (lviBitCounter=0;lviBitCounter<=7;lviBitCounter++)
{
iDataByte = (iCrc & 1) ^ iDataByte
iCrc = iCrc >> 1
if (iDataByte & 1)
{
iCrc = iCrc ^ $A001
}
iDataByte = iDataByte >> 1
}
}
return itohex(iCrc)
}


The system is sending a heart beat all the time:
$08,$0D,$05,$00,$00,$00,$00,$00,$00,$59,$F3
the first byte is the length and the last 2 are the CRC, and they should be left out of the calculation,
so i just run my function on "$0D,$05,$00,$00,$00,$00,$00,$00" but I get a different CRC then the end of this heart beat.

Any thoughts?

Comments

  • AMXJeffAMXJeff Posts: 450
    Ronen wrote: »
    Any thoughts?

    This function works... The data length is part of the CRC, at least according to the C code... With the length data the return is correct.
    DEFINE_FUNCTION CHAR[2] CRC16(CHAR cMsg[])
    {
    	CHAR X;
    	INTEGER CRC;
    	CHAR BYTEDATA;
    	CHAR BIT;
    	
    	FOR (X = 1, CRC = 0; X <= LENGTH_STRING(cMsg); X++)
    	{
    		BYTEDATA = cMsg[X];
    		
    		FOR (BIT = 0; BIT < 8; BIT++)
    		{
    			BYTEDATA = TYPE_CAST(BYTEDATA ^ (CRC & 1));
    			
    			CRC = TYPE_CAST(CRC >> 1);
    			
    			IF (BYTEDATA & 1)
    				CRC = CRC ^ $A001;
    				
    			BYTEDATA = TYPE_CAST(BYTEDATA >> 1);
    		}		
    	}
    	
    	RETURN "(CRC >> 8),(CRC & $FF)";
    }
    
    
  • RonenRonen Posts: 55
    Great work AMXJeff!
    AMXJeff wrote: »
    The data length is part of the CRC, at least according to the C code...[/code]

    "crc=crcCalc(arr,length+1);"

    i guess i missed this little part in the C code... ; ;

    thanks again for your help - rate up!
  • AMXJeffAMXJeff Posts: 450
    Ronen wrote: »
    i guess i missed this little part in the C code...

    this is where I caught the length being part of the CRC calculation...

    arr[0]=(char)length;
  • udiudi Posts: 107
    Hello Ronen
    I'm also trying to learn about the PIMA protocol and I'm new in this can you help me with this. An example code will be great or explaination how to control it.
    I will be pleased If you also send me the protocol I think I have an old version.
    Thanks for any help that leads me in the right direction. Examples are very much appreciated
Sign In or Register to comment.