#include <stdio.h>


typedef unsigned short ushort;

typedef unsigned char uchar;


typedef union _CRC

{

    ushort crc16;

    uchar by[2];

} CRC;


//输入不带CRC码的数据时,返回值是CRC码

//输入带CRC码的数据时,则可以进行校验,返回0时CRC校验成功,否则CRC校验失败

ushort CRC16(uchar *ba, int size)

{

   CRC crc;

   crc.crc16 = 0xffff;

   int i, l;

   for (i=0; i<size; i++)

   {

       uchar ch = ba[i];

       crc.by[0] = crc.by[0] ^ ch;

       for (l=0; l<8; l++)

       {

           if (crc.by[0] & 0x01)

           {