User Tools

Site Tools


Changing the serial buffer size

by Richard Russell, December 2012

When you open a serial port for input, a buffer (queue) is automatically created to hold incoming data until it is read by your program. This buffer is typically 4096 bytes in size (although that may vary according to the version of Windows, serial port driver etc.).

A 4096-byte buffer will be sufficient in almost all situations. At the highest standard baud rate (115200 baud) it corresponds to more than 1/3 of a second of storage, so as long as your program services the serial port at least three times a second no data should be lost. Typically a program will check the serial port much more often than that - if it doesn't you should probably consider revising the structure of your program.

However in exceptional circumstances it may be useful to be able to increase the size of the serial input buffer. Windows provides the means to request an increased buffer size, but there is no guarantee that the request will be granted. This is what MSDN says: “The device driver receives the recommended buffer sizes, but is free to use any input and output (I/O) buffering scheme, as long as it provides reasonable performance and data is not lost due to overrun (except under extreme circumstances)”.

With that caveat, this is how you can request a new buffer size:

        chan% = OPENUP("COM1:115200,n,8,1")
        IF chan%=0 ERROR 100, "Failed to open serial port"
        SYS "SetupComm", @hfile%(chan%), rxqueue%, 0

Here rxqueue% is the requested buffer size in bytes; the serial port parameters shown are of course only an example. It is important to call SetupComm immediately after the serial port has been opened, and before any data transfer takes place.

If you need to know whether the request to change the buffer size has been successful, you can discover the current size using the GetCommProperties API:

        DIM commprop{wPacketLength{l&,h&}, wPacketVersion{l&,h&}, dwServiceMask%, \
        \            dwReserved1%, dwMaxTxQueue%, dwMaxRxQueue%, dwMaxBaud%, \
        \            dwProvSubType%, dwProvCapabilities%, dwSettableParams%, \
        \            dwSettableBaud%, wSettableData{l&,h&}, wSettableStopParity{l&,h&}, \
        \            dwCurrentTxQueue%, dwCurrentRxQueue%, dwProvSpec1%, dwProvSpec2%, \
        \            wcProvChar{l&(1),h&}}
        SYS "GetCommProperties", @hfile%(chan%), commprop{}

The buffer size is returned in the commprop.dwCurrentRxQueue% member.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
changing_20the_20serial_20buffer_20size.txt · Last modified: 2018/04/17 16:17 by tbest3112