How to set timeout parameters (Serial Communication)

Source: Internet
Author: User

If you want to read/write reliability, set the appropriate value based on different rates and actual conditions of the application. It is irrelevant to the synchronous mode, asynchronous mode, and different communication protocols.

 

Commtimeouts: commtimeouts is mainly used for setting serial port timeout parameters. The commtimeouts structure is as follows:

Typedef struct _ commtimeouts {
DWORD readintervaltimeout;
DWORD readtotaltimeoutmultiplier;
DWORD readtotaltimeoutconstant;
DWORD writetotaltimeoutmultiplier;
DWORD writetotaltimeoutconstant;
} 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. The maximum time (in milliseconds) is specified, and there is a time difference between the two bytes allowed to be received. That is to say, after receiving a byte, no new bytes have arrived after the readintervaltimeout time, it is deemed that this serial read operation is complete (the subsequent bytes are processed by the next read operation ). Even if you want to read 8 bytes, but after reading 2nd bytes, after the readintervaltimeout time, 3rd bytes have not arrived yet. In fact, it is read-only in 2 bytes.


Readtotaltimeoutmultiplier: Specifies the proportional factor (MS). In fact, it is used to set the time required to read a byte and wait for the next byte. In this way, the total timeout value is the number of bytes read multiplied by this value, after the same read operation reaches this time, it is deemed that this read operation has ended.

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. It can be understood as a correction time. In fact, the timeout value calculated by readtotaltimeoutmultiplier and the time is used as the entire timeout value.

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

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.