C# WPF host computer development (preparation of communication protocol)

[Disclaimer: All rights reserved. Reprinting is welcome. Please do not use it for commercial purposes. Contact email: [email protected]]

        As a host computer, a very important part of it is the need to communicate data with external devices. Many times, it is during this communication process that the host computer software has its own value. If the host computer can further build its own algorithm by collecting data, then the value of the host computer itself will be even greater. The USB, CAN, and 232 mentioned before are all communication media. In essence, the most important thing is the communication protocol.

        The so-called communication protocol is actually the method of communication and the format of the message. The method of communication generally represents who initiates communication first and who responds passively; the format of the message represents how much data is communicated each time and what meaning each data represents. That’s what the newsletter is all about.

1. Methods of communication

        Most host computers act as the active party when communicating, and the devices being communicated generally respond passively to messages. For example, a rolling shutter door device, a charging device, a robot device, etc. Basically, it will do what the host computer asks it to do. During the operation, the host machine continuously polls the current working status.

2. Where can I see the data protocol?

        There are still many such agreements. If it is a website server protocol, it is generally a private protocol, and you only need to connect with the developer of the corresponding website; if it is a device protocol, then we can get it online or offline. After purchasing the equipment, you can obtain the development agreement for the corresponding equipment through their after-sales personnel. Of course, many equipment suppliers now also publish corresponding communication protocols on their official websites or github in advance, which is possible.

3. What are the common protocols?

        For the field of industrial control, the most common protocol is the modbus protocol. Its communication format is very simple, which is a simple response mode. The host computer sends a request, and the device replies with a request. The main request only has two modes: read and write. The read and write messages are also very short, usually only a dozen bytes. This is also because the modbus device itself has a relatively simple function, a dozen bytes plus the corresponding bits. In fact, it is easy to explain most of the functions clearly.

4. Analysis of 232 communication protocol of a certain device

4.1 Read device protocol

        This is a read device protocol. The entire data is in hexadecimal form, and the data content is 0x0101006400087c13. The entire data is 8 bytes. The first 01 represents the device address, and the second 01 represents the function, here it is read. The third and fourth bytes are 0064, which is 100 when converted to decimal, which means the register address is 100. The fifth and sixth bytes are 0008, which means 8 registers need to be read. The seventh and eighth bytes are 7c13. This is the check code, which is generally calculated through the CRC algorithm.

4.2 Read device return protocol

        If we send the data in the form of 4.1, then if there is no problem, the host computer will read the returned data from the device. Assume this data is 0x0101010f118c. At this time, just like 4.1, we only need to follow the official example to parse it byte by byte. In terms of length, the returned data is only 6 bytes. The first byte 01 represents the device address. The second byte 01 represents function code 01, which is reading. The third byte represents the data length 01, which is 1 byte. The fourth byte 0F represents the returned data 0F. Fifth, the six bytes 0x118c represent the check code. The user can judge whether the sent data is incorrect based on the check code.

        You can think about it here. Assuming that the returned data is 4, what should the format be? For example, here, the returned data may be like this, 01+01+04+0000000f+check code 2 bytes, so the total length of all data is 9 bytes .

4.3 Write device protocol

        Compared with reading the device protocol, reading the device protocol is a little more complicated. The command to write the device here is 0x010f00640008010fcf59. There are two things that need to be noted. First, the function has changed from 01 to 0f. Second, compared with the reading device, there is an additional 010f, where 01 represents the length of the data to be written, and 0f represents the content of the data to be written. The other parts are not much different from the reading device protocol.

4.4 Write device return protocol

        Compared with writing device protocol, writing device return is much simpler. Basically, take away the length of the written data and the content of the written data, and what is left is the content returned by the writing device. Of course, the corresponding check code must also have changed.

5. What does the data content represent?

        ​ ​ ​ We discussed the device protocol earlier, but have you noticed that we did not discuss the content of reading and writing data. This is mainly because the meaning of specific data is actually related to the device itself. It may be a door, a light, a switch, a motor, a display screen. For all these, we will analyze the specific bits separately when using them. Bits and specific data meanings are enough. The main thing to learn this time is how to analyze the logic of this communication protocol.

6. Other things to pay attention to

        When writing a protocol, you still need to be very careful. Generally speaking, there are a few points to pay attention to;

        ​ ​ 1) Pay attention to byte order;

        2) Pay attention to the meaning of each bit;

        3) One receive and one send, and constantly check the return results of execution;

        4) Strictly according to the requirements of the equipment to ensure the legality of the input parameters;

        5) Be bold in doubting and careful in assumptions;

        ​ ​ 6) When problems occur, most of them are because the parameters are not set properly or the environment has changed. The probability of equipment problems is small.

Guess you like

Origin blog.csdn.net/feixiaoxing/article/details/134904456