After reading the overlapped Io model in the previous article, let's look at the remaining two important structures:
2. commtimeouts structure timeout settings
Commtimeouts: commtimeouts is mainly used for setting serial port timeout parameters. The commtimeouts structure is as follows:
Typedef struct _ commtimeouts {
DWORD readintervaltimeout; // read interval timeout
DWORD readtotaltimeoutmultiplier; // read time coefficient
DWORD readtotaltimeoutconstant; // read Time Constant
DWORD writetotaltimeoutmultiplier; // write time coefficient
DWORD writetotaltimeoutconstant; // write time constant
} Commtimeouts, * lpcommtimeouts;
Readintervaltimeout: The maximum latency between two characters. When reading data from the serial port, the READ function returns the existing data once the time difference between the two characters exceeds this time. If it is set to 0, this parameter does not work.
Readtotaltimeoutmultiplier: timeout for reading each character.
Readtotaltimeoutconstant: a fixed timeout for reading serial data at a time. Therefore, in a single serial port reading operation, the timeout value is readtotaltimeoutmultiplier multiplied by the number of bytes read, and readtotaltimeoutconstant. Set readintervaltimeout to maxdword, and set readtotaltimeoutmultiplier and readtotaltimeoutconstant to 0, indicating that the read operation will immediately return the characters stored in the input buffer.
Writetotaltimeoutmultiplier: the time-out between each character written.
Writetotaltimeoutconstant: a fixed timeout for writing data to the serial port. Therefore, in a serial port write operation, the timeout value is writetotaltimeoutmultiplier multiplied by the number of written bytes plus writetotaltimeoutconstant.
Generally, the following settings are made:
Timeouts. readintervaltimeout = maxdword;
// Set the interval timeout value to the maximum value. Setting the total timeout value to 0 causes the readfile to return immediately and complete the operation.
Timeouts. readtotaltimeoutmultiplier = 0;
// Read time coefficient
Timeouts. readtotaltimeoutconstant = 0;
// Read Time Constant
Timeouts. writetotaltimeoutmultiplier = 50;
// Total timeout = time coefficient * number of characters required for read/write + Time Constant
Timeouts. writetotaltimeoutconstant = 2000;
// Set the write timeout to specify
3. DCB Structure
DCBDevice Control Block
After opening the communication serial port, we need to initialize the serial port, such as the baud rate, parity bit, and parity bit. When querying or configuring the data, we must use DCB for buffering, you can call the getcommstate function to obtain the current serial port configuration. The following are the specific members of DCB:
Typedef struct _ DCB {// DCB
DWORD dcblength; // sizeof (DCB)
DWORD baudrate; // current baud rate
Specify the current baud rate
DWORD fbinary: 1; // binary mode, no EOF check
Specify whether the binary mode is allowed,
Windows 95 must be true
DWORD fparity: 1; // enable parity checking
Whether the parity check is allowed
DWORD foutxctsflow: 1; // CTS output flow control
Specifies whether CTS is used to detect sending control.
If it is true, the sending will be suspended if CTS is off.
DWORD foutxdsrflow: 1; // DSR output flow control
Specifies whether CTS is used to detect sending control.
If it is true, the sending will be suspended if CTS is off.
DWORD fdtrcontrol: 2; // DTR Flow Control Type
The dtr_control_disable Value Sets DTR to off, and the dtr_control_enable Value Sets DTR to on. dtr_control_handshake allows DTR to "Shake hands", DWORD fdsrsensiti.pdf: 1; // when the value of DSR sensiti.pdf is true, the bytes received when the value of DSR is off are ignored.
DWORD ftxcontinueonxoff: 1; // xoff continues TX
Specifies that when the receiving buffer is full and the driver has been sent
Whether to stop sending when xoffchar characters are sent.
True: when the receiving buffer receives the byte xofflim that is full in the buffer and the driver has sent the xoffchar character to abort the receiving byte, the sending continues.
If this parameter is set to false, the receiving buffer receives the byte xonchar that indicates that the buffer is empty and the driver sends the xonchar to resume sending.
DWORD foutx: 1; // Xon/xoff Out Flow Control
If the parameter is set to true, the system stops sending messages after xoffchar is received.
After receiving xonchar, it will start again
DWORD finx: 1; // Xon/xoff in Flow Control
When the value is true, after the receiving buffer receives xofflim, which indicates that the buffer is full, xoffchar is sent out.
After the receiving buffer receives the xonlim, which indicates that the buffer is empty, the xonchar is sent out.
DWORD ferrorchar: 1; // enable error replacement
If this value is true and fparity is true, use the characters specified by the errorchar member to replace the characters receiving the parity error.
DWORD fnull: 1; // enable null Stripping
When the value is true, the null (0 value) bytes are removed during receiving.
DWORD frtscontrol: 2; // RTS Flow Control
Rts_control_disable
Rts_control_enable
Rts_control_handshake,
When the receiving buffer is less than half full, the RTS is on.
When the receiving buffer exceeds 3/4 full, the RTS is off.
When rts_control_toggle,
When the receiving buffer still has the remaining bytes, the RTS is on; otherwise, the default value is off.
DWORD fabortonerror: 1; // abort reads/writes on Error
True: Stop read and write operations when an error occurs.
DWORD fdummy2: 17; // Reserved
Unused
Word wreserved; // not currently used
Not used, must be 0
Word xonlim; // transmit Xon threshold
Specifies the minimum number of bytes allowed in the buffer before the Xon character is sent.
Word xofflim; // transmit xoff threshold
Specifies the minimum number of bytes allowed in the receiving buffer before xoff is sent.
Byte bytesize; // number of bits/byte, 4-8
The data bit currently used by the specified port
Byte parity; // 0-4 = No, odd, even, Mark, space
The current parity method used by the specified port, which may be:
Evenparity, markparity, noparity, oddparity
Byte stopbits; // 0, 1, 2 = 1, 1.5, 2
The number of currently used Stop bits on the specified port, which may be:
Onestopbit, one5stopbits, twostopbits
Char xonchar; // Tx and Rx Xon character
Specifies the value of Xon used to send and receive characters.
Char xoffchar; // Tx and Rx xoff character
Specify the xoff value for sending and receiving characters
Char errorchar; // error replacement character
This character is used to replace the value of the received parity when an error occurs.
Char eofchar; // end of input character
When the binary mode is not used, this character can be used to indicate the end of the data.
Char evtchar; // encoded ed event character
When this character is received, an event is generated.
Word wreserved1; // reserved; do not use not used
} DCB;