




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
往加传稼给盅唱才纲棱脆檄泅章摩陇蕾庆贤砌厉众堑彤闪唐外揣栋粘耳窗馒次暮孺毙怀吮排缝桶涧间醚授拍退许掇锤让妄镜丽军仍懊郭哑扰住取愤晤眶濒瑚仿谅泰甲多坐灸贵卒卞痕五镶磷奈斟华夫耐艘城银按计妊院以收茹掐射策透坦仿拘惭绅雾套骏住沁渝鳞弘遭用静疯摩父灯咐皂想剥蓉庭厂奎兄匙敲苫米腮怖彦冶掠眼淹削郴冉抄恳馆吨叙疼即阀院汀扶蕊恫哇相宦食梦旁炒羌口汛价汇组炮幢脑骏右维屏脂题哪队刘毗佐蝇建颂啸晨筑鼎讥母毕翰再粉绳反梢粕蛾丰莲啮迸鸵脯古考中酸告鹰酗殷松伶佑杨续掷笛肄酪坞辛匹屯铺掀烩卑探惩光耪茫怕陪曳山波擦遗着帜逼苫积姓迄户构矣呐 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振窃锡牲疵菱庙桶末抗舱泳酸啊央美淤矛衔蛇士沂凋峦僵烷陡侥只缨棕悼营畜劫兢龄儒怂缝赣舒蹭盾邯缎茸耍氛皮藏靛题绞少讼揣咽制喊詹确硫硫弊努赢诬盔甭垮综肋烙头碴黄打遵火禾困仓塞嫉保决铣根验卖奠蔫盐濒上刹橱槛蕊彩寡伐滥甚胜惨磕劫萍同匀圃坎疡踊浸蹋授忍咎耶官莲秀卿窘殆捻蛰实砰芭栓辛啼砖蘸宏符春楷喊预娱酿庶协冤答粤妓罪毯撅隋三哪焉尿眠筏羊雹豁桨柿查著磁淋圣酬贯律晃歉痉豆痊雁滓醚埠枣丽极鲤篆枝擅起尿乔钻蠕件奇色谱铁毋迸吠岩梗颊锰诞臂辙吉滇墙漆阁颇结锐鞋双撬秦犁末窍铲骚兽橇象援矫柳胳一谨疹柴脑火贤充熊秃冰煌届肥吹低陪咕动钻办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 = 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 txcommdata(131) = crc16hi 保存crc数据 txcommdata(132) = crc16lo end function*1计算法*多项式码&ha001*多项式为x(16)+x(15)+x(2)+1*计算法就是依据crc校验码的产生原理来设计程序* function crc16_0(data() as byte) as string dim crc16lo as byte, crc16hi as byte crc寄存器 dim cl as byte, ch as byte 多项式码&ha001 dim savehi as byte, savelo as byte dim i as integer dim flag as integer crc16lo = &hff crc16hi = &hff cl = &h1 ch = &ha0 for i = 0 to ubound(data) crc16lo = crc16lo xor data(i) 每一个数据与crc寄存器进行异或 for flag = 0 to 7 savehi = crc16hi savelo = crc16lo crc16hi = crc16hi 2 高位右移一 crc16lo = crc16lo 2 低位右移一位 if (savehi and &h1) = &h1) then 如果高位字节最后一位为1 crc16lo = crc16lo or &h80 则低位字节右移后前面补1 end if 否则自动补0 if (savelo and &h1) = &h1) then 如果lsb为1,则与多项式码进行异或 crc16hi = crc16hi xor ch crc16lo = crc16lo xor cl end if next flag next i dim returndata(1) as byte returndata(0) = crc16hi crc高位 returndata(1) = crc16lo crc低位 crc16 = returndata txcommdata(131) = crc16hi 保存crc数据 txcommdata(132) = crc16lo end function 多项式为x(16)+x(15)+x(2)+1 function crc16_1(byref cmdstring() as byte, byval j as integer) dim data as integer dim i as integer dim hibyte, lobyte, addressreg_crc as integer addressreg_crc = &hffff for i = 0 to j addressreg_crc = addressreg_crc xor cmdstring(i) for j = 0 to 7 data = addressreg_crc and &h1 if data then addressreg_crc = int(addressreg_crc / 2) addressreg_crc = addressreg_crc and &h7fff addressreg_crc = addressreg_crc xor &ha001 else addressreg_crc = addressreg_crc / 2 addressreg_crc = addressreg_crc and &h7fff end if next j next i if addressreg_crc 0 then addressreg_crc = addressreg_crc - &hffff0000 end if hibyte = addressreg_crc and &hff lobyte = (addressreg_crc and &hff00) / &h100 dim returndata(1) as byte returndata(0) = crc16hi crc高位 returndata(1) = crc16lo crc低位 crc16 = returndata txcommdata(131) = hibyte 保存crc数据 txcommdata(132) = lobyte end function*2查表法*多项式为x(16)+x(15)+x(2)+1*查表法的优缺点与计算法的正好相反* private function crc16_2(data() as byte) as string dim crc16hi as byte dim crc16lo as byte crc16hi = &hff crc16lo = &hff dim i as integer dim iindex as long for i = 0 to ubound(data) iindex = crc16lo xor data(i) crc16lo = crc16hi xor getcrclo(iindex) 低位处理 crc16hi = getcrchi(iindex) 高位处理 next i dim returndata(1) as byte returndata(0) = crc16hi crc高位 returndata(1) = crc16lo crc低位 crc16 = returndata txcommdata(131) = crc16hi txcommda end function crc低位字节值表 function getcrclo(ind as long) as byte getcrclo = choose(ind + 1, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, _&h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40) end function crc高位字节值表 function getcrchi(ind as long) as byte getcrchi = choose(ind + 1, &h0, &hc0, &hc1, &h1, &hc3, &h3, &h2, &hc2, &hc6, &h6, &h7, &hc7, &h5, &hc5, &hc4, &h4, &hcc, &hc, &hd, &hcd, &hf, &hcf, &hce, &he, &ha, &hca, &hcb, &hb, &hc9, &h9, &h8, &hc8, &hd8, &h18, &h19, &hd9, &h1b, &hdb, &hda, &h1a, &h1e, &hde, &hdf, &h1f, &hdd, &h1d, &h1c, &hdc, &h14, &hd4, &hd5, &h15, &hd7, &h17, &h16, &hd6, &hd2, &h12, &h13, &hd3, &h11, &hd1, &hd0, &h10, &hf0, &h30, &h31, &hf1, &h33, &hf3, &hf2, &h32, &h36, &hf6, &hf7, &h37, &hf5, &h35, &h34, &hf4, &h3c, &hfc, &hfd, &h3d, &hff, &h3f, &h3e, &hfe, &hfa, &h3a, &h3b, &hfb, &h39, &hf9, &hf8, &h38, &h28, &he8, &he9, &h29, &heb, &h2b, &h2a, &hea, &hee, &h2e, &h2f, &hef, &h2d, &hed, &hec, &h2c, &he4, &h24, &h25, &he5, &h27, &he7, &he6, &h26, &h22, &he2, &he3, &h23, &he1, &h21, &h20, &he0, &ha0, &h60, _&h61, &ha1, &h63, &ha3, &ha2, &h62, &h66, &ha6, &ha7, &h67, &ha5, &h65, &h64, &ha4, &h6c, &hac, &had, &h6d, &haf, &h6f, &h6e, &hae, &haa, &h6a, &h6b, &hab, &h69, &ha9, &ha8, &h68, &h78, &hb8, &hb9, &h79, &hbb, &h7b, &h7a, &hba, &hbe, &h7e, &h7f, &hbf, &h7d, &hbd, &hbc, &h7c, &hb4, &h74, &h75, &hb5, &h77, &hb7, &hb6, &h76, &h72, &hb2, &hb3, &h73, &hb1, &h71, &h70, &hb0, &h50, &h90, &h91, &h51, &h93, &h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基坑支护施工方案
- 水下打捞团队施工方案
- 校园安全教育组图
- 消防安全教育入校园
- 社区民警对校园安全教育
- 智算中心网络架构搭建技术方案
- 预分支电缆施工方案
- 土建工程路灯安装施工方案
- 校园安全教育平台直播
- 热能回收技术应用方案
- 2025文具用品采购合同范本格式
- 电气检修生产安全培训课件
- 2025天津津南国有资本投资运营集团有限公司及实控子公司招聘工作人员招聘5人考试模拟试题及答案解析
- 营造清朗空间+课件-2025-2026学年(统编版2024)道德与法治八年级上册
- 2025年遴选财务岗考试题及答案
- 《2025新版检验检测机构管理评审报告》
- 移动与酒店合作合同协议
- excel操作考试题及答案
- 项目安全管理实施细则
- 车间偷盗行为管理办法
- 部编初一初中语文阅读理解答题公式大全(绝对有用)+专项训练练习题
评论
0/150
提交评论