BytesAvailableFcn

Specify the M-file callback function to execute when a specified number of bytes is available in the input buffer, or a terminator is read

Description

You configure BytesAvailableFcn to execute an M-file 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.

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

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

Characteristics

Read only

Never

Data type

Callback function

Values

The default value is an empty string.

Example

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

s = serial('COM1');

Configure s to execute the M-file 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.

fopen(s)

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.

fprintf(s,'*IDN?')

MATLAB displays:

BytesAvailable event occurred at 18:33:35 for the object: 
Serial-COM1.

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

s.BytesAvailable
ans =
    56

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

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

MATLAB displays:

BytesAvailable event occurred at 18:33:48 for the object: 
Serial-COM1.

BytesAvailable event occurred at 18:33:48 for the object: 
Serial-COM1.

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.

s.BytesAvailable
ans =
   102

See Also

Functions

record

Properties

BytesAvailableFcnCount, BytesAvailableFcnMode, RecordStatus, Terminator, TransferStatus

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS