The Win32 API configured in commconfig.

Source: Internet
Author: User
Tags readfile

Understanding the usage of the serial communication API functions is the key to mastering the serial communication programming technology. In Win32, the system unifies the serial port and file, and uses the same API functions for their open, read, write, and close operations, but there are differences between them, these differences are mainly reflected in the setting of some parameters in the API function. APIS for communication mainly include opening the serial port, closing the serial port, configuring the serial port, setting the buffer, setting the timeout, event driver, reading the serial port, and writing the serial port. The following describes and analyzes how to use serial port operation functions based on the specific situation of GPS. I. Opening and closing the serial port 1: Opening the serial port. In Windows, the serial port is used as a file.
Createfile (). The function prototype is handle createfile (lpctstr lpfilename, DWORD dwdesiredaccess, DWORD dw1_mode, lpsecurity_attributes lpsecurityattributes, DWORD dwcreationdisposition, DWORD detail, handle htemplatefile). The meanings of several parameters are: lpfilename specifies the file name or device name. During serial communication, it must be "comx", where "X" indicates the serial number, such as the first serial port.
"COM1"; "dwdesiredaccess" indicates the serial port read/write attribute; "dw1_mode" indicates the Port Sharing attribute. The serial port cannot be used as a shared device, so the parameter value must be 0; and "lpsecurityattributes" indicates the Security attribute, the serial port should be 0; dwcreationdisposition indicates the file creation mode. The serial port must be open-existing; dwflagsandattributes describes other attributes of the file creation. For the serial port, valid settings can only be file-flag-overlapped or 0, indicating asynchronous or synchronous read/write, respectively. The htemplatefile parameter must be null.
Return Value: If successful, the created handle is returned; otherwise, the invalid-handle-value is returned. 2: Close the serial port. It is relatively easy to close the serial port. You can close the handle bool closehandle (handle hobject) function. hobject is the handle obtained when the serial port is opened. After the handle is closed, the serial port resources are released, which can be used by other programs. 2. After the serial port is configured to open the serial port, the previous configuration will be maintained. To make the sum of peripherals communicate, you must configure the serial port so that the sum of peripherals maintains the same serial port configuration, in this way, you can effectively configure peripherals and receive data. In Win32, the serial port configuration is mainly implemented through a structure called commconfig, which contains a DCB structure. In DCB, there are baud rate, data bits, Stop bits, check bits, and other strings.
For the communication parameters of the row interface, the configuration of the serial port is mainly the configuration of these parameters. The Win32 API for configuring commconfig mainly includes the following: 1: Get the default configuration function. The prototype is bool getdefacommcommconfig (lpcstr lpszname, lpcommconfig lpcc, lpdword lpdwsize). The parameters are as follows: lpszname indicates the device name, for example, "COM1"; and lpcc indicates the pointer of the commconfig struct. Lpdwsize is the size of the lpcc. You can use this function to obtain the default configuration of a serial port. This function is related to the hardware of the machine and has different results on different machines.
This function is prone to errors. Therefore, we recommend that you rewrite this function and assign values to commconfig members to ensure stability and reliability. 2: Get the current configuration of the serial port. The prototype is as follows: bool getcommconfig, lpcommconfig lpcc, DWORD dwsize); this function is configured by the serial port specified by hcommdev according to the content in the lpcc struct. The main Member of the lpcommconfig struct is Device Control.
Block Structure DCB, which specifies the device's baud rate, parity bit, traffic control, stop bit, and other parameters. 4: configure the serial port dialog box bool commconfigdialog (lpcstr lpszname, hwnd, lpcommconfig lpcc) This function provides a user-familiar serial port configuration dialog box with the handle being hwnd. In this dialog box, you can configure the serial port indicated by lpszname. The configuration result is stored in the commconfig structure pointed to by the lpcc. 5: Setting the buffer for reading and writing serial ports in Win32 environment is not a traditional method for serial UART, but a buffer zone.
Buffer. It is important to determine the buffer size, which is too resource-consuming and too small, leading to the loss of valid data. The user must calculate the peripheral traffic size before reading and writing the serial port, determine the appropriate buffer size according to the principle of greater than small and redundant, and then use the setupcomm () function to set the serial port. The prototype is as follows: bool setupcomm (handle hfile, DWORD dwinqueue, DWORD dwoutqueue); hfile is the handle obtained when the serial port is opened; dwinqueue is the size of the input buffer, in bytes; dwoutqueue is the size of the output buffer. 6. Set unpredictable events that may occur during data communication timeout, such as damage to the data transmission cable, unexpected interruption of peripheral failure, or sudden Data Transmission
Stop. A key issue for communication applications is how to handle unpredictable events in communication. The ability to handle error events is an important indicator of the merits and demerits of communication applications. Otherwise, I/O threads may be suspended or threads may be blocked infinitely. Windows provides security measures for such problems. You can set timeout to determine whether the communication is abnormal and handle the problem accordingly. Therefore, timeout settings are particularly important in serial communication. The setcommtimeouts () function is provided in Win32 to set the serial port timeout value. Bool setcommtimeouts (handle hfile, lpcommtimeouts );
Hfile is the handle obtained when the serial port is opened; lpcommtimeouts is the pointer to the timeout struct. 3. After opening the serial port and correctly configuring it, you can use it for communication to read and write data. Reading data is to receive input, and some data is to send data to peripherals. Read and Write Data through two API functions. 1: Read data functions: bool readfile (handle hfile, lpvoid lpbuffer, DWORD nnumberofbytestoread, lpdword lpnumberofbytesread, lpoverlapped
Data); hfile is the handle returned by createfile (); lpbuffer is the data buffer pointer read; nnumberofbytestoread is the number of bytes to read; lpnumberofbytesread is the first address of the number of bytes actually read; lpoverlapped is a data structure pointer pointing to an overlapped I/O (asynchronous. If lpoverlapped is set to null, readfile () works in synchronous mode. If lpoverlapped points to an overlapping structure, it works in asynchronous mode. Whether synchronous or asynchronous, The readfile () function will return immediately after execution.
2: Write the serial port function bool writefile (handle hfile, lpvoid lpbuffer, DWORD nnumberofbytestoread, lpdword lpnumberofbytesread, lpoverlapped data). Its Parameter meaning is similar to that. 4. There are multiple operation modes during serial port read/write operations. From the perspective of Synchronous Control, there are synchronous and asynchronous modes, and from the programming perspective, there are query and event-driven modes. During synchronous reading of the serial port, if the content in the serial port buffer is not read, the system will be waiting until the reading is completed.
If it is asynchronous, the system will go down and execute the next code even if the buffer is not read. If the serial port is frequently interacted, asynchronous read/write is recommended. However, for general data collection, data is mainly read. There are not many write commands, and the data size read at a time is not too large, therefore, synchronization is better and easier to control. The query method is to directly read and write serial data in an infinite loop. For reading data from the serial port, the query is the most direct and simple method, but this method is inefficient, it takes a lot of CPU time. The event-driven Mode means that a thread performs read and write operations by monitoring a group of events in the communication resources. This mode is similar to the DOS interrupt mode, with high efficiency. The monitoring event is implemented through the setcommmask () and waitcommevent () functions. In read or write operations, you can use the setcommmask () function to set events.
Block to monitor events on a specified communication resource. after a group of events is specified, the thread can use the waitcommevent () function to wait for an event to be generated. The thread will be suspended while waiting, in this way, it takes a very short CPU time. Once a corresponding event occurs, waitcommevent () will return and tell the System of the event. You can determine the corresponding processing method based on the event content. When waiting for an event, the waitcommevent () function will exclusively occupy the serial port. At this time, the serial port cannot be read or written, and the read/write operation must wait until the waitcommevent () function returns. Set the synchronous control mode when the serial port is enabled, which is specified by the third parameter. The following functions adopt the event-driven mode during programming.
1: Set the event mask bool setcommmask (handle hfile, DWORD dwevtmask). hfile is the file handle for opening the serial port. Dwevtmask is the set mask. Its form and meaning are as follows: the mask event ev_break detects the breakpoint ev_cts (clear-to-send) in the input) when the signal changes to the status ev_dsr (data-set-ready), when the signal changes to the status ev_err, the line status error occurs. Ev_ring indicates ev_rlsd RlSD (receive-line-signal-detect)
When the signal changes to the status ev_rxchar, one character arrives and is stored in the receiving buffer ev_rxflag, and one event character arrives and is stored in the receiving buffer. The event character is included in the DCB of the device, the last character applied by the setcommstate function to the serial port ev_txempty sending buffer is sent 2: waiting for the arrival of the event bool setcommmask (handle hfile, DWORD dwevtmask, lpoverlapped ); its Parameter meaning is similar to that of the Set mask. This function waits for the arrival of events set by the setcommmask () function. When this function is used, the serial port is blocked.
Dedicated serial port in Plug mode. If the defined event arrives, we can take corresponding measures to deal with it. During data collection, the usual event is: ev_rxchar. When the event arrives, you can read the data in the receiving buffer and hand it over to the background thread for processing. If more than one serial port is used, the Win32 Multi-task feature must be used.

Understanding the usage of the serial communication API functions is the key to mastering the serial communication programming technology. In Win32, the system unifies the serial port and file, and uses the same API functions for their open, read, write, and close operations, but there are differences between them, these differences are mainly reflected in the setting of some parameters in the API function. APIS for communication mainly include opening the serial port, closing the serial port, configuring the serial port, setting the buffer, setting the timeout, event driver, reading the serial port, and writing the serial port. The following describes and analyzes how to use serial port operation functions based on the specific situation of GPS. I. Opening and closing the serial port 1: Opening the serial port. In Windows, the serial port is used as a file.
Createfile (). The function prototype is handle createfile (lpctstr lpfilename, DWORD dwdesiredaccess, DWORD dw1_mode, lpsecurity_attributes lpsecurityattributes, DWORD dwcreationdisposition, DWORD detail, handle htemplatefile). The meanings of several parameters are: lpfilename specifies the file name or device name. During serial communication, it must be "comx", where "X" indicates the serial number, such as the first serial port.
"COM1"; "dwdesiredaccess" indicates the serial port read/write attribute; "dw1_mode" indicates the Port Sharing attribute. The serial port cannot be used as a shared device, so the parameter value must be 0; and "lpsecurityattributes" indicates the Security attribute, the serial port should be 0; dwcreationdisposition indicates the file creation mode. The serial port must be open-existing; dwflagsandattributes describes other attributes of the file creation. For the serial port, valid settings can only be file-flag-overlapped or 0, indicating asynchronous or synchronous read/write, respectively. The htemplatefile parameter must be null.
Return Value: If successful, the created handle is returned; otherwise, the invalid-handle-value is returned. 2: Close the serial port. It is relatively easy to close the serial port. You can close the handle bool closehandle (handle hobject) function. hobject is the handle obtained when the serial port is opened. After the handle is closed, the serial port resources are released, which can be used by other programs. 2. After the serial port is configured to open the serial port, the previous configuration will be maintained. To make the sum of peripherals communicate, you must configure the serial port so that the sum of peripherals maintains the same serial port configuration, in this way, you can effectively configure peripherals and receive data. In Win32, the serial port configuration is mainly implemented through a structure called commconfig, which contains a DCB structure. In DCB, there are baud rate, data bits, Stop bits, check bits, and other strings.
For the communication parameters of the row interface, the configuration of the serial port is mainly the configuration of these parameters. The Win32 API for configuring commconfig mainly includes the following: 1: Get the default configuration function. The prototype is bool getdefacommcommconfig (lpcstr lpszname, lpcommconfig lpcc, lpdword lpdwsize). The parameters are as follows: lpszname indicates the device name, for example, "COM1"; and lpcc indicates the pointer of the commconfig struct. Lpdwsize is the size of the lpcc. You can use this function to obtain the default configuration of a serial port. This function is related to the hardware of the machine and has different results on different machines.
This function is prone to errors. Therefore, we recommend that you rewrite this function and assign values to commconfig members to ensure stability and reliability. 2: Get the current configuration of the serial port. The prototype is as follows: bool getcommconfig, lpcommconfig lpcc, DWORD dwsize); this function is configured by the serial port specified by hcommdev according to the content in the lpcc struct. The main Member of the lpcommconfig struct is Device Control.
Block Structure DCB, which specifies the device's baud rate, parity bit, traffic control, stop bit, and other parameters. 4: configure the serial port dialog box bool commconfigdialog (lpcstr lpszname, hwnd, lpcommconfig lpcc) This function provides a user-familiar serial port configuration dialog box with the handle being hwnd. In this dialog box, you can configure the serial port indicated by lpszname. The configuration result is stored in the commconfig structure pointed to by the lpcc. 5: Setting the buffer for reading and writing serial ports in Win32 environment is not a traditional method for serial UART, but a buffer zone.
Buffer. It is important to determine the buffer size, which is too resource-consuming and too small, leading to the loss of valid data. The user must calculate the peripheral traffic size before reading and writing the serial port, determine the appropriate buffer size according to the principle of greater than small and redundant, and then use the setupcomm () function to set the serial port. The prototype is as follows: bool setupcomm (handle hfile, DWORD dwinqueue, DWORD dwoutqueue); hfile is the handle obtained when the serial port is opened; dwinqueue is the size of the input buffer, in bytes; dwoutqueue is the size of the output buffer. 6. Set unpredictable events that may occur during data communication timeout, such as damage to the data transmission cable, unexpected interruption of peripheral failure, or sudden Data Transmission
Stop. A key issue for communication applications is how to handle unpredictable events in communication. The ability to handle error events is an important indicator of the merits and demerits of communication applications. Otherwise, I/O threads may be suspended or threads may be blocked infinitely. Windows provides security measures for such problems. You can set timeout to determine whether the communication is abnormal and handle the problem accordingly. Therefore, timeout settings are particularly important in serial communication. The setcommtimeouts () function is provided in Win32 to set the serial port timeout value. Bool setcommtimeouts (handle hfile, lpcommtimeouts );
Hfile is the handle obtained when the serial port is opened; lpcommtimeouts is the pointer to the timeout struct. 3. After opening the serial port and correctly configuring it, you can use it for communication to read and write data. Reading data is to receive input, and some data is to send data to peripherals. Read and Write Data through two API functions. 1: Read data functions: bool readfile (handle hfile, lpvoid lpbuffer, DWORD nnumberofbytestoread, lpdword lpnumberofbytesread, lpoverlapped
Data); hfile is the handle returned by createfile (); lpbuffer is the data buffer pointer read; nnumberofbytestoread is the number of bytes to read; lpnumberofbytesread is the first address of the number of bytes actually read; lpoverlapped is a data structure pointer pointing to an overlapped I/O (asynchronous. If lpoverlapped is set to null, readfile () works in synchronous mode. If lpoverlapped points to an overlapping structure, it works in asynchronous mode. Whether synchronous or asynchronous, The readfile () function will return immediately after execution.
2: Write the serial port function bool writefile (handle hfile, lpvoid lpbuffer, DWORD nnumberofbytestoread, lpdword lpnumberofbytesread, lpoverlapped data). Its Parameter meaning is similar to that. 4. There are multiple operation modes during serial port read/write operations. From the perspective of Synchronous Control, there are synchronous and asynchronous modes, and from the programming perspective, there are query and event-driven modes. During synchronous reading of the serial port, if the content in the serial port buffer is not read, the system will be waiting until the reading is completed.
If it is asynchronous, the system will go down and execute the next code even if the buffer is not read. If the serial port is frequently interacted, asynchronous read/write is recommended. However, for general data collection, data is mainly read. There are not many write commands, and the data size read at a time is not too large, therefore, synchronization is better and easier to control. The query method is to directly read and write serial data in an infinite loop. For reading data from the serial port, the query is the most direct and simple method, but this method is inefficient, it takes a lot of CPU time. The event-driven Mode means that a thread performs read and write operations by monitoring a group of events in the communication resources. This mode is similar to the DOS interrupt mode, with high efficiency. The monitoring event is implemented through the setcommmask () and waitcommevent () functions. In read or write operations, you can use the setcommmask () function to set events.
Block to monitor events on a specified communication resource. after a group of events is specified, the thread can use the waitcommevent () function to wait for an event to be generated. The thread will be suspended while waiting, in this way, it takes a very short CPU time. Once a corresponding event occurs, waitcommevent () will return and tell the System of the event. You can determine the corresponding processing method based on the event content. When waiting for an event, the waitcommevent () function will exclusively occupy the serial port. At this time, the serial port cannot be read or written, and the read/write operation must wait until the waitcommevent () function returns. Set the synchronous control mode when the serial port is enabled, which is specified by the third parameter. The following functions adopt the event-driven mode during programming.
1: Set the event mask bool setcommmask (handle hfile, DWORD dwevtmask). hfile is the file handle for opening the serial port. Dwevtmask is the set mask. Its form and meaning are as follows: the mask event ev_break detects the breakpoint ev_cts (clear-to-send) in the input) when the signal changes to the status ev_dsr (data-set-ready), when the signal changes to the status ev_err, the line status error occurs. Ev_ring indicates ev_rlsd RlSD (receive-line-signal-detect)
When the signal changes to the status ev_rxchar, one character arrives and is stored in the receiving buffer ev_rxflag, and one event character arrives and is stored in the receiving buffer. The event character is included in the DCB of the device, the last character applied by the setcommstate function to the serial port ev_txempty sending buffer is sent 2: waiting for the arrival of the event bool setcommmask (handle hfile, DWORD dwevtmask, lpoverlapped ); its Parameter meaning is similar to that of the Set mask. This function waits for the arrival of events set by the setcommmask () function. When this function is used, the serial port is blocked.
Dedicated serial port in Plug mode. If the defined event arrives, we can take corresponding measures to deal with it. During data collection, the usual event is: ev_rxchar. When the event arrives, you can read the data in the receiving buffer and hand it over to the background thread for processing. If more than one serial port is used, the Win32 Multi-task feature must be used.

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.