C # implement Modbus ascii-based LRC Verification

Source: Internet
Author: User

If the request is verified as two ASCII codes, the LRC is the round-robin sum, the modulo of 256, and the complement code, that is, the checksum. If you want four digits, change the modulo value.

Two textbox are used;

Textbox1 for Character Input

Textbox2 for post-processing output

Button1 for response

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; namespace encoding {public partial class form1: FORM {public form1 () {initializecomponent ();} private void button#click (Object sender, eventargs e) {string original = textbox1.text; // read the original data from the wide text box // The data used for testing: a0030201c8 EE // start with a colon // A0 as the device address // 03 as the control instruction // 02 as the Data Length this test procedure only calculates the second bit // 01c8 as the data // EE it is the verification code string [] Buf = new string [original. length]; for (INT I = 1; I <original. length; I ++) // returns the first character of the string ":" omitted {Buf [I-1] = original. substring (I, 1);} int Len = (INT) (convert. tochar (BUF [5])-48; // read the data segment length, which is an ascii code, minus 48. it is more convenient to convert the string to decimal. // This sentence is not tested. It is used to calculate the data length of two digits: int Len = (INT) (convert. tochar (BUF [4])-48) * 10 + (INT) (convert. tochar (BUF [5])-48); int checksum = 0; For (INT I = 0; I <original. length-3; I ++) {checksum + = (INT) (convert. tochar (BUF [I]); // calculate the checksum in decimal format and} int sum = 255-checksum % 256 + 1; // calculate the checksum, LRC, ASCII verification string hex = convert. tostring (sum, 16 ). toupper (); // All ASCII characters in use are in uppercase. There are two digits in total. The principle is not much about string [] buff = new string [hex. length]; // the ASCII verification code that separates the last two digits for future comparison (INT I = 0; I 
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.