SDI-12 CRC校验

最近在搞SDI-12的数据,奇怪的是数据读取有万一之一的概率有问题,一般的acsii字符,有时候可能有变位,奇怪的是7E1,做校验还有概率出错,万幸有crc校验,但是crc有不同于一般的crc,所幸看看sdi12的协议规范,写了个测试函数,搞定

 

 

#include<stdio.h>

#define uint16 unsigned int
#define uint8 unsigned char
uint16 CRC_SDI(uint8 *buffer,uint16 len)
{
    uint16 crc=0;
    uint16 count=0;
    for(count=0;count<len;count++)
    {
        uint16 i=0;
        crc=crc^(*(buffer+count));
        for(i=0;i<8;i++)
        {
            if(crc&1)
            {
                crc>>=1;
                crc=crc^0xa001;
            }
            else
            {
                crc>>=1;
            }
        }
    }
    return crc;
}
int main(void)
{
    //long a=0;
    //char b='a';
    //while(1)
    //{
    //    printf("请输入数字:\r\n");
    //    scanf("%ld",&a);
    //    printf("十进制是%ld,十六进制是%x\r\n",a,a);
    //    //getchar();
    //    b=getchar();
    //    if(b=='e')
    //        break;
    //}
    unsigned char buffer[3];
    unsigned int data=0xC4A8;
    unsigned int data1=0;

    //uint8 mydata[]={0x30,0x2b,0x32,0x2e,0x30,0x31,0x33,0x2b,0x30};
    uint8 mydata[]={0x30,0x2B,0x32,0x2E,0x30,0x31,0x31,0x2B,0x30};
    data=CRC_OTT(mydata,9);
    //data=(data1>>8)|((data1&0xff)<<8);
    buffer[0]=(data>>12)|0x40;
    buffer[1]=((data>>6)&0x3F)|0x40;
    buffer[2]=((data)&0x3F)|0x40;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值