Why can't I pass a serial object to an interrupt?
2 次查看(过去 30 天)
显示 更早的评论
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]);
0 个评论
回答(1 个)
Walter Roberson
2015-9-2
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!