0
votes

I'm using the Win32 API to read data from a serial port:

DWORD numRead = 0;
ReadFile(mPortHandle, mReceiveBuffer.get(), mReceiveBufferSize, &numRead, nullptr);

This call takes extremely long: 129ms to be accurate (measured via QueryPerformanceCounter).

I did the measurement with several different hardware devices:

  • USB adapter with FTDI chipset
  • USB adapter with prolific chipset
  • PCI express RS232 card
  • Mainboard's onboard serial port

Evvery of these devices gives me exactly the same delay: 129ms. Thus I don't think its the hardware or the driver's fault (each device should be using a totally different driver, right?).

I also fiddled around with timeouts, but that did not change anything.

What else could it be?

2
Reading from serial ports is slow; that's not so surprising. What baud rate are you using?Cody Gray
Currently I'm using 115200 Bd. But there is actually no data comming in, so ReadFile returns 0 Bytes. Still it takes ~130ms for doing nothing.Boris
@Boris - the NtReadFile really return STATUS_TIMEOUT i guess if you got 0 bytes / you need set COMMTIMEOUTS firstRbMm

2 Answers

0
votes

Perhaps it is because you always readfile by specifying the total size of the receive buffer.

Is the receive buffer size large?

If you register an event handler for the DataReceived event and only read the data that arrives in the buffer of the device driver, the extra waiting time will be reduced.

0
votes

OK, problem solved :-)

Until now I set the timeouts like this:

COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;

This does not work. If I only set the constant, it works though:

COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;

Now ReadFile returns within 1ms.