Win32 ReadFile timeout takes 25ms more then expected

  c++, serial-port, windows

What I am trying to do:

I am writting a C# app calling ReadFile from a dll. I am reading from a serial COM port;

I would like to set the timeout the ReadFile function to a fixed value of Nms.

The problem:

When I set the constant timeout to a fixed Nms, it seems to take about N+25ms.

timeout=1ms -> actual time=31ms  
timeout=10ms -> actual time=31ms  
timeout=20ms -> actual time=46ms  
timeout=100ms -> actual time=125ms 


Code for configuring the timeouts:

config_timeouts.ReadIntervalTimeout = 0;
config_timeouts.ReadTotalTimeoutConstant = value;
config_timeouts.ReadTotalTimeoutMultiplier = 0;

How I am measuring the time:

public int read(byte[] data, int offset, int len)
    lock (lock_port) 
        var watch_read = new Stopwatch();
        var result = port_read(handle, data, (uint)offset, (uint)len);
        Console.WriteLine($"read in={watch_read.ElapsedMilliseconds}");
        return result;

Unmanaged code I am calling:

int port_read(HANDLE port, uint8_t *data, unsigned int offset, unsigned int len)
    DWORD total = 0;
    DWORD read = 0;
    BOOL status;

    // read byte
    status = ReadFile(port, data + read + offset, len - read, &read, NULL);

    if (FALSE == status) {
        return 0;

    return read;

The call stack should be:
read > port_read > ReadFile
(a C# function calling a C++ function calling ReadFile)

I am also calling WriteFile from C# from a different thread, but I use a lock() {} for both C# functions calling ReadFile, WriteFile.
But I write virtually nothing, and the write timeout does not affect the read timeout.

The ReadFile call will be shorter if all bytes are read.

How can I make the ReadFile call timeout after Nms (the set constant timeout)? In other words, where is the added 25ms comming from and how can I remove this additional delay?

Source: Windows Questions