Two methods to deal with communication problems are provided: one is event-driven method, and the other is query method.
Two ways to deal with communication with 1 MSComm control
MSComm controls provide the following two ways to handle communication: event-driven and query.
1. 1 event-driven mode
Event-driven communication is a very effective method to deal with serial communication. In many cases, you need to be notified when an event occurs, for example, when there are characters in the serial receiving buffer, or when there is a change on the character arrival or carrier detection (CD) or request to send (RTS) line. In these cases, you can use the OnComm event of the MSComm control to capture and handle these communication events. OnComm events can also check and handle communication errors. For a list of all communication events and communication errors, refer to the CommEvent property. During programming, you can add your own processing code to the OnComm event handler. The advantages of this method are timely program response and high reliability. Each MSComm control corresponds to a serial port. If your application needs to access multiple serial ports, you must use multiple MSComm controls.
1.2 query mode
The query method is event-driven in nature, but it is more convenient in some cases. After each key function of the program, you can query events and errors by checking the value of the CommEvent property. This approach may be better if the application is small and self-sustaining. For example, if you write a simple telephone dialing program, there is no need to generate an event for each character received, because the only character waiting to be received is the "OK" response of the modem.
The common attributes of 2.2. MSComm control
MSComm controls have many important properties, but first you must be familiar with several properties.
CommPort sets and returns the communication port number.
Sets and returns baud rate, parity, data bit and stop bit as a string.
PortOpen sets and returns the status of the communication port. You can also open and close ports.
Enter the characters returned and deleted from the receive buffer.
Output Writes the string to the transmission buffer.
The following are described separately:
CommPort property: Sets and returns the communication port number.
Grammar object. Comport [value] (value is an integer value representing the port number. )
Description At design time, the value can be set to any number between 1 and 16 (the default value is 1). However, if a nonexistent port is opened by using the PortOpen property, the MSComm control will generate an error 68 (invalid device).
Note: CommPort property must be set before opening the port.
RThreshold property: the number of characters to be received that are set and returned before the MSComm control sets the CommEvent property to comEvReceive and generates OnComm.
Syntax: object. Rthreshold [= value] (value integer expression indicating the number of characters to receive before generating the OnComm event. )
Note: After receiving characters, if the Rthreshold property is set to 0 (the default value), the OnComm event will not be generated. For example, if Rthreshold is set to 1, every character received by the receive buffer will cause the MSComm control to generate an OnComm event.
CTSHolding attribute: determines whether data can be sent by querying the status of CTS lines. "Permission to send" is a signal sent by the modem to the relevant computer, indicating that transmission can be made. This property is invalid at design time and read-only at run time.
Syntax: object. CTSHolding (boolean type)
Setting value of CTSHolding property of Mscomm control:
The real clearing line is high.
True and false clear delivery line is low.
Description: If the clear send line is low (CTSHolding = False) and times out, the MSComm control sets the CommEvent property to ComeventCTSTO and generates the OnComm event.
The clear transmission line is used for RTS/CTS (request transmission/clear transmission) hardware handshake. If it is necessary to determine the status of clearing the sending line, the CTSHolding property provides a manual query method.
SThreshold property: Before setting the CommEvent property to comEvSend and generating the OnComm event, the MSComm control sets and returns the minimum number of characters allowed in the transmission buffer.
Grammar object. SThreshold [= value] plastic expression representing the minimum number of characters in the transmission buffer before generating the OnComm event.
Description: If the Sthreshold property is set to 0 (the default value), the data transmission event will not generate the OnComm event. If the Sthreshold property is set to 1, the MSComm control will generate an OnComm event when the transmission buffer is completely empty. If the number of characters in the transmission buffer is less than the value, the CommEvent property is set to comEvSend and an OnComm event is generated. When the number of characters exceeds the threshold, the comEvSend event is activated only once. For example, if Sthreshold is equal to 5, comEvSend will only occur when the number of characters in the output queue is reduced from 5 to 4. If the number of characters in the output queue never exceeds Sthreshold, the comEvSend event will never occur.
Handshake constant
Constant value description
ComNone 0 did not shake hands.
Comxonoff 1xon/xoff shake hands.
Com RTS 2 requests to send/clear the send handshake.
Comrtsxonoff 3 request sending and clear sending can shake hands.
OnComm constant
Constant value description
ComEvSend 1 sends an event.
ComEvReceive 2 receives the event.
ComEvCTS 3 clears the transmission line change.
ComEvDSR 4 dataset ready line change.
ComEvCD 5 carrier detects line changes.
Ring detection.
End of 7 files.
Settings property: Sets and returns parameters of baud rate, parity, data bit and stop bit.
Syntax: object. Set [= value]
Description: When the port is open, the MSComm control will generate an error 380 (illegal property value) if the value is illegal.
The value consists of four setting values in the following format:
BBBB, doctor, master
BBBB is baud rate, P is parity, D is the number of data bits, and S is the number of stop bits. The default value of value is:
9600,N,8, 1
InputLen property: Sets and returns the number of characters that the Input property reads from the receive buffer.
Grammar object. input value
InputLen attribute syntax includes the following parts:
Value integer expression indicating the number of characters that the input attribute reads from the receive buffer.
Description: The default value of the InputLen property is 0. When InputLen is set to 0, using Input will make MSComm control read everything in the receive buffer.
If the Input character in the receive buffer is invalid, the input property returns a zero-length string (""). Before using Input, users can choose to check the InBufferCount property to determine whether there are the required number of characters in the buffer. This property is very useful when reading data from a machine whose output format is fixed-length data.