MATLAB: How to obtain the exact time at which a particular value was read from the serial port using MATLAB 7.7 (R2008b)

bytesavailablefcnbytesavailablefcncntMATLABserialtimestamp

I am reading data asynchronously from a serial port using MATLAB 7.7 (R2008b). I am performing this read operation using the READASYNC function. This function returns the values that are available on the serial port. I would like to obtain the exact time stamp at which each value was read from the serial port.

Best Answer

The serial port object has a callback function called "BytesAvailableFcn" and a property associated with this callback function called "BytesAvailableFcnCnt".
The "BytesAvailableFcn" is a callback that executes when a specified number of bytes is available in the input buffer, or a terminator is read.
The "BytesAvailableFcnCnt" property specifies the number of bytes that must be present in the input buffer to trigger the "BytesAvailableFcn" callback.
To obtain the time-stamp information for every byte read from the serial port, please follow the steps below:
1. Set BytesAvailableFcnCount = 1. This will cause the callback to be triggered for every byte received.
2. The event structure in the callback function contains the time at which this event was triggered. This event corresponds to the instance of time at which the byte was read from MATLAB.
Please note that if the input data rate is very large, this callback function will be triggered very often which will lead to degradation in performance.
Related Question