MSCOMM control is used in serial port programming

xiaoxiao2021-03-19  216

Auditory

MSCOMM control method of handling communication

CommPort property

Rthreshold property

CTSHOLDING attribute

Sthreshold property

CDHOLDING attribute

DSRHOLDING attribute

Settings attribute

Inputlen property

Eofenable property

Handshake constant

ONCOMM constant

INPUTMODE constant

wrong information

The MSCOMM control is transmitted and received by serial port, providing a serial communication function for the application. MSCOMM controls are very convenient when serial port programming, and programmers don't have to take time to learn more complex API functions, and can be used in languages ​​such as VC, VB, Delphi. Microsoft Communications Control (hereinafter referred to as MSCOMM) is an ActiveX control provided by Microsoft's simplified Windows serial communication programming, which provides an easy way for the application to send and receive data through serial interfaces. Specifically, it provides two methods of processing communication issues: First, an event-driven method, one is the query method.

1. MSCOMM control ways to process communication

The MSCOMM control provides the following two ways of processing communication: event driver and query mode.

1.1 Event Drive

Event Drive Communication is a very effective way to handle serial port interactions. In many cases, a notification is required when an event occurs, for example, in a serial port receiving buffer, or a character arrival or a change occurs in the carrier detect (CD) or REQUEST TO Send (RTS) line. In these cases, these communication events can be captured and processed using the ONCOMM event of the MSCOMM control. OnComm events can also check and process communication errors. All communication events and communication errors are reviews the commevent attribute. In the programming process, you can add your own processing code in the OnComm event handler. The advantage of this method is that the program response is timely and reliability. Each MSCOMM control corresponds to a serial port. If the application needs to access multiple serial ports, multiple MSCOMM controls must be used.

1.2 query mode

The inquiry method is essentially the event driver, but in some cases, this way is more convenient. After each key function of the program, you can query events and errors by checking the value of the commevent attribute. This method may be more desirable if the application is small and self-maintained. For example, if you write a simple phone dialing program, there is no need to generate events per received, because the unique characters wait for the received characters are the "determination" response of the modem.

2. Common properties for MScomm control

MSCOMM controls have many important properties, but must first be familiar with several properties.

CommPort settings and returns the communication port number.

Settings sets and returns baud rate, parity, data bit, stop bit in the form of string.

Portopen settings and returns the status of the communication port. You can also turn the port and close the port.

INPUT returns and deletes characters from the received buffer.

OUTPUT writes a string to the transfer buffer.

The following describes:

CommPort property settings and returns the communication port number.

Syntax object.commport [value] (Value is an integer value, indicating the port number.)

Description When designing, Value can be set to any number from 1 to 16 (the default is 1). However, if you open an unsaved port with the Portopen property, the MSCOMM control generates an error 68 (if the device is invalid).

Note: You must set the CommPort property before opening the port. Rthreshold Property: The number of characters to receive before the MSCOMM control is set to COMEVRECEIVE and generate ONCOMM, set and returned.

Syntax object.rthreshold [= value] (Value integer expressions, indicating the number of characters to receive before generating an ONCOMM event.)

Note When the RTHRESHOLD property is set to 0 (default value), the ONCOMM event is not generated when the RTHRESHOLD property is set to 0 (default). For example, set RTHRESHOLD to 1, and receive the buffer to receive each character causes the MSCOMM control to generate an ONCOMM event.

CTSHOLDING Properties: Determine if data can be sent by querying the status of the Clear To Send (CTS) line. The Clear TO Send is a signal that the modem sends to the associated computer, indicating that the transfer can be performed. This attribute is invalid when designing, is read-only at runtime.

Syntax: Object.ctSholding (Boolean)

CTSHOLDING Properties of MSCOMM Control:

The True Clear TO Send line is high.

False Clear TO Send is low.

Description: If the CTSHolding = false is low (CTSHOLDING = FALSE) and timeout, the MSCOMM control sets the commevent attribute to COMEVENTCTSTO (CLEAR TO Send Timeout) and generates an ONCOMM event.

The Clear to Send is used for RTS / CTS (Request to Send / Clear TO Send) hardware handshake. If you need to determine the status of the Clear to Send line, the CTSHOLDING property gives a method of manually query.

For more information, please refer to the handshaking property.

Sthreshold Property: MSCOMM Control Sets the COMMEVENT property to COMEVSEND, set and return to the minimum number of characters allowed in the transfer buffer before the ONCOMM event is generated.

Syntax Object.Sthreshold [= value]

Value plastic expressions represent the minimum number of characters in the transfer buffer before the ONCOMM event is generated.

Note: If the sthreshold property is set to 0 (default), the data transfer event does not generate an ONCOMM event. If the STHRESHOLD attribute is set, when the transfer buffer is fully empty, the MSCOMM control generates an ONCOMM event. If the number of characters in the transport buffer is less than Value, the Commmevent property is set to COMEVSEND and generates an ONCOMM event. The COMEVSEND event is activated once when the character number is crossed in the character. For example, if STHRESHOLD is equal to 5, COMEVSEND occurs only when the number of characters from 5 in the output queue will occur. If there is no character than these characters in the output queue, the COMEVSEND event will never happen.

Handshake constant

Constant value description

COMNONE 0 has no handshake.

Comxonxoff 1 xon / xoff handshake. COMRTS 2 Request-to-send / clean-to-send handshake.

COMRTSXONXOFF 3 Request-to-send and Clear-to-Send are shaking hands.

ONCOMM constant

Constant value description

Comevsend 1 Send an event.

ComeVReceive 2 Receive events.

COMEVCTS 3 CLEAR-TO-SEND line changes.

Comevdsr 4 Data-set Ready line changes.

Comevcd 5 Carrier Detect line changes.

ComeVRING 6 ringing detection.

ComeVeOf 7 file ends.

Error constant

Constant value description

COMEVENTBREAK 1001 receives an interrupt signal

COMEVENTCTSTO 1002 CLEAR-TO-Send Timeout

ComeventDSRTO 1003 Data-set Ready timeout

ComeVentFrame 1004 frame error

ComeVentoverrun 1006 port speed

ComeventCDTO 1007 Carrier Detect timeout

COMEVENTRXOVER 1008 receive buffer overflow

ComeTrxParity 1009 Parity Error

ComeventTxfull 1010 Transmission Buffer Full

ComeventDCB 1011 Retrieval Port Equipment Control Block (DCB) accident error

INPUTMODE constant

Constant value description

CominputModeText 0 (Default) Removes data in text through the input property.

CominputModeBinary 1 Checks data in binary in binary by input attributes.

CDHOLDING Properties: Determine whether there is transmission in the current state by querying the status of the Carrier Detect (CD) line. Carrier Detect is a signal from the modem to the associated computer, indicating that the modem is online. This attribute is invalid when designing, is read-only at runtime.

Syntax Object.cdholding

Set value: The set value of the cdholding property is:

Set description

True Carrier Detect line is high

False carrier detect line is low

Note: Note When the Carrier Detect line is a high level (CDHOLDING = true), the MSCOMM control sets the commevent attribute to COMEVENTCDTO (Carrier Detect timeout error) and generates an ONCOMM event.

Note that it is especially important to capture a loss in the host application, such as a bulletin board, because the caller can hang (abandon the transmission) at any time.

Carrier Detect is also known as Receive Line Signal Detect (RLSD).

Data type Boolean

DSRHOLDING Properties: Determine the status of the Data Set Ready (DSR) line. The DATA SETETY signal is sent by the modem to the connected computer, indicating the work preparation. This attribute is invalid when designing, is read-only at runtime.

Object.dsrholding

Object The object expression is represented, and its value is an object in the "application" list.

The DSRHOLDING property returns the following values:

Value description

True Data Set Ready Line High False Data Set Ready Line

Description: When the Data Set Ready line is a high level (DSRHOLDING = TRUE), the MSCOMM control sets the commevent attribute to COMEVENTDSRTO (data ready timeout) and generates an ONCOMM event.

This property is very useful when writing a Data Terminal Equipment (DTE) machine. This property is very useful.

Data type: Boolean

Settings Properties: Set and return baud rate, parity, data bits, stop bit parameters.

Syntax: Object.Settings [= value]

Note: When the port is opened, if the value is illegal, the MSCOMM control generates an error 380 (illegal attribute value).

Value consists of four set values, with the following format:

"BBBB, P, D, S"

BBBB is the baud rate, p is parity, D is the number of data digits, and S is the number of stop bits. Value's default is:

"9600, N, 8, 1"

Inputlen Properties: Set and return the number of characters read from the receiving buffer.

Syntax Object.inputlen [= Value]

Inputlen attribute syntax includes the following sections:

Value integer expression, describing the number of characters read from the received buffer.

Description: The default value of the Inputlen property is 0. When INPUTLEN is 0, use INPUT will enable the MSCOMM control to read all in the receiving buffer.