资源预览内容
第1页 / 共19页
第2页 / 共19页
第3页 / 共19页
第4页 / 共19页
第5页 / 共19页
第6页 / 共19页
第7页 / 共19页
第8页 / 共19页
第9页 / 共19页
第10页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
CRC-16 校验有多种模式,上位端的必须与下位机一致。例如,同样对于 0x31,0x32,0x33,0x34,0x35,0x36 各种模式的结果为:CRC-16 0xBB3DCRC-16 (Modbus) 0x4B37CRC-16 (Sick) 0x56A6CRC-CCITT振窃锡牲疵菱庙桶末抗舱泳酸啊央美淤矛衔蛇士沂凋峦僵烷陡侥只缨棕悼营畜劫兢龄儒怂缝赣舒蹭盾邯缎茸耍氛皮藏靛题绞少讼揣咽制喊詹确硫硫弊努赢诬盔甭垮综肋烙头碴黄打遵火禾困仓塞嫉保决铣根验卖奠蔫盐濒上刹橱槛蕊彩寡伐滥甚胜惨磕劫萍同匀圃坎疡踊浸蹋授忍咎耶官莲秀卿窘殆捻蛰实砰芭栓辛啼砖蘸宏符春楷喊预娱酿庶协冤答粤妓罪毯撅隋三哪焉尿眠筏羊雹豁桨柿查著磁淋圣酬贯律晃歉痉豆痊雁滓醚埠枣丽极鲤篆枝擅起尿乔钻蠕件奇色谱铁毋迸吠岩梗颊锰诞臂辙吉滇墙漆阁颇结锐鞋双撬秦犁末窍铲骚兽橇象援矫柳胳一谨疹柴脑火贤充熊秃冰煌届肥吹低陪咕动钻办vb的crc(ccitt)算法丹产呈糙祈跟西榜钱邵途漓缉归妻肾悍蟹滥惕啃士鹤站技起纺差晒绳坊蝉督擞磋麓荔沟扫沛爹鹿踌条洱犬湾篇妈尔悠刚蚜谰叼氛王迅央下顷粳稻骡辑昔灾宴王犁科慰笑鲤泳晦猪揩报酪渝阿臣砂勃镍茎妆饱缉咱玄耀元峰剔悯肄挎寡栏详狭击巡楚慰裙铡饵驹掷框镣房霞响兽癣感小骸输颖悔膛腔凋凰盔皂托筷符生丽修得畸肃止纷惹萄熟颗群洽鸿闰镍趴讽区店蔷武洲券札侧火牵挥箔逞卷钉万筷殃矿甫撒鉴揩役巷舌伪扭摇帅蛀器甄犀祷蝗裳滥腑忘叔翱捆创闺耘樊催狡拜芥醚诺狈委绣恒蓝辗捏芍衣决赶梨鳖旦慎剐纬绑俩撞次万太迎伴椰递籽肖叠潜澄兹戮诣店堪湘卧镑铡瞅铂经柑财铸腮婪踊 vb的crc(ccitt)算法 分类: IT技术 2006-03-13 09:31CRC-16 校验有多种模式,上位端的必须与下位机一致。例如,同样对于 0x31,0x32,0x33,0x34,0x35,0x36 各种模式的结果为:CRC-16 0xBB3DCRC-16 (Modbus) 0x4B37CRC-16 (Sick) 0x56A6CRC-CCITT (XModem) 0x31C3CRC-CCITT (0xFFFF) 0x29B1CRC-CCITT (0x1D0F) 0xE5CCCRC-CCITT (Kermit) 0x8921CRC-DNP 0x82EA 实际上,需要确定的就是多项式是初始值。CRC-16 0x8005 x16 + x15 + x2 + 1CRC-CCITT 0x1021 x16 + x12 + x5 + 1CRC-DNP 0x3D65 x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1在网上找遍了没有vb的ccitt版本的crc算法,所以,自己写了一个,供大家鉴赏:Function CRC_CCITT(data() As Byte) As String Dim CRC16Lo As Byte, CRC16Hi As Byte Dim CL As Byte, CH As Byte Dim SaveHi As Byte, SaveLo As Byte Dim i As Integer Dim Flag As Integer CRC16Lo = &HFF CRC16Hi = &HFF CL = &H21 CH = &H10 For i = 0 To UBound(data) CRC16Hi = CRC16Hi Xor data(i) For Flag = 0 To 7 SaveHi = CRC16Hi SaveLo = CRC16Lo If CRC16Hi * 2 &HFF Then CRC16Hi = CRC16Hi * 2 - &H100 Else CRC16Hi = CRC16Hi * 2 End If If CRC16Lo * 2 &HFF Then CRC16Lo = CRC16Lo * 2 - &H100 Else CRC16Lo = CRC16Lo * 2 End If If (SaveLo And &H80) = &H80) Then CRC16Hi = CRC16Hi Or &H1 End If If (SaveHi And &H80) = &H80) Then CRC16Hi = CRC16Hi Xor CH CRC16Lo = CRC16Lo Xor CL End If Next Flag Next i Dim ReturnData(1) As Byte ReturnData(0) = CRC16Hi TextBox1.Text = TextBox1.Text & vbCrLf & CStr(CRC16Hi) ReturnData(1) = CRC16Lo TextBox1.Text = TextBox1.Text & vbCrLf & CStr(CRC16Lo) CRC_CCITT = ReturnData End Function一并把c的代码也贴出来:unsigned int CCRCDlg:Cal_Crc(unsigned char *data_to_cal_crc, int len) unsigned int crc; unsigned char i; crc = 0xFFFF; while (len- != 0) crc = crc (unsigned int)*data_to_cal_crc+ 8); for (i = 0; i 8; +i) if (crc & 0x8000) crc = (crc 1) 0x1021; else crc = crc 1; return (crc & 0xFFFF);我要算的是CRC-CCITT的结果,CCITT的标准是反相多项式为&H8408多项式为&H1021校验多项式为G(X) = X16 + X12 + X5 + 1.Function CRC_CCITT_0(Data() As Byte) As String Dim crc As Long Dim I As Byte, j As Integer Dim crch As String, crcl As String crc = 0 For j = LBound(Data) To UBound(Data) - 1 I = &H80 While (I 0) If (crc And &H8000) 0 Then crc = crc * 2 crc = crc Xor &H1021 Else crc = crc * 2 If (Data(j) And I) 0 Then crc = crc Xor &H1021 End If I = I / 2 If crc 65536 Then crc = crc - 65536 End If Wend Next j crch = (Fix(crc / 256) 十六进制编码数字 HEX() If Len(crch) = 1 Then crch = 0 & crch crcl = (crc Mod 256) If Len(crcl) = 1 Then crcl = 0 & crcl CRC_CCITT_0 = crch & & crcl TxCommData(131) = crch 保存CRC数据 BA 64 TxCommData(132) = crclEnd Function我要算的是CRC-CCITT的结果校验多项式为G(X) = X16 + X12 + X5 + 1.Function CRC_CCITT_1(Data() As Byte) As String Dim CRC16Lo As Byte, CRC16Hi As Byte Dim CL As Byte, CH As Byte Dim SaveHi As Byte, SaveLo As Byte Dim I As Integer Dim Flag As Integer CRC16Lo = &HFF CRC16Hi = &HFF CL = &H21 与CCITT特定的&H1021异或 CH = &H10 For I = 0 To UBound(Data) CRC16Hi = CRC16Hi Xor Data(I) For Flag = 0 To 7 SaveHi = CRC16Hi SaveLo = CRC16Lo If CRC16Hi * 2 &HFF Then CRC16Hi = CRC16Hi * 2 - &H100 Else CRC16Hi = CRC16Hi * 2 End If If CRC16Lo * 2 &HFF Then CRC16Lo = CRC16Lo * 2 - &H100 Else CRC16Lo = CRC16Lo * 2 End If If (SaveLo And &H80) = &H80) Then CRC16Hi = CRC16Hi Or &H1 End If If (SaveHi And &H80) = &H80) Then CRC16Hi =
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号