Communication control MSComm usage

Source: Internet
Author: User
The MSComm control transmits and receives data through the serial port for the Application Program Provides the serial communication function. MSComm controls are very convenient for serial programming. programmers do not have to spend time learning more complex API functions. , VB, Delphi, and other languages. Microsoft Communications Control (MSComm) is an ActiveX control provided by Microsoft to simplify serial communication programming in windows. It provides an easy way for applications to send and receive data through a serial interface. Specifically, it provides two methods to deal with communication problems: the event-driven method and the query method.

1. MSComm control

The MSComm control provides two communication methods: event-driven and query.

1.1 event-driven approach

Event-driven communication is a very effective method for handling serial port interactions. In many cases, you need to be notified when an event occurs. For example, the serial Receive Buffer contains characters, or the carrier detect (CD) or request to send (RTS) when a character arrives online or a change occurs. In these cases, you can use the oncomm event of the MSComm control to capture and process these communication events. The oncomm event can also check and handle communication errors. For a list of all communication events and communication errors, see commevent attributes. In the programming process, you can add your own processing in the oncomm event processing function.Code. The advantage of this method is that the program responds promptly and has high 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 Method

The query method is actually event-driven, but in some cases, this method is more convenient. After each key function of the program, you can check the value of the commevent attribute to query events and errors. This method may be more desirable if the application is small and self-maintained. For example, if you write a simple dialing program, there is no need to generate an event for each received character, because the only character waiting for receiving is the modem's "OK" response.

2. Common attributes of the MSComm control

The MSComm control has many important attributes, but you must first be familiar with these attributes.

Commp ORT Set and return the communication port number.
Settings Set and return the baud rate, parity, data bit, and stop bit in the form of a string.
Portopen Set and return the status of the communication port. You can also open or close the port.
Input Returns and deletes characters from the receiving buffer.
Output Write a string to the transmission buffer.

The descriptions are as follows:

Comport attributes: Set and return the communication port number.

Syntax object. commp ORT [value] (value: an integer value indicating the port number .)

Description: during the design, the value can be set to any number from 1 to 16 (the default value is 1 ). However, if you use the portopen attribute to open a port that does not exist, the MSComm control produces error 68 (the device is invalid ).

Note: You must set the comport Attribute before opening the port.

Rthreshold attributes: Set the number of characters to be received before the MSComm control sets the commevent attribute to comevreceive and generates oncomm.

Syntax: object. rthreshold [= value] (value integer expression, indicating the number of characters to be received before the oncomm event is generated. )

Note: After receiving characters, if the rthreshold attribute is set to 0 (default), no oncomm event is generated. For example, if rthreshold is set to 1, the MSComm control generates an oncomm event when the receiving buffer receives each character.

Ctsholding attributes: Determines whether data can be sent by querying the status of the clear to send (CTS) line. Clear to send is the signal sent by the modem to the connected computer, indicating that the transmission can be performed. This attribute is invalid at design and read-only at runtime.

Syntax: object. ctsholding (Boolean)

The ctsholding attribute setting value of the MSComm control:

The true clear to send line is a high level.
False clear to send line is low.

Note: If the clear to send line is low (ctsholding = false) and times out, the MSComm control sets the commevent attribute to comeventctsto (clear to send timeout) and generates an oncomm event.

The clear to send line is used for the hardware handshaking of RTS/CTS (request to send/clear to send. To determine the status of the clear to send line, the ctsholding attribute provides a method for manual query.

For more information about handshaking protocols, see handshaking attributes.

Sthreshold attributes: Before the MSComm control sets the commevent attribute to comevsend and generates an oncomm event, it sets and returns the minimum number of characters allowed in the transport buffer.

Syntax object. sthreshold [= value]

Value integer expression, which represents the minimum number of characters in the transport buffer before the oncomm event is generated.

Note: If the sthreshold attribute is set to 0 (default), the data transmission event will not generate an oncomm event. If the sthreshold attribute is set to 1, the MSComm control generates an oncomm event when the transmission buffer is completely empty. If the number of characters in the transmission buffer is less than value, the commevent attribute is set to comevsend and an oncomm event is generated. The comevsend event is activated only once when the number of characters overlaps with sthreshold. For example, if sthreshold is equal to 5, comevsend only occurs when the number of characters in the output queue drops from 5 to 4. If there are no more characters than sthreshold in the output queue, the comevsend event will never happen.

Handshake constant

Constant Value Description
Comnone 0 No handshake.
Comxonxoff 1 Xon/xoff handshake.
Comrts 2 Request-to-send/clear-to-send handshake.
Comrtsxonxoff 3 Both request-to-send and clear-to-send handshakes are allowed.

Oncomm constant

Constant Value Description
Comevsend 1 Send events.
Comevreceive 2 Receive events.
Comevcts 3 The clear-to-send line changes.
Comevdsr 4 Data-set ready line changes.
Comevcd 5 The carrier detect line changes.
Comevring 6 Zhenling detection.
Comeveof 7 End the file.

Error constant

Constant Value Description
Comeventbreak 1001 Received interrupt signal
Comeventctsto 1002 Clear-to-send times out.
Comeventdsrto 1003 Data-set ready timeout
Comeventframe 1004 Frame Error
Comeventoverrun 1006 Port overspeed
Comeventcdto 1007 Carrier detect timeout
Comeventrxover 1008 Receiving Buffer Overflow
Comeventrxparity 1009 Parity error
Comeventtxfull 1010 Transmission buffer full
Comeventdcb 1011 An error occurred while retrieving the port device control block (DCB ).

Inputmode constant

Constant Value Description
Cominputmodetext 0 (default) Use the INPUT attribute to retrieve data in text format.
Cominputmodebinary 1 The INPUT attribute is used to retrieve data in binary mode.

Cdholding attributes: Query the status of the carrier detect (CD) line to determine whether the current transmission exists. Carrier detect is a signal sent from the modem to the connected computer, indicating that the modem is online. This attribute is invalid at design and read-only at runtime.

Syntax object. cdholding

Setting Value: the setting value of the cdholding attribute is:

Set Description
True The carrier detect line is high.
False The carrier detect line is low.

Note: When the carrier detect line is high (cdholding = true) and times out, 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 lost transfer in the Host application, such as a bulletin board, because callers can be suspended at any time (discard transfer ).

Carrier detect is also known as receive line signal detect (RlSD ).

Data Type: Boolean

Dsrholding attributes: Determine the status of the data set ready (DSR) line. The data set ready signal is sent from the modem to the connected computer, indicating that the operation is ready. This attribute is invalid at design and read-only at runtime.

Syntax: object. dsrholding

The object is located at the object expression, and its value is "applied" to the object in the list.

The dsrholding attribute returns the following values:

Value Description Description
True Data Set ready line height When the data set ready line is high (dsrholding = true) and times out, the MSComm control sets the commevent attribute to comeventdsrto and generates an oncomm event.
This attribute is useful when writing data set ready/Data Terminal ready handshaking routines for data terminal equipment (DTE) machines.
Data Type: Boolean
False Low data set ready line

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

Syntax: object. settings [= value]

Note: When the port is opened, if the value is invalid, the MSComm control produces error 380 (invalid attribute value ).

Value is composed of four set values in the following format:

"BBBB, P, D, S"

Bbbb indicates the baud rate, P indicates the parity, D indicates the number of data digits, and s indicates the number of stopped digits. The default value of value is:

"9600, N, 8, 1"

Inputlen attributes: Sets and returns the number of characters that the INPUT attribute reads from the receiving buffer.

Syntax object. inputlen [= value]

The inputlen property syntax includes the following parts:

Value integer expression, indicating the number of characters read by the INPUT attribute from the receiving buffer.

Note: The default value of the inputlen attribute is 0. When inputlen is set to 0, the MSComm control is used to read all the content in the receiving buffer.

If the inputlen character in the receiving buffer is invalid, the INPUT attribute returns a zero-length string (""). Before using input, you can choose to check the inbuffercount attribute to determine whether the buffer has a required number of characters. This attribute is useful when reading data from a machine whose output format is set to long.

Eofenable attributes: Determines whether the MSComm control looks for the EOF Characters During the input process. If the EOF character is found, the input is stopped and the oncomm event is activated. The commevent attribute is set to comeveof,

Syntax: object. eofenable [= value]

The eofenable attribute syntax includes the following parts:

Value Boolean expression to determine whether the oncomm event is activated when the EOF character is found, as described in "set value.

Value:

True when the EOF character is found, the oncomm event is activated.

False (default) The oncomm event is not activated when the EOF character is found.

Note: When the eofenable attribute is set to false, the oncomm control will not find the EOF character in the input stream.

3. error message (MS Comm control)

The following table lists the errors that can be captured by the MSComm control:

Value Description
380 Invalid attribute value: cominvalidpropertyvalue
383 Attribute is read-only comsetnotsupported
394 Attribute is read-only comgetnotsupported
8000 The operation is invalid when the port is opened.
8001 The timeout value must be greater than 0.
8002 Invalid Port Number comportinvalid
8003 The attribute is valid only at run time.
8004 Attribute read-only at runtime
8005 Comportalreadyopen has been enabled on the port.
8006 The device identifier is invalid or does not support this identifier.
8007 The device baud rate is not supported.
8008 The specified byte size is invalid.
8009 Default parameter error
8010 Hardware unavailable (locked by other devices)
8011 The function cannot allocate a queue.
8012 The device has not enabled comnoopen.
8013 The device has been enabled.
8014 Cannot Use comm notification
8015 The comm status comsetcommstatefailed cannot be set
8016 Cannot set comm event shielding
8018 The operation is effective only when the port is opened.
8019 Busy Devices
8020 Read comm device error comreaderror
8021 Internal error comdcberror when Retrieving Device Control Blocks for this port

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.