Instrcallback function and while loops

조회 수: 4 (최근 30일)
Maheen Siddiqui
Maheen Siddiqui 2016년 4월 4일
댓글: Walter Roberson 2016년 4월 7일
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
appenderFile=fopen('Event_Markers.txt','a+t');
s=serial('COM3');
set(s,'BytesAvailable',{@myCallback,appenderFile});
set(s,'BytesAvailableFcnCount',1);
set(s,'BytesAvailableFcnMode','byte');
fopen(s);
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!!

답변 (2개)

Dave Behera
Dave Behera 2016년 4월 6일
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 2016년 4월 6일
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(isstruct(event)),
if ~(isfield(event, 'Type') && isfield(event, 'Data'))
error(Error.error1Id, Error.error1);
end
EventType = event.Type;
if(strcmpi(EventType, 'BytesAvailable')),
bytesToRead = get(serialObj, 'BytesAvailablefcnCount');
T = fread(serialObj, bytesToRead, 'uchar')';
end
end
You can also refer to the 'instrcallback' template by entering the following command in MATLAB command window and base your callback function accordingly
type('instrcallback')
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 4월 7일
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
fread(s,s.BytesAvailable)
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by