Article directory
foreword
This article introduces the use of ActiveX control MSComm to realize high-performance serial port transceiver.
1. What is MSComm
MSComm is a serial communication control, and each MSComm control corresponds to a serial port. If you need to access multiple serial ports, you must use multiple MSComm controls.
MSComm is an ActiveX control, which can control the sending and receiving of serial port data on PC, and supports query mode and interrupt mode (called event-driven mode under Windows).
- Interrupt mode: When data arrives at the serial port or data is written into the buffer of the serial port, an interrupt will be triggered, and OnComm can be used to capture the event and process it. This method responds in a timely manner and is more efficient than the polling method.
- Query method: In fact, it is still event-driven, and you need to check the CommEvent property value to query events.
2. MSComm control download
Click this link: MSComm Control
3. Registration of MSComm control
1. Put the four files MSCOMM.SRG, MSCOMM32.DEP, MSCOMM32.oca, and mscomm32.ocx downloaded above into C:\windows\system32 (32-bit system) or C:\Windows\SysWOW64 (64-bit system) Under contents.
2. Find the "cmd.exe" file under "this directory", right-click to run it as an administrator, enter: regsvr32 mscomm32.ocx in the pop-up dialog box, and press Enter. After the control is successfully registered, as shown in the figure below.
Fourth, use the MSComm control
1. Place controls on the front panel
①. Open the front panel, right-click -> container -> ActiveX container
②, right-click the ActiveX container -> Insert ActiveX object
③, select Microsoft Communication Control, version 6.0, click OK to get the MSComm control shown in the figure below
④, right-click the MSComm control, Select "Property Browser" to get the properties of the control
2. Common attributes of MSComm
- CommPort : Set and return the communication port number
- Settings : Set and return baud rate, parity, data bits, stop bits as a string
- PortOpen : Set and return the state of the communication port. Ports can also be opened and closed
- Input : returns and removes characters from the receive buffer
- InputLen : Sets and returns the number of characters the input property reads from the receive buffer
- Output : Write a string to the transfer buffer
- RThreshold : Before setting the CommEvent attribute to comEvReceive and generating OnComm, set and return the number of characters to be received
- CTSHolding : Determine whether data can be sent by querying the state of the CTS line
- SThreshold : Set and return the minimum number of characters allowed in the transmission buffer before setting the CommEvent attribute to comEvSend and generating the OnComm event
- CDHolding : Determine whether there is current transmission by querying the state of the CD line
- DSRHolding : Determine the state of the DSR line
- EOFEnable : Determines whether the MSComm control looks for end-of-file (EOF) during input
3. Events of MSComm control
The MSCOMM control only uses one event OnComm, and uses seventeen values of the attribute CommEvent to distinguish different triggering opportunities. There are mainly the following:
- When CommEvent=1: the number of characters in the transmission buffer is less than Sthreshold (settable attribute value)
- When CommEvent=2: Rthreshold (settable attribute value) characters are received in the receiving buffer, and the process of receiving data can be written by using this event
- When CommEvent=3: CTS line changes
- When CommEvent=4: DSR line changes
- When CommEvent=5: CD line changes
- When CommEvent=6: the ringing signal is detected
The other ten situations are generated when communication errors occur, that is, error codes.
5. Realize serial port sending and receiving
1. Build a virtual serial port
Refer to my previous blog: https://blog.csdn.net/qq_41839588/article/details/131936554?spm=1001.2014.3001.5501
2. Send test
The program uses the COM8 port, so open a virtual serial port COM9, COM9 (send) --> COM8 (receive)
3. Acceptance test
The program uses the COM8 port, so open a virtual serial port COM9, COM8 (send) --> COM9 (receive)
4. The core program block diagram of the rear panel
6. Program self-pickup
Link: https://download.csdn.net/download/qq_41839588/88104522
My qq: 2442391036, welcome to communicate!