Android Question Custom checksum crc16

Pesciolina

Active Member
Licensed User
Longtime User
Hi,
C:
WORD CRC16Table[256] ;
 
// initialize the CRC16 table
   extern void InitCRC16( void ) {
   WORD i, j ;   
   WORD crc ;
 
    for ( i = 0 ; i < 256 ; i += 1 ) {   
    for ( crc = i << 8, j = 0 ; j < 8 ; j += 1 ) 
    crc = ( crc << 1 ) ^ ( ( crc & 0x8000 ) ? 0x1021 : 0 ) ;   
    CRC16Table[ i ] = crc ;   
     }
    }
 
 
// calculate the crc of a char array pointed at by p
extern WORD CalcCRC16( unsigned char * p, WORD size ) {   
 WORD crc = 0xFFFF ;  WORD i ; 
 
    for ( i = 0 ; i < size ; i++, p++ )  // for all chars     
     crc = CRC16Table[ ( ( crc >> 8 ) & 255 ) ] ^ ( crc << 8 ) ^ *p ;   
    return crc ; }
    
// …assuming a preceding InitCRC16() has initialized the CRC16 table…
// build a full record, ‘Message’ is command without <SOR>, <CRC> and <EOR>
sprintf( buf, "%c%sx%X\n", 1, Message, CalcCRC16( Message ) ) ;
// buf is now command with <SOR>, <CRC> and <EOR>
I have to talk to an instrument and to send the commands I have to use a customized function of the manufacturer. The producer gives the example in C, you can help me transform it for B4A. Thanks
 
Top