Serial Port Input Buffer Refills Even After Device is Disconnected
Show older comments
Hi,
I can successfully read data from my serial port device (I'm reading the mass from an electronic balance), but the input buffer doesn't refresh with the newest data, there seems to be a backlog of data that is being sent to the input buffer.
Each mass reading is 14 bytes, so I set the inputbuffersize to 14.
After I read data with fscanf (I read in 14 bytes), the input buffer empties, and then I was thinking that the buffer should refill with the next balance reading (the balance is continuously transmitting data at 300 baud). Well the input buffer refills, but it refills with "old" data. It seems like there is a second buffer that is holding all of the old data, and then the data is transferred from there to input buffer when the input buffer is empty. In fact I can disconnect the balance and still continue to read in data from the balance using fscanf.
I have set the serial port settings to match the balance settings.
The balance does not support handshaking/flow control.
Any help would be appreciated, Thanks! Evan
1 Comment
Fergus
on 13 Mar 2012
I'm having a similar issue. I'm receiving packets of sensor data (108 bytes per 'packet' of sensor reading) from a micro-controller at around 28,000 bps and i want to process only the most recent data.
Unfortunately the input buffer doesn't seem to be overwriting itself with the most recent data. In fact there seems to be at least several kilobytes of old data stored in a buffer somewhere..
Similar to Evan I tried setting the matlab input buffer size to low values (128 in my case) without any luck. I also tried emptying the matlab serial buffer and letting it refill before taking a measurement ( http://www.mathworks.com.au/support/solutions/en/data/1-30N8YX/index.html?product=ML&solution=1-30N8YX ) also with no luck.
It figured it could be windows doing the buffering rather than Matlab and had a look through the Com Port's setting in device manager, there i disabled the FIFO buffer, but this had no noticeable effect either..
Also, i'm using a virtual com port over USB if that helps.
Has anyone else encountered this issue or have other ideas on how to remedy it?
Answers (1)
Fergus
on 13 Mar 2012
0 votes
5 minutes after writing the above comment i had an idea that seems to work.
I used the BytesAvailableFcn (triggers a function when BytesAvailable gets to a certail size) to dump data from the buffer as it comes in. The following is roughly the code i used (its on another computer so no copypasta):
Serial_Port_Object.InputBufferSize = 256; Serial_Port_Object.BytesAvailableFcnCount = 255; Serial_Port_Object.BytesAvailableFcnMode = 'byte'; Serial_Port_Object.BytesAvailableFcn = {'Empty_Buffer'};
and then made another function in the current folder:
function Empty_Buffer(obj, event) discarded_bytes = fread(obj, 128, 'uint8'); %not sure if this is the most efficient way to do %this - but it seems to work. end
its a bit of an ugly workaround though, so a real fix would still be appreciated!
Cheers, Fergus
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!