BytesAvailableFcn is getting called too often

3 views (last 30 days)
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
Tom DeLonge on 28 Feb 2019
I went a little further in my research and found the following behaviour:
I use on a remote system an application that transfers 3MByte of data to the local system, the local buffer size being only 65kB. The remote system (labview) finishes the data transmission in about a second. The local system needs roughly 30s, because I do computation on the data.
Now, when unplugging the network cable of the local system anytime during those 30s, Matlab continuos to stream all the data, without any losses.
This might be great for many applications, where data loss is not desired. But for my case, it is not. And honestly, I don't understand how this works. Is there another, "hidden" buffer on the local system? Can I decrease it's size?

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!