Using serial communication control programming in C + + Builder

Source: Internet
Author: User
Tags error code port number advantage

Absrtact: Serial port is a commonly used data transmission channel between computer and external serial device, so it is widely used because of the convenience of serial communication. This paper describes how to use serial communication control to program serial communication in C + + Builder.

First, the introduction

At present, in the use of computer data transmission, the commonly used is the serial communication mode. When you use C + + Builder to write serial communication programs, you can call Windows API functions, or you can take advantage of the MSComm controls in VB. When using API function to write practical application, it is often necessary to consider multithreading, so the program is not only very large, but also complex structure, poor inheritance and maintenance difficulties. However, the use of serial communication control is relatively simple, and powerful, safe and reliable performance. This article briefly describes the use of the MSComm control in C + + Builder programming.

Ii. Common Properties and events for MSComm controls

The MSComm control transmits and receives data through the serial port, providing serial communication to the application. Specifically, it provides two ways to deal with communication problems: one is the event-driven (Event-driven) method and the other is the query method.

Event-driven mode

When you use event-driven design programs, each time a new character arrives, or the port state changes, or an error occurs, the MSComm control will extract the OnComm event, and after the application captures the event, you can learn about the event or error that occurred by checking the CommEvent property of the MSComm control. thereby taking the appropriate action. The advantage of this method is that the program response is timely and the reliability is high.

Query method

The query is essentially an event-driven approach, but in some cases it is more convenient. After each key feature of the program, you can query for events and errors by checking the value of the CommEvent property. This method may be preferable if the application is small and is self sustaining.

Common Properties for 1.MSComm controls

Commport Property: Sets or returns the communication port number, can set to any value between 1 to 16, this system uses the default value 2;

Settings property: Sets or returns the baud rate, parity, data bit and stop bit as a string, the system uses the default value "9600,n,8,1";

PortOpen Property: Sets or returns the status of the communication port and opens and closes the port, either by setting the property to True or false to open or close the port;

Inbuffersize and Outbuffersize properties: Sets the amount of memory allocated for the receive and send buffers, in bytes, with a default value of 1024byte and 512byte respectively;

Inputlen property: Determines the number of characters that you want to move out of the receive buffer, and when you inputlen=0, remove all the characters from the receive buffer at a time;

Input property: Reads the data from the receive buffer and then removes the data from the buffer.

Output property: Passes the pending data to the send buffer.

InBufferCount and OutBufferCount properties: Determines the number of characters currently residing in the receive buffer waiting to be fetched and the send buffer is ready to be sent, the two properties are set to 0, and the contents of the receive and send buffers are cleared;

Inputmode property: Set to receive incoming data format, set to 0 in text form, set to 1 using binary format, the system is set to binary format for sending and receiving;

Sthreshold property: Save a OnComm event to generate the threshold value, the system set this property is 0, sending data does not produce ONCOMM events;

RThreshold property: Set when receiving a few characters to trigger the OnComm event, this system set this property to 1, each receive a character to produce a OnComm event;

2. Events for MSComm controls

The MSComm control uses only one event OnComm, using 17 values of the attribute commevent to distinguish between different trigger times. The following are the main ones:

(1) Commevent=1: The number of characters in the transport buffer is less than sthreshold (a property value that can be set).

(2) commevent=2: The RThreshold (property value can be set) character is received in the receive buffer, which can be used to write the process of receiving data.

(3) Commevent=3: The CTS line is changed.

(4) COMMEVENT=4: The DSR line is changed.

(5) When commevent=5: the CD line changes.

(6) Commevent=6: The ringing signal is detected.

The other 10 cases are the result of a communication error, the error code.

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.