﻿{"id":976,"date":"2015-01-01T09:51:31","date_gmt":"2015-01-01T01:51:31","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=976"},"modified":"2014-12-31T10:33:33","modified_gmt":"2014-12-31T02:33:33","slug":"modbus%e5%8d%8f%e8%ae%aecrc16%e6%a0%a1%e9%aa%8cs7-scl%e7%ae%97%e6%b3%952%e6%9f%a5%e8%a1%a8%e6%b3%95","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=976","title":{"rendered":"ModBus\u534f\u8baeCRC16\u6821\u9a8cS7-SCL\u7b97\u6cd52:\u67e5\u8868\u6cd5"},"content":{"rendered":"<p>\u7ecf\u8fc7\u51e0\u5929\u7684\u6d4b\u8bd5\uff0c\u53d1\u73b0\u5728FC\u4e2d\u65e0\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u5728FB\u4e2d\u53ef\u4ee5\uff0c\u6240\u4ee5\u5c31\u5c06\u8fd9\u79cd\u7b97\u6cd5\u5199\u6210FB\u4e86\u3002<br \/>\n\u5728\u5b9a\u4e49\u65f6\u5bf9\u6570\u7ec4\u7684\u521d\u59cb\u5316\u8fd9\u4e2a\u4e8b\u56f0\u6270\u4e86\u6211\u597d\u591a\u5929\uff0c\u540e\u6765\u53d1\u73b0\u521d\u59cb\u5316\u4e0d\u80fd\u8d85\u8fc7255\u4e2a\u6210\u5458\uff0c\u53ef\u80fd\u662f\u7cfb\u7edf\u7684\u9650\u5236\u5427\u3002<br \/>\n\u4e0b\u9762\u662fCRC\u529f\u80fd\u6a21\u5757\u6e90\u4ee3\u7801\uff1a<!--more--><\/p>\n<pre class=\"lang:delphi decode:true \" >FUNCTION_BLOCK FB17\r\nCONST\r\n    \/\/ Constants\r\n    ARRAY_COUNT := 32;\r\nEND_CONST\r\n\r\nVAR\r\n    CRCHi: ARRAY [0..255] OF BYTE := [  \/\/ \u4e0d\u77e5\u9053\u8fd9\u91cc\u4e3a\u4ec0\u4e48\u53ea\u80fd\u521d\u59cb\u5316255\u4e2a\u6210\u5458\uff0c\u7cfb\u7edf\u9650\u5236\u5417\uff1f\r\n        16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41, 16#00,\r\n        16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1,\r\n        16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81,\r\n        16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40,\r\n        16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#01,\r\n        16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0,\r\n        16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80,\r\n        16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41,\r\n        16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00,\r\n        16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0,\r\n        16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80,\r\n        16#41, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41,\r\n        16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#01,\r\n        16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1,\r\n        16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81,\r\n        16#40, 16#01, 16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40,\r\n        16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1, 16#81, 16#40, 16#01,\r\n        16#C0, 16#80, 16#41, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#00, 16#C1,\r\n        16#81, 16#40, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81, 16#40, 16#01, 16#C0, 16#80,\r\n        16#41, 16#01, 16#C0, 16#80, 16#41, 16#00, 16#C1, 16#81];\r\n\r\n    CRCLo: ARRAY [0..255] OF  BYTE := [\r\n        16#00, 16#C0, 16#C1, 16#01, 16#C3, 16#03, 16#02, 16#C2, 16#C6, 16#06, 16#07, 16#C7, 16#05,\r\n        16#C5, 16#C4, 16#04, 16#CC, 16#0C, 16#0D, 16#CD, 16#0F, 16#CF, 16#CE, 16#0E, 16#0A, 16#CA,\r\n        16#CB, 16#0B, 16#C9, 16#09, 16#08, 16#C8, 16#D8, 16#18, 16#19, 16#D9, 16#1B, 16#DB, 16#DA,\r\n        16#1A, 16#1E, 16#DE, 16#DF, 16#1F, 16#DD, 16#1D, 16#1C, 16#DC, 16#14, 16#D4, 16#D5, 16#15,\r\n        16#D7, 16#17, 16#16, 16#D6, 16#D2, 16#12, 16#13, 16#D3, 16#11, 16#D1, 16#D0, 16#10, 16#F0,\r\n        16#30, 16#31, 16#F1, 16#33, 16#F3, 16#F2, 16#32, 16#36, 16#F6, 16#F7, 16#37, 16#F5, 16#35,\r\n        16#34, 16#F4, 16#3C, 16#FC, 16#FD, 16#3D, 16#FF, 16#3F, 16#3E, 16#FE, 16#FA, 16#3A, 16#3B,\r\n        16#FB, 16#39, 16#F9, 16#F8, 16#38, 16#28, 16#E8, 16#E9, 16#29, 16#EB, 16#2B, 16#2A, 16#EA,\r\n        16#EE, 16#2E, 16#2F, 16#EF, 16#2D, 16#ED, 16#EC, 16#2C, 16#E4, 16#24, 16#25, 16#E5, 16#27,\r\n        16#E7, 16#E6, 16#26, 16#22, 16#E2, 16#E3, 16#23, 16#E1, 16#21, 16#20, 16#E0, 16#A0, 16#60,\r\n        16#61, 16#A1, 16#63, 16#A3, 16#A2, 16#62, 16#66, 16#A6, 16#A7, 16#67, 16#A5, 16#65, 16#64,\r\n        16#A4, 16#6C, 16#AC, 16#AD, 16#6D, 16#AF, 16#6F, 16#6E, 16#AE, 16#AA, 16#6A, 16#6B, 16#AB,\r\n        16#69, 16#A9, 16#A8, 16#68, 16#78, 16#B8, 16#B9, 16#79, 16#BB, 16#7B, 16#7A, 16#BA, 16#BE,\r\n        16#7E, 16#7F, 16#BF, 16#7D, 16#BD, 16#BC, 16#7C, 16#B4, 16#74, 16#75, 16#B5, 16#77, 16#B7,\r\n        16#B6, 16#76, 16#72, 16#B2, 16#B3, 16#73, 16#B1, 16#71, 16#70, 16#B0, 16#50, 16#90, 16#91,\r\n        16#51, 16#93, 16#53, 16#52, 16#92, 16#96, 16#56, 16#57, 16#97, 16#55, 16#95, 16#94, 16#54,\r\n        16#9C, 16#5C, 16#5D, 16#9D, 16#5F, 16#9F, 16#9E, 16#5E, 16#5A, 16#9A, 16#9B, 16#5B, 16#99,\r\n        16#59, 16#58, 16#98, 16#88, 16#48, 16#49, 16#89, 16#4B, 16#8B, 16#8A, 16#4A, 16#4E, 16#8E,\r\n        16#8F, 16#4F, 16#8D, 16#4D, 16#4C, 16#8C, 16#44, 16#84, 16#85, 16#45, 16#87, 16#47, 16#46,\r\n        16#86, 16#82, 16#42, 16#43, 16#83, 16#41, 16#81, 16#80];\r\nEND_VAR\r\n\r\nVAR_INPUT\r\n    Byte_Array : ARRAY[0..ARRAY_COUNT-1] OF BYTE;\r\n    Count : INT;    \r\nEND_VAR\r\n\r\nVAR_OUTPUT\r\n    RESULT : WORD;\r\nEND_VAR\r\n\r\nVAR_TEMP\r\n    \/\/ Temporary Variables\r\n    hi: BYTE;\r\n    lo: BYTE;\r\n    index,i,datalength: INT;\r\nEND_VAR\r\n\r\n    \/\/ Statement Section\r\n    hi := 16#FF;\r\n    lo := 16#FF;\r\n    datalength := Count;\r\n    i := 0;\r\n    WHILE (datalength &gt; 0) DO\r\n        index := BYTE_TO_INT(hi XOR Byte_Array[i]);\r\n        i := i + 1;\r\n        hi := lo XOR crchi[index];\r\n        lo := crclo[index];\r\n        datalength := datalength - 1;\r\n    END_WHILE;\r\n    RESULT := hi;\r\n    RESULT := SHL(IN := RESULT, N:= 8) OR lo;\r\nEND_FUNCTION_BLOCK\r\n\r\nDATA_BLOCK DB17  FB17\r\n\/\/\r\n\/\/ Block Comment...\r\n\/\/\r\nBEGIN\r\n    \/\/ \u6570\u7ec4\u5b9a\u4e49\u65f6\u53ea\u8ba9\u521d\u59cb\u5316255\u4e2a\uff0c\u5269\u4e0b\u7684\u4e00\u4e2a\u53ea\u80fd\u5728\u8fd9\u521d\u59cb\u5316\u4e86\r\n    CRCLo[255] := 16#40;\r\n    CRCHi[255] := 16#40;\r\nEND_DATA_BLOCK\r\n<\/pre>\n<p>\u8c03\u7528\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:delphi decode:true \" >    dd[0] := 16#31;dd[1] := 16#32;dd[2] := 16#33;dd[3] := 16#34;dd[3] := 16#35;\r\n    \r\n    FB17.DB17(Byte_Array := DD \/\/ IN: ARRAY\r\n           ,Count := 5 \/\/ IN: INT\r\n           ); \r\n    MW112 := DB17.RESULT; \/\/ OUT: WORD\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u8fc7\u51e0\u5929\u7684\u6d4b\u8bd5\uff0c\u53d1\u73b0\u5728FC\u4e2d\u65e0\u6cd5\u5bf9\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u800c\u5728FB\u4e2d\u53ef\u4ee5\uff0c\u6240\u4ee5\u5c31\u5c06\u8fd9\u79cd\u7b97\u6cd5\u5199\u6210FB\u4e86\u3002 \u5728\u5b9a\u4e49\u65f6\u5bf9\u6570\u7ec4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[15,32,54],"class_list":["post-976","post","type-post","status-publish","format-standard","hentry","category-program","tag-crc","tag-modbus","tag-s7-scl"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=976"}],"version-history":[{"count":0,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/976\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}