jcurl / RJCP.DLL.SerialPortStream

SerialPortStream is an independent implementation of System.IO.Ports.SerialPort and SerialStream for better reliability and maintainability. Default branch is 2.x and now has support for Mono with help of a C library.
Microsoft Public License
624 stars 197 forks source link

Make COMMTIMEOUTS fields publicly settable #74

Closed dybs closed 2 years ago

dybs commented 5 years ago

I'm trying to improve my download speed from an FTDI FT232H (Hi-speed USB) running at 4 Mbaud using the FTDI VCP driver v2.12.28.0 (Windows 7 x64). Each data packet (about 2900 bytes) has a total ~40 ms turnaround time on a PIC24 (process command, read SD card, prepare response, send out UART to FT232H, wait for next request from PC). There's about a 10 ms delay from when the last USB data frame is sent to when the PC sends its next command.

I tracked down this delay to the line 222 in CommOverlappedIo.cs:

// Time out if data hasn't arrived in 10ms, or if the read takes longer than 100ms in total
timeouts.ReadIntervalTimeout = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 0;

Is there a particular reason a 10 ms timeout was chosen? I was able to reduce this timeout to 1 ms which improved my download time by about 25%. It would be nice if this timeout could be configured from my application rather than modifying the library source.

As a side note, I tried disabling the PL2303_WORKAROUNDS to use "non-asychronous behavior" but then I started getting CE_OVERRUN errors after the first 10 or so data packets. The only recommended solutions I've found are to reduce my baud rate, or read the data out of the serial port buffer faster.

jcurl commented 5 years ago

The main reason to use 10ms is to prevent constant CPU usage of 100%. It's small, but yet sufficient timeout for 99% of applications.

jcurl commented 2 years ago

Implemented now in master with 76253971d133791332e2f3b6df5d2fbee83dcd87.