In the development of microcomputer control and data acquisition systems, it is often necessary to realize serial communication between PC and single-chip microcomputer through RS232 interface. In the DOS era, programmers need to have considerable hardware knowledge to write serial communication programs under PC. But now under VB, using the existing Microsoft Comm control control, only a small amount of program code is needed to complete the task easily and efficiently. What's more, Visual Basic is a visual programming language widely loved by programmers, and it can also be used to write beautiful applications under Windows.
1. Introduction to Microsoft Comm control
The Microsoft Comm control (MSComm) provided by Microsoft provides programmers with simplified serial communication programming under Windows, so that programmers do not need to master a lot of hardware knowledge. It provides two methods for processing serial communication: one is the event-driven method; the other is the query method.
1. Event-driven approach
This is a very powerful method to handle serial port activities. When the serial port receives or sends a specified amount of data, or when the state changes, the MSComm control will trigger the OnComm event, which can also capture errors in communication. When the application captures these events, it can check the value of the CommEvent property of the MSComm control to know the event or error that occurred, and perform corresponding processing. This method has the advantages of timely program response and high reliability.
2. Query method
You can query the values of certain properties of the MSComm control (such as the CommEvent property and the InBufferCount property) after each important program to detect events and communication errors. This may be more common for small self-contained programs.
The MSComm control has many important properties, some of which are as follows:
· CommPort: Sets or returns the communication port. When it is 1, it corresponds to COM1; when it is 2, it corresponds to COM2.
· Settings: Sets or returns the baud rate, parity, data bits, and stop bit parameters.
· PortOpen: Opens or closes the communication port.
· Input: Reads or deletes the data stream in the buffer.
· Output: Writes data to the send buffer.
· InputLen: Sets and returns the number of bytes read by the Input property from the receive buffer.
· InputMode: Sets and returns the type. When this property is 0, the data retrieved by the Input property is text; when it is 1, the data retrieved by the Input property is binary data. This property is particularly important for communication with the microcontroller.
2. Communication line connection
A serial port of the PC is connected to the 232 level port of the RS232 transceiver MAX232 through a three-wire cross connection via a cable, as shown in Figure 1. The logic level port of MAX232 is connected to the serial port of the microcontroller. Pins 4 and 6, and pins 7 and 8 of the 9-pin connector of the PC RS232 are not connected.
Figure 1 Block diagram of the communication interface circuit between PC and MCU
3. Programming Implementation
1. Implementing the function
Because this program is a communication demonstration program, its functions are relatively simple. The specific function is to use the keyboard to input a 6-byte (12-bit 0~9, A~F) binary number in the PC, and then click the communication command button with the mouse. The PC will send this binary number to the microcontroller. After receiving this number, the microcontroller will send it back as it is. After receiving it, the PC will display it on the window. The experimenter can compare the two data sent and received by naked eyes to check whether the communication is successful.
2. Communication Protocol
Baud rate: 19.2kb/s; no parity check; 8 data bits; 1 stop bit.
3. PC VB program
(1) Add a form to the project, name it frmcomm, and set its Caption property to Communication.
(2) Add two text boxes of the same size to the form, name them txtSend and txtRcv.
(3) Add a command button to the form, name it cmdcomm, and set its Caption property to Communication.
(4) Add an MSComm control to the form, name it MSComm1.
(5) Open the code window and add the following program code to the Click event of the cmdcomm control:
Private Sub cmdcommClick()
Dim Senddat(5) As Byte,Rcvdat() As Byte,
dattemp As Variant,i As Integer
cmdcomm.Enabled=False′ Disable the cmdcomm button
For i=0 To 5′ Get the sending data from the sending text box txtSend
Senddat(i)="&H" & Mid(txtSend.Text,i * 2+1,2)
Next i
MSComm1.CommPort=1′ Set the port number to 1
MSComm1.Settings="19200,N,8,1"′ Set the baud rate and other communication protocols
MSComm1.InputLen=6′ Set to read 6 bytes from the serial port at a time
MSComm1.PortOpen=True′ Open the serial port
MSComm1.InputMode=comInputModeBinary′ Read binary data from the serial port
MSComm1.Output=Senddat′ Send data
Do Until MSComm1.InBufferCount >= 6′ Query mode, wait for 6 bytes to be received
DoEvents
Loop
dattemp=MSComm1.Input′ Read data from the serial port to the variant variable
Rcvdat=dattemp′ Send data to the receiving binary array
txtRcv.Text=""
For i=0 To 5′ Received data is sent to the receiving text box txtRcv to display
txtRcv.Text=txtRcv.Text & Right("0" & Hex(Rcvdat(i)),2)
Next i
MSComm1.PortOpen=False′ Close the serial port
cmdcomm.Enabled=True′ Enable the cmdcomm button
End Sub
(6) Select Start - Run. Use the PC keyboard to enter the 6-byte binary data to be sent in the input text box, and then use the mouse to click the communication button.
4. MCU C51 Program
The crystal oscillator of the MCS-51 microcontroller is 11.0592MHz, and the serial port is set to mode 1, 10-bit asynchronous transmission. The query mode is used for receiving and sending. The program list is as follows:
#include
main() {
uchar temp,datmsg[6];
TMOD=0x20; //Set the baud rate to 19.2kb/s
PCON=0x80;
TH1=0xfd; TL1=0xfd;
TR1=1; //Start timer 1
SCON=0x50; //Set the serial port to 10-bit asynchronous transmission and reception, and allow //reception
while(1) {for(temp=0;temp<6;temp++) //Continuously receive 6 //bytes
{while(RI==0); RI=0;
datmsg[temp]=SBUF;
}
for(temp=0;temp<6;temp++) //Continuously send 6 //bytes
{SBUF=datmsg[temp]; while(TI==0);TI=0;
}
}
}
Conclusion
Due to space limitations, this program is only a demonstration reference program and has no practical significance. However, it demonstrates the general method of serial communication between PC and MCU and the method of processing binary data in VB, making the application of MCU and PC more closely integrated. Readers can add some handshake signals and error detection codes, such as parity check, cumulative sum check and cyclic redundancy check (CRC), etc., to make their own application.
Previous article:Issues to note when using MSP430F1121 interrupts
Next article:Design and implementation of I2C serial bus in 8031 single chip microcomputer application system
Recommended ReadingLatest update time:2024-11-16 16:36
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Virtualization Technology Practice Guide - High-efficiency and low-cost solutions for small and medium-sized enterprises (Wang Chunhai)
- 2024 DigiKey Innovation Contest
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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
- ARM co-founder launches "SAVE ARM" campaign to intervene in NVIDIA acquisition
- How to convert PWM signal into analog signal
- Uf2 Introduction
- Wi-Fi-7?
- Analog Series: RF: In the 5G Era, RF Front-End Shines
- STM32F103 vs MM32F103
- FPGA Design Tips
- UDP packet loss problem
- Live broadcast data collection | ST home appliance three-phase motor control solution
- New generation of ESD protection devices no longer require VCC connection