User Tools

Site Tools


changing_20the_20serial_20buffer_20size

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

changing_20the_20serial_20buffer_20size [2018/03/31 13:19]
127.0.0.1 external edit
changing_20the_20serial_20buffer_20size [2018/04/17 15:17] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //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 [[http://​msdn.microsoft.com/​en-gb/​library/​windows/​desktop/​aa363439.aspx|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:​\\ ​ //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 [[http://​msdn.microsoft.com/​en-gb/​library/​windows/​desktop/​aa363439.aspx|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:​\\ ​
 +<code bb4w>
         chan% = OPENUP("​COM1:​115200,​n,​8,​1"​)         chan% = OPENUP("​COM1:​115200,​n,​8,​1"​)
         IF chan%=0 ERROR 100, "​Failed to open serial port"         IF chan%=0 ERROR 100, "​Failed to open serial port"
  
         SYS "​SetupComm",​ @hfile%(chan%),​ rxqueue%, 0         SYS "​SetupComm",​ @hfile%(chan%),​ rxqueue%, 0
 +</​code>​
 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:​\\ ​ 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:​\\ ​
 +<code bb4w>
         DIM commprop{wPacketLength{l&,​h&​},​ wPacketVersion{l&,​h&​},​ dwServiceMask%,​ \         DIM commprop{wPacketLength{l&,​h&​},​ wPacketVersion{l&,​h&​},​ dwServiceMask%,​ \
         \            dwReserved1%,​ dwMaxTxQueue%,​ dwMaxRxQueue%,​ dwMaxBaud%, \         \            dwReserved1%,​ dwMaxTxQueue%,​ dwMaxRxQueue%,​ dwMaxBaud%, \
Line 15: Line 18:
  
         SYS "​GetCommProperties",​ @hfile%(chan%),​ commprop{}         SYS "​GetCommProperties",​ @hfile%(chan%),​ commprop{}
 +</​code>​
 The buffer size is returned in the **commprop.dwCurrentRxQueue%** member. The buffer size is returned in the **commprop.dwCurrentRxQueue%** member.
changing_20the_20serial_20buffer_20size.txt ยท Last modified: 2018/04/17 15:17 by tbest3112