Use the MSComm control to write the serial port Program (from Baidu)

Source: Internet
Author: User
MSComm acts as a serial communication control Program Serial Communication Programming saves a lot of time. It is very easy to add a MSComm control to a dialog box-based application. You only need to perform the following operations:
Open "Project-> Add to project-> components and controls-> registered ActiveX controls", and select control: Microsoft Communication Control, version 6.0 to insert to the current project. In this way, MSComm. cpp and MScomm. H files related to cmscomm are added to the project. During programming, you only need to drag the MSComm control in the control dialog box to your application dialog box.
The MSComm control provides two communication methods:
1. event-driven communication is a powerful method for handling serial port activities. For example, when a character arrives or changes on the CD (carrier detect) line or the request to send line, you can use the oncomm event of the MSComm control to capture and process these communication events. Oncomm can also capture and handle communication errors.
2. Check the value of the commevent attribute after each important program function to detect events and communication errors.
Each MSComm control used corresponds to a serial port. To access multiple serial ports in an application, you must use multiple MSComm controls. You can modify the interrupt address of the serial port address on the Windows Control Panel.
The MSComm control has many important attributes,
Comport attribute: Set and return the communication port number.
Syntax: object. commp ORT [value] (value: an integer value indicating the port number .)
Note: during 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 ).
Rthreshold attribute: specifies the number of characters to be received before the MSComm control sets the commevent attribute to comereceive 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 attribute: 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:
1. True clear to send line is high.
2. 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.
Sthreshold attribute: Set and return the minimum number of characters allowed in the transport buffer before the MSComm control sets the commevent attribute to comevsend and generates an oncomm event.
Syntax: object. sthreshold [= value] value integer expression, representing 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.
Cominputmodetext 0 (default) retrieves data in text format using the INPUT attribute.
Cominputmodebinary 1 retrieves data in binary mode through the INPUT attribute.
Cdholding attribute: 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 carrier detect line is high
False 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.
It is particularly important to capture a lost transfer in a host application, such as a bulletin board, because callers can suspend at any time (discard the transfer ).
Carrier detect is also known as receive line signal detect (RlSD ). Data type boolean
Dsrholding attribute: determines 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
True data set ready line height
False data set ready line low
Note: When the data set ready line is high (dsrholding = true) and times out, the MSComm control sets the commevent attribute to comeventdsrto (data preparation times out) 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
Settings attribute: 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 is: "9600, N, 8, 1"
Inputlen attribute: Set and return the number of characters that the INPUT attribute reads from the receiving buffer.
Syntax: The object. inputlen [= value] inputlen attribute 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, input will make the MSComm control read all in the receiving buffer.

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.