Hybrid application development-Serial Communication (1), application development-Serial Communication

Source: Internet
Author: User
Tags processing instruction

Hybrid application development-Serial Communication (1), application development-Serial Communication
CRC verification

  Cyclic Redundancy check(Cyclic Redundancy Check, CRC) is a hash function that generates a short fixed-digit verification code based on network data packets or computer files, it is mainly used to detect or verify possible errors after data transmission or storage. It uses the division and remainder principles for error detection. In the process of data transmission, no matter how perfect the design of the transmission system is, errors always exist, this error may cause one or more frames transmitted on the link to be damaged (a bit error occurs, where 0 is changed to 1, or 1 is changed to 0 ), the receiver receives the error data. To improve the accuracy of data received by the recipient as much as possible, the receiver must perform an error detection on the data before receiving the data. Only when the detection result is correct can the receiver truly accept the data. There are multiple detection methods, including parity check, internet verification, and cyclic redundancy check.

1 var order = {2 begin: '3a ', 3 number: '00', 4 CommandCode: {5 Inventory: '01', 6 StayQuiet: '02', 7 Select: '03', 8 ResetToReady: '04 ', 9 WriteAFI: '05', 10 LockAFI: '06', 11 ReadBlock: '07' 12}, 13 dataLen: '123', 14 orderWay: {15 anyAsk: '06', 16 anypsk: '07', 17 oneAsk: '26', 18 oneFsk: '27' 19 }, 20 checkCode: '', 21 end: '0d0a' 22 23}; 24 var orderString =''; 25 functi On makeOrder (a, B) {26 // splicing command (0001020027) 27 orderString = order. number + order. commandCode [a] + order. dataLen + order. orderWay [B]; 28 // generates the check code 29 order. checkCode = CRC. toModbusCRC16 (orderString); 30 // splicing command 31 orderString = orderString + order. checkCode; 32 // Processing Instruction string 33 orderString = orderString. split (''); 34 function topow (x) {35 return x. charCodeAt (). toString (16); 36}; 37 orderString = orderSt Ring. map (topow ). join (''); 38 // concatenate the final command 39 orderString = order. begin + orderString + order. end; 40 // number + 41 // order. number ++; 42 43}; 44 45 // crc verification code to generate 46 var CRC ={}; 47 48 CRC. CRC16 = function (data) {49 var len = data. length; 50 if (len> 0) {51 var crc = 0 xFFFF; 52 53 for (var I = 0; I <len; I ++) {54 crc = (crc ^ (data [I]); 55 for (var j = 0; j <8; j ++) {56 crc = (crc & 1 )! = 0? (Crc> 1) ^ 0x8408): (crc> 1); 57} 58} 59 var hi = (crc & 0xFF00)> 8 ); // high position 60 var lo = (crc & 0x00FF); // low position 61 62 return [hi, lo]; 63} 64 return [0, 0]; 65 }; 66 67 CRC. isArray = function (arr) {68 return Object. prototype. toString. call (arr) ===' [object Array] '; 69}; 70 71 CRC. toCRC16 = function (str, isReverse) {72 return CRC. toString (CRC. CRC16 (CRC. isArray (str )? Str: CRC. strToByte (str), isReverse); 73}; 74 75 CRC. toModbusCRC16 = function (str, isReverse) {76 return CRC. toString (CRC. CRC16 (CRC. isArray (str )? Str: CRC. strToHex (str), isReverse); 77}; 78 79 CRC. strToByte = function (str) {80 var tmp = str. split (''), arr = []; 81 for (var I = 0, c = tmp. length; I <c; I ++) {82 var j = encodeURI (tmp [I]); 83 if (j. length = 1) {84 arr. push (j. charCodeAt (); 85} else {86 var B = j. split ('%'); 87 for (var m = 1; m <B. length; m ++) {88 arr. push (parseInt ('0x '+ B [m]); 89} 90} 91} 92 return arr; 93}; 94 95 CRC. convertChinese = function (str) {96 var tmp = str. split (''), arr = []; 97 for (var I = 0, c = tmp. length; I <c; I ++) {98 var s = tmp [I]. charCodeAt (); 99 if (s <= 0 | s> = 127) {100 arr. push (s. toString (16); 101} 102 else {103 arr. push (tmp [I]); 104} 105} 106 return arr; 107}; 108 109 CRC. filterChinese = function (str) {110 var tmp = str. split (''), arr = []; 111 for (var I = 0, c = tmp. l Ength; I <c; I +++) {112 var s = tmp [I]. charCodeAt (); 113 if (s> 0 & s <127) {114 arr. push (tmp [I]); 115} 116} 117 return arr; 118}; 119 120 CRC. strToHex = function (hex, isFilterChinese) {121 hex = isFilterChinese? CRC. filterChinese (hex ). join (''): CRC. convertChinese (hex ). join (''); 122 123 // clear all spaces 124 hex = hex. replace (/\ s/g, ""); 125 // if the number of characters is odd, fill in a space of 126 hex + = hex. length % 2! = 0? "": ""; 127 128 var c = hex. length/2, arr = []; 129 for (var I = 0; I <c; I ++) {130 arr. push (parseInt (hex. substr (I * 2, 2), 16); 131} 132 return arr; 133}; 134 135 CRC. padLeft = function (s, w, pc) {136 if (pc = undefined) {137 pc = '0'; 138} 139 for (var I = 0, c = w-s. length; I <c; I ++) {140 s = pc + s; 141} 142 return s; 143}; 144 145 CRC. toString = function (arr, isReverse) {146 if (typeof IsReverse = 'undefined') {147 isReverse = true; 148} 149 var hi = arr [0], lo = arr [1]; 150 return CRC. padLeft (isReverse? Hi + lo * 0x100: hi x 0x100 + lo ). toString (16 ). toUpperCase (), 4, '0'); 151}; 152 // makeOrder ('ventory ', 'onefsk ');

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.