Serial Communication (MSComm) development process records (including a relatively BASIC program version that can communicate)

Source: Internet
Author: User

What I want to do is a serial port tool similar to a Super Terminal for AT command applications. The option card interface sends at commands to communicate with the data card, facilitating R & D personnel to read and write data cards and testers to test, improve work efficiency.
Start with two images:


When receiving this task, we must first analyze the two main parts of the software. What are the main technical difficulties? The software includes two parts: interface design and serial communication programming. The interface is designed with VC ++ 6.0, which can be installed without a client. net, it is easy to design the interface with VC, but it is only a matter of time. Serial Communication is completely out of the box, and it does not know how to communicate with the data card through the serial port. This is the most difficult problem. The at command also does not fully understand and needs to be focused on.
After the analysis is clear, we need to check the information. First, we need to clarify the concept, principles, and methods of serial communication. It is time-consuming to query materials, but it is also critical to find good information to better understand, better understand, and better choose. When you do not know what you want to search, you do not know what you want to find the information you want, it is time-consuming to search for items that do not meet the requirements. But there is no way to do this. Be patient.
I initially wondered what the serial communication is, and the data card I used was a USB port. Can I operate USB ports through serial communication. Later, I figured out that the data card driver was installed with a virtual serial port, which could be operated as a common serial port. What is the difference between PC serial communication and single-chip serial communication? You do not need to know the serial communication protocol or handshake.
After reading the materials extensively, you will know that PC serial communication is nothing more than setting up the serial port, enabling the serial port, communication, and closing the serial port. It took me a long time to get to know me. This is what we call simplicity. I have never done it, so it is not clear that everything is difficult at the beginning. It takes a lot of effort to get started.
If you have an understanding of serial communication, you need to consider how to implement it. Before selecting a method, you must first find out which methods can be selected. If you do not understand VC, you may only know your own programming implementation. How can I compile my own programming? After checking it, I may know that I want to use Windows APIs. But it is best not to start with the method you take for granted. I am a cainiao, so I should first look at what others are using, and then I will know that the serial communication can use Windows APIs and MScomm controls (and other serial communication controls ), other people develop methods to implement serial ports (such as cserialport.
(For details, refer to www.enet.com.cn/article/2006/0216/a20060216501544.shtmland Gong Jianwei's vcpus #/turbo C serial communication programming practices)
The method is missing. No choice. If there are more methods, you can choose to be familiar with them or feel a little simpler, but it will also be a headache and you don't know which one to choose. Therefore, you need to have an understanding of several methods and then compare them.
I chose the MSComm control method. I didn't know what controls are and how to use them. However, there are a lot of materials on the Internet, and they are easy to use. Let's try it.
Programming can be referenced, but cannot depend on. At the beginning, I wanted to use the accompanying book code of Yan Jianwei, and I found that it was useless. After thinking of some previous experiences, I feel that the Code provided by others is generally not used directly and has bugs. I still need to understand it and modify the code of others. However, this has saved a lot of time. At least the Framework is available, and it is very painful to compile it from the beginning!
As for how powerful this control is, it is only known after it is used. I checked a lot in the Forum and found many people asked if this control could be used for virtual serial communication. I am a little disheartened to read these posts. First, I thought the control was easier to use and there were ready-made programs. We can see that there are so many problems, some people suggest using other controls, and other methods are recommended. At least it is not said that this can be done. Then I am not even familiar with it. I want to change the solution. However, I am very lazy. After thinking about it, I think it should be done first. If you have never done it, you have no right to speak. Sometimes you have to rely on your own. I am in a hurry and have limited time. I need to complete the serial port communication tool of AT command within one month. Of course, it includes two parts: Serial Communication and interface programming. All of them are ignorant, starting from scratch.
It seems that MSComm is still very powerful, although it can only be used based on the form. The following describes the problems I encountered during the development process.
1. First, develop a simple serial communication program based on the dialog box.
The introduction of the MSComm control, Yi Jianwei, has been mentioned in this book, and I am so lazy. Refer to the book for the creation process.
In the serial communication program section, you must first configure the serial port. In the oninitdialog () initialization function of the dialog box, you can use the get/set function pair provided by MSComm to easily complete the configuration, including the serial port number and baud rate, parity Check, data bit, input/output buffer size, input mode (set as binary mode), rthreshold and sthreshold related to event (event messages are automatically generated after certain conditions are met. For details, see the description of MSComm control attributes, functions, and events in chapter 3 of the exercise.
Then there is the programming of the data sending and receiving part. After you click the send button, the message is received in oncomm, the message event processing function. The receiving area does not display the response after the source code is sent in the writing script. It took a lot of effort to solve this problem. I read the first and third chapters of the book several times and then thought about what the problem would be. Whether the serial port is set up or not. Is the data sent successfully? How can I check whether the message is sent successfully? If the message is sent successfully, why is no response. At the beginning, I used debug to check whether the sent data is correct, because the setoutput function of the control requires that the parameter be of the variant type and be converted from the sctring type. Check and find that there is no problem with data conversion. I thought, the data is correct, but the data has not been sent. If the data is sent, the serial port configuration is correct. How to view it. Sthreshold and oncomm () are used (). Set sthreshold to 1, that is, an event is generated after the sending buffer is empty. The event is processed in oncomm, and "data has been sent" is displayed in commevent = 1 ". The data is also sent successfully. That's even more depressing. Why didn't we return it? What is the problem with the data card? However, data cards can communicate with super terminals. The example command section in the document that comes up with the AT command is written as follows: at <CR>, the Super Terminal also presses the Enter key to send. In this case, is there a problem with the command format. The carriage return is not simply a part of the command, but a part of the command. When I try to add the \ n string, I still cannot find it. I had to go to the Forum and check it. After a long time, I saw someone saying that I couldn't do anything at first. Then I tried to add \ r \ n. I tried it, and it was OK. Later, I saw an article about linefeeds, which was written in my article "AT Instruction Set related content", explaining why \ r \ n was added.
The two most important parts of this Code are the sending and receiving processes. Type conversion is required. You need to understand.
2. Interface Design
The interface design is completed quickly. First, you can use the property page to check. Then we made a routine in the book we borrowed, with only one property page. Read the code. Although you don't have to design it yourself, you can't be too lazy. You still need to understand the various files and sections of the Code of the project. Otherwise, how do you know how to modify it. It is easy to add two property pages. I have a clear understanding of the engineering structure of the dialog box interface. This is very important.
There is also a complete part of the back-end interface. Previously, we just made a look. We also needed multiple controls and variables and message functions. You have to understand this. The controls use a lot of online materials and can be found online if you want the controls to implement certain attribute functions. However, you must learn to add variables and functions to controls, especially to distinguish between control types and numeric types. Let's check the difference. Check the difference. Just do it on your own. You can also add message processing functions for the class. In short, you must do it yourself.
3. Integration of two parts
I thought it was half done. After completing the second part, I thought it was okay. It was just the integration of serial communication and interface. Of course, this integration has also been expected to have problems. But it actually took me more time and effort.
This section mainly encounters two problems: first, how to apply MSComm to multiple attribute pages, and second, how to send and receive multiple AT commands simultaneously.
The consensus on the internet is that a dialog box corresponds to a MSComm control. A MSComm control corresponds to a serial port. Many people are also asking how to use MSComm on the property page. They say no, but they should use other methods. I almost vomited blood. If this is not the case, I am not finished. How can this problem be solved. The first internship task cannot be completed. It is too late to redo it using other methods. No way, just stick your head to it and feel hopeful.
I thought about the possible solution: 1) a dialog box is used with MSComm. I use one for each dialog box, which is equivalent to a separate operation, however, these controls operate on the same serial port. When the page is switched over, the serial port is closed and re-opened on the other page. At that time, it was a failure. Some unknown errors caused by the control, but now I think this method should work too well, but it will not be so thorough, if you cannot see it, you can use another method;
2) I think of the method of applying MSComm in the MDI. This method is to apply the control in a single text interface, that is, it is separated from the dialog box. Of course, this method only seems to be out of the dialog box. It actually places the control in the aboutbox dialog box that has never been used, in this method, you must manually add a lot of content to the object of the text view. In this way, the control can be used in the text interface. Based on this method, we can also try to add the MSComm control to only one dialog box, while other property pages use the manual method similar to the MDI example to create MSComm, instead of dragging a widget to the dialog box. I think the most important reason for manual creation is the statement m_mscomm.create (null, 0, crect (,), this, idc_mscomm1. In this way, we can use it on each property page. However, there may be many problems when using the same control on multiple pages. I would like to give two examples. First, you must disable the serial port when you open the serial port on a page and exit, otherwise, an error occurs when you switch to another page. You must reconfigure the serial port on each property page. Otherwise, an error occurs. You do not need to set other property pages on one property page; now, we can explain why we must disable the serial port, because after the serial port is opened, we can no longer reset the serial port. This will cause an error. For example, after opening the serial port, you still need to reconfigure the serial port on another property page. For example, if you do not write this statement, it is set to 1. If you do not write this statement, the assertion error will occur again, this is because the serial port attribute is changed without being closed.
Another problem is the issue of sending more commands. You must learn to use DEBUG to observe the data and run it. I used a method to send an instruction to receive a corresponding instruction, and then called the sending instruction in oncomm's handling of the buffer event. Instead of sending and receiving all data. The order of received data is messy. Of course, there may be other better solutions. I'm tired and will not try again later. Here, we need to pay attention to the latency issue. If multiple commands are sent together, the latency must be added. I don't need this method, but I feel uncomfortable. It's not a standard solution.
4. Details, leakage detection, and code specifications
This step is close to success. The layout and beautification of interfaces and code optimization and standardization are essential. Especially for the code, the format is still immature. Once it comes to naming, the variable is M, N, P, and so on. The code can also be in a function, just inside a function, like to get together. Approved by the supervisor. But the most memorable thing is to be bug-free. This is because we thought it would be better to implement the function. As for those bugs, we just need to standardize them during operations. I always want to be lazy. However, there is no way to solve the various bugs. At first, I felt troublesome, and later I thought it was quite interesting. Every time I solved it, I was very happy. When the last bug was lost, I felt that it was not difficult to compile a software that could implement certain functions. It was a sense of accomplishment to compile a software without a bug.
Three major gains: msdn, debug, and nobug
Paste a program to provide you with a convenient and basic example. You need to modify the com Number:
You can change the serial port number by yourself.

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.