This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Specify callback function to execute when specified number of bytes are available in input buffer, or terminator is read


You configure BytesAvailableFcn to execute a callback function when a bytes-available event occurs. A bytes-available event occurs when the number of bytes specified by the BytesAvailableFcnCount property is available in the input buffer, or after a terminator is read, as determined by the BytesAvailableFcnMode property.

    Note   A bytes-available event can be generated only for asynchronous read operations.

If the RecordStatus property value is on, and a bytes-available event occurs, the record file records this information:

  • The event type as BytesAvailable

  • The time the event occurred using the format day-month-year hour:minute:second:millisecond

    Note:   You cannot use ASCII values larger than 127 characters. The function is limited to 127 binary characters.

To learn how to create a callback function, refer to Creating and Executing Callback Functions.



Any instrument object

Read only


Data type

Callback function


The default value is an empty character vector.


Create the serial port object s on a Windows® machine for a Tektronix® TDS 210 two-channel oscilloscope connected to the serial port COM1.

s = serial('COM1');

Configure s to execute the callback function instrcallback when 40 bytes are available in the input buffer.

s.BytesAvailableFcnCount = 40;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = @instrcallback;

Connect s to the oscilloscope.


Write the *IDN? command, which instructs the scope to return identification information. Because the default value for the ReadAsyncMode property is continuous, data is read as soon as it is available from the instrument.


The resulting output from instrcallback is shown below.

BytesAvailable event occurred at 18:33:35 for the object: 

56 bytes are read and instrcallback is called once. The resulting display is shown above.

ans =

Suppose you remove 25 bytes from the input buffer and issue the MEASUREMENT? command, which instructs the scope to return its measurement settings.

out = fscanf(s,'%c',25);

The resulting output from instrcallback is shown below.

BytesAvailable event occurred at 18:33:48 for the object: 

BytesAvailable event occurred at 18:33:48 for the object: 

There are now 102 bytes in the input buffer, 31 of which are left over from the *IDN? command. instrcallback is called twice; once when 40 bytes are available and once when 80 bytes are available.

ans =
Was this topic helpful?