MATLAB Answers

Instrcallback function and while loops

4 views (last 30 days)
Maheen Siddiqui
Maheen Siddiqui on 4 Apr 2016
Commented: Walter Roberson on 7 Apr 2016
I am trying to make use of Matlab instrument call back function however I am slightly confused. If I set my object such that I have a callback function and set it to be triggered when 1 byte is available, for eg:
% code
Then do I need to have anything else in the script? For example I am interested in recording the output from fread(s,s.BytesAvailable). So should I specify this inside the callback function or somewhere later in the script?
More specifically I am also interested in categorising the output from fread. In my fread I either get the output to be "67" or "86" and I want to so if the output is 67... Fwrite this or while the output is 86... Fwrite this. But I'm unsure if these if and while loops should be in the callback function or in the script later on? And if I am creating loops to read the port, does it defeat the purpose of a callback function?
I don't have much knowledge of callback functions and I couldn't understand this through documentation. Any advice will be appreciated!!

Answers (2)

Dave Behera
Dave Behera on 6 Apr 2016
It seems you will need to define a property called 'ReadAsyncMode'. See this link:
and go to section 'Defining an Asynchronous Read Callback' in it.
Also, to categorise the read data as you mentioned, you will need to add the corresponding logic to your callback function.

Ritesh Naik
Ritesh Naik on 6 Apr 2016
Hi Maheen,
I understand that you have set the callback function to be triggered when bytes-available event is generated. In your case since you have set the 'BytesAvailableFcnCount' to 1, bytes-available event would be generated everytime a byte is available in the input buffer.
Use 'fprintf' or 'fwrite' after you open the serial object to send data/command to the device i.e if device has to receive some instruction/command to send the data back as a reply.
Once device starts sending data, input buffer receives bytes thus generating bytes-available event. that in turns calls a callback function.
At this moment, there are bytes available in the input buffer when the callback function is called and you can place the 'fread' to read the data from the input buffer and 'if' logic to compare the bytes and send the data accordingly to the device using 'fwrite' inside the callback function.
For example: Following callback function reads the data sent by the device which is available in the the input buffer.
function CallBack(serialObj, event)
if ~(isfield(event, 'Type') && isfield(event, 'Data'))
error(Error.error1Id, Error.error1);
EventType = event.Type;
if(strcmpi(EventType, 'BytesAvailable')),
bytesToRead = get(serialObj, 'BytesAvailablefcnCount');
T = fread(serialObj, bytesToRead, 'uchar')';
You can also refer to the 'instrcallback' template by entering the following command in MATLAB command window and base your callback function accordingly
  1 Comment
Walter Roberson
Walter Roberson on 7 Apr 2016
Note: the callback is invoked once each time BytesAvailableFcnCount more bytes are available, no matter how many bytes at a time you read using fread(). So if you
then because you are reading all of the available bytes, if multiple bytes were queued, you would end up processing additional queued callbacks during which no further bytes were available, having been gobbled already. To avoid problems, you should have the callback fetch the BytesAvailable, assign that to a variable, test the value to be sure it is non-0, and if not then read that many bytes. Assigning to a variable is to reduce "race conditions" during which the BytesAvailable might change between the time you tested and the time you use it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!