Why can't I pass a serial object to an interrupt?

1 view (last 30 days)
Question: What is going on so I can't use a serial object stored as a Global in an interrupt? (Specifically a "s.BytesAvailableFcn=@mycallback;" callback in case that matters)
I've solved my initial problem, but don't understand 'why' it's working and hope someone can help out. (If this has already been answered well, please help with the link as I couldn't find it)
Problem/Background: A serial object in my main code is used in an interrupt callback every time a termination string is read. The interrupt is set to then read the serial string and write it to a .csv for logging. I tried setting the serial object as a Global as well as trying to pass it as an input. Neither worked and I always got the error:
"Warning: The BytesAvailableFcn is being disabled. To enable the callback property either connect to the hardware with FOPEN or set the BytesAvailableFcn property."
To get around this, I had to "re-define" the serial object in the interrupt with this:
port = dir('/dev/tty.Key*');
s = instrfind('port',['/dev/',port.name]);

Answers (1)

Walter Roberson
Walter Roberson on 2 Sep 2015
The first argument passed to the BytesAvailableFcn will be the serial object, so you do not need to pull it from a global.
fid = fopen('TheLogFile.csv', 'wt');
s.BytesAvailableFcn = @(src,event) mycallback(src, event, fid)
and
function mycallback(src, event, fid)
thisline = fgetl(src);
fprintf(fid, '%s\n', thisline);
end

Categories

Find more on MATLAB Support Package for IP Cameras in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!