In-depth analysis of Visual C + + serial communication Programming in the detailed _c language

Source: Internet
Author: User
Tags function prototype readfile
You can use Visual C + + to design asynchronous serial communication programs in a Windows environment using different methods. one way to use the serial port API functions provided by Windows system is to use the ActiveX control Mscomm.ocx provided by Microsoft Company directly. It is relatively simple to use the Mscomm.ocx control to design the serial port, as long as the properties, events and methods of the control are set and operated, the simple serial communication function can be accomplished. The serial port API functions that are provided directly using Windows systems are relatively flexible. In the experiment, you can program any one of them according to your own situation. The following is a brief introduction to programming using the serial port API functions provided by Windows system

In Windows systems, serial ports and other communication devices are processed as files. The function used to open, close, send, and receive the serial port is the same as the function that operates the file. Generally speaking, the programming of asynchronous serial communication with Visual C + + can be divided into 4 stages, which are serial port opening stage, serial port State value reading and property setting stage, serial data sending and receiving phase, and serial port shutdown.

(1) Open serial port
Before you perform all operations on a serial port, first open it. The serial port can be opened using the CreateFile function, and the CreateFile function will return a handle to be used in various operations that are subsequently associated with the serial port. As with file operations, the serial port can also be specified as read access, write access, or read/write access by using CreateFile to open the serial port.
Copy Code code as follows:

HANDLE CreateFile (
LPCTSTR lpFileName
DWORD dwdesiredaccess
DWORD Dwsharedmode
Lpsecurity_attributes lpsecurityattributes
DWORD dwcreationdisposition
DWORD dwFlagsAndAttributes
HANDLE hTemplateFile
);

When the call succeeds, CreateFile returns a handle to the open file that will be used in subsequent call functions associated with the serial port. If the call fails, CreateFile returns INVALID_HANDLE_VALUE.
(2) The State Reading and property setting of the serial port
Once the serial port is turned on, the properties of the serial port can be set. Because of the complexity of the properties of the serial port, it is commonly used to read the current state value of the serial port, and then modify the method on this basis.
Gets the current state of the serial port
Copy Code code as follows:

BOOL Getcommstate (
HANDLE hfile
LPDCB LPDCB
);

The first parameter of the Getcommstate function hfile is returned by the CreateFile function to the handle that points to the open serial port. The second parameter points to the device control block DCB. DCB is a very important data structure in which almost all of the serial port properties and states are stored in the member variables of the struct.
set up the serial port
The Windows system uses the SetCommState function to modify the current parameter configuration of the serial port. The SetCommState function is declared as follows:
Copy Code code as follows:

BOOL SetCommState (
HANDLE hfile
LPDCB LPDCB
);

The first parameter of the Getcommstate function hfile is returned by the CreateFile function to the handle that points to the open serial port. The second parameter points to the device control block DCB. If the function call succeeds, the return value is not 0; If the function call fails, the return value is 0. When an application needs only to modify the configuration values of a subset of the serial ports, the current DCB structure can be obtained by the Getcommstate function, then the parameters are changed, and the SetCommState function is set to the modified DCB to configure the serial port.
allocating receive and send buffers for serial ports
When a serial port is open, you can assign a send buffer and a receive buffer for the serial port. The configuration of the serial port send buffer and receive buffer can be implemented by the function Setupcomm. If Setupcomm is not invoked, the system assigns the default send buffer and receive buffer to the serial port. However, to ensure that the size of the buffer is consistent with the actual needs, it is best to call the function to set it. The Setupcomm function prototype is as follows:
Copy Code code as follows:

BOOL Setupcomm (
HANDLE hfile
DWORD Dwinqueue
DWORD Dwoutqueue
);

Where hfile is returned by the CreateFile function to a handle to the open serial port. The parameters dwinqueue and Dwoutqueue Specify the size of the receive buffer and send buffer that the application recommends to use respectively.
emptying the receive and send buffers
It is best to use the PurgeComm function to make the data in the serial port send buffer and receive buffer clear and clean before you perform all the send and receive data operations on the serial port. The PurgeComm function prototype is as follows:
Copy Code code as follows:

BOOL PurgeComm (
HANDLE hfile
DWORD dwflages
);

The parameter hfile is returned by the CreateFile function to a handle to the open serial port, and the parameter dwflags indicates the action performed. If dwflags is Purge_txclear, notifies the system to empty the send buffer, and if dwflags is Purge_rxclear, notifies the system to empty the receive buffer and, if necessary, clears the send and receive buffers. You can set the dwflags to purge_txclear| Purge_rxclear. If the PurgeComm function call succeeds, the return value is not 0; If the function call fails, the return value is 0.
(3) serial data sending and receiving
The same as normal file operations, in the operation of the serial port, the ReadFile function is usually used to read the data received by the serial port, using the WriteFile will be sent to write data such as serial port.
Receipt of serial data
The ReadFile function allows you to read the data received by the serial port. The ReadFile function prototype is as follows:
Copy Code code as follows:

BOOL ReadFile (
HANDLE hfile
Lpviod lpbuffer
DWORD nNumberOfBytesToRead
Lpdword Lpnumberofbytesread
lpoverlapped lpoverlapped
);

Where the parameter hfile points to a serial port handle that is already open; lpbuffer points to a read data buffer; nNumberOfBytesToRead Specifies the number of bytes to read from the serial device ; Lpnumberofbytesread indicates the number of bytes actually read from the serial port; lpoverlapped points to a overlapped struct variable that contains a synchronization event.

Typically, if the call succeeds, ReadFile returns a value other than 0, otherwise the return value is 0. However, the return value of 0 does not necessarily indicate that the function call failed for a serial port that received the operation in the background. You can call the GetLastError function to get further information at this time. If the GetLastError return value is error_io_pending, the operation of the read serial port is still in the background waiting state, rather than a real error.
Serial Data Delivery
Use the WriteFile function to write data to a serial port. The WriteFile function prototype is as follows:
Copy Code code as follows:

BOOL WriteFile (
HANDLE hfile
Lpviod lpbuffer
DWORD Nnumberofbytestowrite
Lpdword Lpnumberofbyteswritten
lpoverlapped lpoverlapped
);

Where the parameter hfile points to a serial port handle that is already open; lpbuffer points to a send data buffer; nNumberOfBytesToRead Specifies the number of bytes to send from the serial device ; Lpnumberofbytesread indicates the number of bytes actually sent from the serial port; lpoverlapped points to a overlapped struct variable that contains a synchronization event.

Typically, if the call succeeds, WriteFile returns a value other than 0, otherwise the return value is 0. However, the return value of 0 does not necessarily indicate that the function call failed for the serial port in the background where the send operation was performed. You can call the GetLastError function to get further information at this time. If the GetLastError return value is error_io_pending, the operation of the write serial port is still in the background waiting state, rather than a real error.
(4) Close serial port
It is usually turned off after the serial port is run out. If you forget to close, the serial port is always open, and other applications cannot open or use it.
Close the serial port can use function CloseHandle, its function prototype is as follows:
Copy Code code as follows:

BOOL CloseHandle (
HANDLE Hobject
);

The CloseHandle function is very simple, where hobject is the handle to the open serial port. If the function call succeeds, the return value is not 0; otherwise, the return value is 0.
Related Article

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.