3355|3

82

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

有关checksum的问题,不知道原因,还烦请高手指点一下,多谢了. [复制链接]

小弟不才,学习checksum算法的时候,对如下数据始终无法校验成功,麻烦各位大哥帮忙指点下。
对两段报文,用相同的机制,一个校验成功,一个失败,但这个两个包都是对的,我不知道原因,麻烦哥指点一下。多谢了。


static u8 packet1[] =
{
                   /*0x38 ,0x8b为checksum字段*/
        0x02, 0xb4, 0x38, 0x8b, 0x00, 0x01, 0x00, 0x0a, 0x53, 0x77,
        0x69, 0x74, 0x63, 0x68, 0x00, 0x05, 0x00, 0xbc, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x4f,
        0x53, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x2c, 0x20, 0x43, 0x32, 0x39, 0x36,
        0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39, 0x36,
        0x30, 0x2d, 0x4c, 0x41, 0x4e, 0x42, 0x41, 0x53, 0x45, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 0x56, 0x65,
        0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x32, 0x28, 0x32, 0x35, 0x29, 0x53, 0x45,
        0x45, 0x32, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f, 0x46, 0x54,
        0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72,
        0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30,
        0x30, 0x36, 0x20, 0x62, 0x79, 0x20, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74,
        0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
        0x65, 0x64, 0x20, 0x46, 0x72, 0x69, 0x20, 0x32, 0x38, 0x2d, 0x4a, 0x75, 0x6c, 0x2d, 0x30, 0x36,
        0x20, 0x30, 0x34, 0x3a, 0x33, 0x33, 0x20, 0x62, 0x79, 0x20, 0x79, 0x65, 0x6e, 0x61, 0x6e, 0x68,
        0x00, 0x06, 0x00, 0x19, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39,
        0x36, 0x30, 0x2d, 0x32, 0x34, 0x54, 0x43, 0x2d, 0x4c, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
        0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x14, 0x46, 0x61,
        0x73, 0x74, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x32, 0x31, 0x00, 0x04,
        0x00, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x08, 0x00, 0x24, 0x00, 0x00, 0x0c, 0x01, 0x12, 0x00,
        0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x20, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x1a, 0xa1, 0xed, 0x0e, 0x00, 0xff, 0x00, 0x00, 0x00, 0x09, 0x00, 0x11, 0x6d, 0x61,
        0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x00, 0x0a, 0x00, 0x06, 0x00,
        0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00,
        0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0x00, 0x00, 0x00,
        0x00
};

static u8 packet[] =
{
                    /*0xba,0x50为checksum字段*/
        0x02, 0xb4, 0xba, 0x50, 0x00, 0x01, 0x00, 0x0a, 0x53, 0x77,
        0x69, 0x74, 0x63, 0x68, 0x00, 0x05, 0x00, 0xbc, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x49, 0x4f,
        0x53, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x2c, 0x20, 0x43, 0x32, 0x39, 0x36,
        0x30, 0x20, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x28, 0x43, 0x32, 0x39, 0x36,
        0x30, 0x2d, 0x4c, 0x41, 0x4e, 0x42, 0x41, 0x53, 0x45, 0x2d, 0x4d, 0x29, 0x2c, 0x20, 0x56, 0x65,
        0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x32, 0x2e, 0x32, 0x28, 0x32, 0x35, 0x29, 0x53, 0x45,
        0x45, 0x32, 0x2c, 0x20, 0x52, 0x45, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x53, 0x4f, 0x46, 0x54,
        0x57, 0x41, 0x52, 0x45, 0x20, 0x28, 0x66, 0x63, 0x31, 0x29, 0x0a, 0x43, 0x6f, 0x70, 0x79, 0x72,
        0x69, 0x67, 0x68, 0x74, 0x20, 0x28, 0x63, 0x29, 0x20, 0x31, 0x39, 0x38, 0x36, 0x2d, 0x32, 0x30,
        0x30, 0x36, 0x20, 0x62, 0x79, 0x20, 0x43, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x53, 0x79, 0x73, 0x74,
        0x65, 0x6d, 0x73, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x0a, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
        0x65, 0x64, 0x20, 0x46, 0x72, 0x69, 0x20, 0x32, 0x38, 0x2d, 0x4a, 0x75, 0x6c, 0x2d, 0x30, 0x36,
        0x20, 0x30, 0x34, 0x3a, 0x33, 0x33, 0x20, 0x62, 0x79, 0x20, 0x79, 0x65, 0x6e, 0x61, 0x6e, 0x68,
        0x00, 0x06, 0x00, 0x19, 0x63, 0x69, 0x73, 0x63, 0x6f, 0x20, 0x57, 0x53, 0x2d, 0x43, 0x32, 0x39,
        0x36, 0x30, 0x2d, 0x32, 0x34, 0x54, 0x43, 0x2d, 0x4c, 0x00, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
        0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x14, 0xde, 0x00, 0x03, 0x00, 0x14, 0x46, 0x61,
        0x73, 0x74, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x30, 0x2f, 0x32, 0x31, 0x00, 0x04,
        0x00, 0x08, 0x00, 0x00, 0x00, 0x28, 0x00, 0x08, 0x00, 0x24, 0x00, 0x00, 0x0c, 0x01, 0x12, 0x00,
        0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x02, 0x21, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x1a, 0xa1, 0xed, 0x0e, 0x00, 0xff, 0x00, 0x00, 0x00, 0x09, 0x00, 0x11, 0x6d, 0x61,
        0x6e, 0x75, 0x66, 0x61, 0x63, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x00, 0x0a, 0x00, 0x06, 0x00,
        0x01, 0x00, 0x0b, 0x00, 0x05, 0x01, 0x00, 0x12, 0x00, 0x05, 0x00, 0x00, 0x13, 0x00, 0x05, 0x00,
        0x00, 0x16, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0xcc, 0x00, 0x04, 0xc0, 0xa8, 0x14,
        0xde
}

unsigned short check_sum( unsigned char* data, int length )
{
        register u32 sum = 0;
        register const u16 *d = ( const u16 * )data;
       

        while ( length > 1 ) {
                sum += *( u16* )d++;
                length -= 2;
        }

        if ( length )
                sum += *( const u8* )d;

        sum = ( sum >> 16 ) + ( sum & 0xffff );
        sum += ( sum >> 16 );
        return ( u16 )~sum;
}

void main()
{
        unsigned int checksum;
        checksum = check_sum( packet,363);
        printf("%d\r\n",checksum );
        checksum = check_sum( packet1,363);
        printf("%d\r\n",checksum );
}
这样计算后由packet1计算的checksum为0而packet的checksum却不为0,但两个数据绝对没有错误,麻烦大哥们指点一下。

最新回复

但总是计算出来相差1  详情 回复 发表于 2008-7-23 00:38
点赞 关注

回复
举报

72

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
大小端的问题吧?
用的什么处理器?
 
 

回复

84

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
powerpc,我想也是这问题,
 
 
 

回复

67

帖子

0

TA的资源

一粒金砂(初级)

4
 
但总是计算出来相差1
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表