Realization of Serial Communication between PC and MCU under Windows Environment

Publisher:幸福自由Latest update time:2012-02-08 Source: 现代电子技术 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial communication is a very important means for computers to communicate with peripherals, monitor peripherals and obtain monitoring data collected by peripherals. It is widely used because it uses fewer transmission lines, has low cost and is easy to implement.

In the past, serial communication on Windows platform was mostly realized by using API functions provided by it. This method requires a lot of low-level settings, so it is cumbersome and difficult to understand. This article combines the author's experience in serial communication programming in the process of developing remote sensing CCD camera control subsystem, and introduces some methods of realizing serial communication between PC and single-chip microcomputer using ActiveX technology of Visual C++6.0 under 32b Windows operating system.

1 Hardware composition

The system uses 80C31 microcontroller as the lower computer and PC as the upper computer. The two receive or upload data through RS232 serial port. The wiring diagram is shown in Figure 1.
Since the level of RS232 signal is:

and the level of microcontroller serial port signal is TTL level:
logic 1 is greater than 3.6 V;
logic 0 is less than 0.3 V;

Therefore, the level conversion between the two must be performed. Here, the integrated level conversion chip MAX232 is used to perform RS232C/TTL level conversion. It uses a single +5 V for its operation, and is equipped with 4 1μF electrolytic capacitors to complete the conversion between RS232 level and TTL level. Its schematic diagram is shown in Figure 2. The converted serial port signals TXD and RXD are directly connected to the serial port of 80C31.

2 Communication Protocol

The communication protocol refers to an agreement between the two communicating parties. In the communication between the PC and the MCU, the PC assumes the main control task and is responsible for setting the control parameters. The MCU receives the PC instructions and uploads data or controls the controlled object according to the instruction information. The communication protocol is as follows:

RS232 serial asynchronous communication is adopted, with 1 start bit, 8 data bits, 1 stop bit, and no parity bit. The baud rate is 9600 b/s. The data transmission adopts binary transmission mode. Taking a data packet as an example, its frame header character is "~", the character value is 7EH (hexadecimal), followed by a data string, and the frame tail is the character "*" with a character value of 2A, then the entire data packet is "~XXXX*". Among them, "XXXX" is the agreed instruction code. Each "X" represents a hexadecimal number, such as "~1AFF*" where "1A" means commanding the lower computer system to set the gain, and "FF" represents a certain agreed gain parameter. After receiving this command, the lower computer system should immediately send a response command back to the PC, and then perform the corresponding operation. If the PC does not receive a response within 1 s, the PC will resend the command. According to the communication protocol, combined with the characteristics of Visual C++6.0, the following programming method is proposed.

[page]

3 Programming

3.1 Introduction to MSComm Control

MSComm (Microsoft Communication Control) is an ActiveX control provided by Microsoft to simplify serial communication programming under Windows. It provides two methods to handle communication problems in serial port programming: one is event-driven method; the other is query method.

Event-driven communication is a very effective method for handling serial port interactions. When a communication event occurs, the MSComm control triggers the OnComm event. The caller can capture the event and check its CommEvent property to confirm what kind of event or error occurred, and then handle it accordingly. The advantage of this method is that the program responds promptly and has high reliability.

The polling method is still event-driven in nature, but in some cases it is more convenient. After each key function in your program, you can poll for events and errors by checking the value of the CommEvent property. If your application is small and self-sustaining, this method may be preferable. For example, if you are writing a simple phone dialer, there is no need to generate an event for every character received, because the only character waiting to be received is the "OK" response from the modem.

Each MSComm control corresponds to a serial port. When using the MSComm control, one MSComm control can only correspond to one serial port at the same time. If the application needs to access multiple serial ports, multiple MSComm controls must be used.

The MSComm control has many important properties, among which the commonly used ones are as follows:

(1) CommPort sets and returns the communication port number. The default value is COM1, and 1 to 16 can be set.
(2) SetStrings sets and returns the string of baud rate, parity, data bits, and stop bits. The baud rate range is 300 to 19,200 b/s.
(3) PortOpen sets and returns the status of the communication port, and is also used to open and close the communication port.
(4) InputLen determines the number of characters read in each Input. The default value is 0, which means reading the entire content of the receive buffer.
(5) Input reads and clears the characters in the receive buffer.
(6) InBufferCount returns the number of characters received in the receive buffer. The receive buffer can be cleared by setting it to 0.
(7) Output writes the sent string or array to the send buffer.
(8) InputMode defines the way the Input property obtains data: 0 for text; 1 for binary.

3.2 Programming Implementation

From the MSComm control properties, we can see that MSComm can receive data in two different forms, namely text and binary. Since almost all control systems with single-chip microcomputer as the core transmit raw data in binary form, this article mainly discusses the use of binary form.

In Visual C++, a dialog-based application is used. After inserting the MSComm control in the dialog box, according to the communication protocol, the following code can be added to the initialization function OnInitDialog() of the main dialog box to open the serial port and set the properties. After


initialization, the next step is to send and receive data.

As can be seen from the communication protocol, the numbers in the data packet are represented by hexadecimal characters, so when sending data, some processing is required for the data. That is, first convert the string to be sent into the corresponding hexadecimal byte string, then convert it into a general VARIANT variable through the COleVariant class, and then send the data from the serial port through the SetOutput() function of the control. Taking the sending of 1 frame of data as an example, the main code for its implementation is as follows: [page]

As for the reception of data, we complete it by letting the MSComm control respond to the OnComm event, and add its response to the event through ClassWizard. When data arrives, it will notify the OnComm() function to process, thereby realizing asynchronous reception of data. Since the received data is variant data VARIANT, some processing must be done. The specific implementation code is as follows:



According to the above method, we can write a powerful serial communication program without spending a lot of work.

4 Conclusion

The program written in the above method was compiled by Microsoft Visual C++6.0 under Windows XP, and achieved satisfactory results in the serial communication process between the PC and the remote CCD camera control subsystem. At the same time, this software and hardware implementation method of serial communication can also be used for communication between PC and other peripherals, and its practical scope is very wide.

References

[1] Hope Book Creation Studio. Visual C++6.0 Technical Insider [M]. 5th Edition. Beijing: Beijing Hope Electronics Press, 1999.
[2] He Liming. Intermediate Tutorial of Single-Chip Microcomputer [M]. Beijing: Beijing University of Aeronautics and Astronautics Press, 1999.
[3] Xiaofeng Studio. Developing Serial Communication Programs Using ActiveX Controls in VCA++ [DB]. http://www.gjwtech.com/serialcomm.htm, 2002.

Reference address:Realization of Serial Communication between PC and MCU under Windows Environment

Previous article:I2C Bus Extension and I2C Virtual Technology for MCU
Next article:Sine Wave Distortion Control Based on Single Chip Microcomputer Technology

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号