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.
[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.
Previous article:I2C Bus Extension and I2C Virtual Technology for MCU
Next article:Sine Wave Distortion Control Based on Single Chip Microcomputer Technology
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- AUTOSAR MCAL Principles and Practice
- Cortex Microcontroller Software Interface Standard (CMSIS)
- Practice of Serial Communication between Single Chip Microcomputer and Computer (Zhang Xiuguan)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【Download】STM32WB BLE application low power design
- MSP430F5529 Getting Started with Small Examples
- Qorvo's point-of-care diagnostic platform seems pretty good.
- Today's live broadcast: New requirements for connectors in 5G multi-scenario terminal applications and Molex's 5G connection solutions
- What will the elevators of the future look like?
- Award-winning live broadcast: Internet of Everything - Shijian Company joins hands with Microchip experts to discuss IoT solutions
- MS8211 to lithium battery
- In order to kill mosquitoes, the doctor DIYed a laser gun using Raspberry Pi!
- Thoughts on the development of single-chip microcomputers
- FPGA_100 Days Journey_Breathing Light