BBC BASIC for Windows
Programming >> Communication and Input/Output >> Serial port buffer http://bb4w.conforums.com/index.cgi?board=communication&action=display&num=1355245811 Serial port buffer
Post by manxman on Dec 11th, 2012, 12:30pm
Is it possible to increase the size of the Windows serial port buffer? I have a program that receives and plots data arriving at 20Hz from the port. The reason I ask is that my program sometimes leaves the graphics task to carry out another time-consuming operation that might result in the buffer overflowing. Since there is no handshaking with the external instrument, this can result in data being lost. Extending the buffer would solve this problem.
Regards,
Manxman Re: Serial port buffer
Post by admin on Dec 11th, 2012, 4:09pm
my program sometimes leaves the graphics task to carry out another time-consuming operation that might result in the buffer overflowing.
Have you actually observed such an overflow, or is it a theoretical concern? The default buffer size is quite large (4 Kbytes I think) so you would have to leave the input 'unserviced' for quite a long time for it to overflow.
If it really does overflow, the solution is surely to service the serial port more frequently (perhaps in a timer interrupt) rather than to try to increase the buffer size.
Richard. Re: Serial port buffer
Post by manxman on Dec 12th, 2012, 09:15am
Yes, I do sometimes observe an overflow when the program is made to depart for several minutes from servicing the serial buffer. I would have to restructure my program so that serial data processing (and associated graphics and filing) is put inside the other task - something I am not keen to do. For me the ideal thing would be to enlarge the buffer, if this is possible. Any hints are most welcome!
Regards,
Manxman
Re: Serial port buffer
Post by admin on Dec 12th, 2012, 2:54pm
Yes, I do sometimes observe an overflow when the program is made to depart for several minutes from servicing the serial buffer.
Several minutes?! You really need to consider restructuring your code - why can't you carry out the serial-port servicing in a timer interrupt? That ought to allow you to avoid overflows without making any changes to the time-consuming 'graphics and filing' code.
Quote:
For me the ideal thing would be to enlarge the buffer, if this is possible.
The trouble is, you can't guarantee to be able to increase the buffer size. All Windows allows you to specify is a recommended buffer size: the OS may, or it may not, increase its size to the value you have requested.
The relevant phrase in MSDN is "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)". Not servicing the serial port for several minutes is without doubt an 'extreme circumstance', so all bets are off:
If your program only ever needs to run on a specific machine or machines, and you can establish that on those machines - with their particular software and hardware configurations - you can increase the buffer size enough to solve your problem, then go ahead and do it that way. But I would urge you to consider making your program less dependent on such factors.
Richard. Re: Serial port buffer
Post by manxman on Dec 12th, 2012, 8:31pm
Hmmm.
My program will only ever be running on a specific machine which is acting mainly as a data logger. I had never considered the use of a timer interrupt (and have no experience of such). From the link you give I could presumably increase the buffer size by including the statement:
_In_ A% dwInQueue
in my code, where A% is the desired size of the input buffer.
Regards,
Manxman
Re: Serial port buffer
Post by admin on Dec 12th, 2012, 9:34pm
From the link you give I could presumably increase the buffer size
Yes, the SetupComm API function is the way to try to increase it. The MSDN description doesn't make clear whether a 'success' return from the function (i.e. a non-zero value) means that the requested buffer size was accepted, but if in doubt you can always call the GetCommProperties API which returns the current buffer size in the dwCurrentRxQueue member. If I try to double the buffer size from the default 4096 bytes to 8192 bytes, here it succeeds:
Code:
SYS "SetupComm", @hfile%(channel%), 8192, 0
Richard.
Re: Serial port buffer
Post by manxman on Dec 13th, 2012, 4:43pm
Wunderbar.
I will include that call in my program and hopefully that will provide my solution.