How to use BytesAvailableFcn for serial communication in App Designer?

조회 수: 22 (최근 30일)
Drofdarb
Drofdarb 2016년 4월 3일
댓글: Drofdarb 2016년 4월 4일
I've been trying to utilize BytesAvailableFcn in order receive data from a serial port in App Designer. My program has no problem sending data and I know it can receive data if I tell it when to check for data (ex: button function that includes fscanf). Unfortunately, I would like it to consistently check for data without needing user input. I know this could technically be implemented with a constant loop, but that would be extremely slow and I know you can implement a bytes available callback function in vanilla MATLAB. In fact, I have that working with SerialComm.m
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM4', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM4');
else
fclose(obj1);
obj1 = obj1(1);
end
% Configure instrument object, obj1.
set(obj1, 'Terminator', {13,13});
% Configure instrument object, obj1.
set(obj1, 'Timeout', 3);
set(obj1, 'BytesAvailableFcnMode', 'terminator');
obj1.BytesAvailableFcn = @(obj1,event) mycallback(obj1, event);
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
fprintf(obj1, 'i');
data1 = cellstr(fscanf(obj1));
disp(data1);
while (true)
end
% Disconnect all objects.
fclose(obj1);
% Clean up all objects.
delete(obj1);
and mycallback.m
function mycallback(src, event)
thisline = (fgetl(src));
disp(thisline);
end
However, when I attempt to implement this in App Designer in the methods section, I have
function receiveData(app, src, event)
disp('Hello');
% disp(fget1(app.obj1));
end
where app is the first argument as required by App Designer. When my BytesAvailableFcn line becomes
app.obj1.BytesAvailableFcn = @(app.obj1,event) receiveData(app.obj1, event);
I get an "Unexpected MATLAB operator" error. If I try to do this line with "src" instead of "app.obj1", then I receive the error "Undefined function 'receiveData' for input arguments of type 'serial'." as well as the notification that "The BytesAvailable Fcn is being disabled."
I would like to say that I think App Designer is a fantastic new feature and I've greatly enjoyed working with it aside from this one hiccup. Any help would be greatly appreciated. Thank you in advance and in case you're curious, this is the entire function for my communication initialization in App Designer
port = app.commPort.Value;
portString = ['COM' num2str(port)];
% Find a serial port object.
app.obj1 = instrfind('Type', 'serial', 'Port', portString, 'Tag', '');
% Configure instrument object, obj1.
set(app.obj1, 'Terminator', {13,13});
% Configure instrument object, obj1.
set(app.obj1, 'Timeout', 10.0);
% set(app.obj1, 'BytesAvailableFcn', @receiveData);
set(app.obj1, 'BytesAvailableFcnMode', 'terminator');
app.obj1.BytesAvailableFcn = @(src,event) receiveData(src, event);
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(app.obj1)
app.obj1 = serial(portString);
else
fclose(app.obj1);
app.obj1 = app.obj1(1);
end
% Connect to instrument object, obj1.
if isvalid(app.obj1)
fopen(app.obj1);
outputString = app.output.Value;
app.output.Value = ['Port Connected'; outputString];
else
outputString = app.output.Value;
app.output.Value = ['Port not valid'; outputString];
end

채택된 답변

kopytjuk
kopytjuk 2016년 4월 3일
Hey,
in my application I read the serial port via callback function in the following way.
BytesAvailableFcn:
app.serial_handler.BytesAvailableFcn = {@app.serial_data_available};
The implemented callback method of the app class (private access):
methods (Access = private)
% ...
function serial_data_available(app, src, event)
data = fgetl(app.serial_handler);
% ...
end
% ...
end
I hope I could help you. Have a nice evening!
  댓글 수: 1
Drofdarb
Drofdarb 2016년 4월 4일
Thank you so much for your help. I didn't realize that I needed the "app." in front of my function. That was the missing piece.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 View and Analyze Simulation Results에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by