Instrument Control Toolbox | ![]() ![]() |
Example: Using Events and Actions to Read Binary Data
This example extends Example: Reading Binary Data by using the M-file action function instraction
to display event-related information to the command line when a bytes-available event occurs during a binary read operation.
1. Create an instrument object - Create the GPIB object g
associated with a National Instruments GPIB controller with board index 0, and an instrument with primary address 1.
g = gpib('ni',0,1);
2. Configure properties - Configure the input buffer to accept a reasonably large number of bytes, and configure the timeout value to two minutes to account for slow data transfer.
g.InputBufferSize = 50000;
g.Timeout = 120;
Configure g
to execute the action function instraction
every time 5000 bytes is stored in the input buffer.
g.BytesAvailableActionMode = 'byte';
g.BytesAvailableActionCount = 5000;
g.BytesAvailableAction = 'instraction';
3. Connect to the instrument - Connect g
to the oscilloscope.
fopen(g)
4. Write and read data - Configure the scope to transfer the screen display as a bitmap.
fprintf(g,'HARDCOPY:PORT GPIB') fprintf(g,'HARDCOPY:FORMAT BMP') fprintf(g,'HARDCOPY START')
Initiate the asynchronous read operation, and begin generating events.
readasync(g)
instraction
is called every time 5000 bytes is stored in the input buffer. The resulting displays are shown below.
BytesAvailable event occurred at 09:41:42 for the object: GPIB0-1. BytesAvailable event occurred at 09:41:50 for the object: GPIB0-1. BytesAvailable event occurred at 09:41:58 for the object: GPIB0-1. BytesAvailable event occurred at 09:42:06 for the object: GPIB0-1. BytesAvailable event occurred at 09:42:14 for the object: GPIB0-1. BytesAvailable event occurred at 09:42:22 for the object: GPIB0-1. BytesAvailable event occurred at 09:42:30 for the object: GPIB0-1.
Wait until all the data is sent to the input buffer, and then transfer the data to MATLAB as unsigned 8-bit integers.
g.TransferStatus ans = idle out = fread(g,g.BytesAvailable,'uint8');
5. Disconnect and clean up - When you no longer need g
, you should disconnect it from the instrument, and remove it from memory and from the MATLAB workspace.
fclose(g) delete(g) clear g
![]() | Enabling Action Functions After They Error | Triggers | ![]() |