BytesAvailableFcn is getting called too often
Show older comments
I have a serial communication port using TCP/IP, using the tcpip routine from the instrument control toolbox. Now, I receive data via network and plot it, everything's fine. But once the remote side closes the connection and I do not receive further data, I still get tons of BytesAvailable-callbacks, tough BytesAvailable equals 0.
Before calling fread, I check the obj.BytesAvailable. If it is zero, I skip the whole routine. Nevertheless, since these are so many callbacks, matlab needs a long time to process each callback.
I figured out that this is due to the fact that I read out the entire buffer using:
results = fread(acqData, acqData.BytesAvailable/2, 'int16');
while the BytesAvailableFcnCount and InputBufferSize is set as such:
acqData = tcpip('XXX.XXX.XXX.XXX', YYYY,'NetworkRole','client','InputBufferSize',2^16);
acqData.BytesAvailableFcnMode = 'byte';
acqData.BytesAvailableFcnCount = 2^5;
I understand that if the remote side fills the buffer very fast, I will get effectively 2^10 empty callbacks per actual callback. I do not want to set the BytesAvailableFcnCount to 2^16. Also, I would not like fread to only read 2^5 bytes each time it gets called (the callback function is rather slow, so it is best to process as much data at once as possible).
Cann I change the behaviour of the BytesAvailableFcn to trigger only once? E.g. when 2^5 bytes have arrived it triggers, but then all further callbacks until the buffer is full are discarded?
Thanks!
1 Comment
Tom DeLonge
on 28 Feb 2019
Answers (0)
Categories
Find more on TCP/IP Communication 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!