Matlab, Arduino Communication using BytesAvailableFcn Too many input arguments

1 次查看(过去 30 天)
Hey,
I'm having some Problems with the Serial Communication from Matlab to an Arduino.
It's supposed to be asynchronus read Operation (input from user) so I am using the 'BytesAvaiableFcn'.
Problem is I always get an Errormsg:
??? Error using ==>
mycallbacktest
Too many input arguments.
Error in ==> instrcb at 36
feval(val{1}, obj,
eventStruct,
val{2:end});
Warning: The BytesAvailableFcn
is being disabled. To enable the
callback property
either connect to the hardware
with FOPEN or set the
BytesAvailableFcn property.
My Matlab code for initalizing the serial Port is:
s = serial('com5');
set(s, 'BaudRate', 38400,'InputBufferSize', 1024, 'OutputBufferSize', 1024,'timeout',100);
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 3;
s.BytesAvailableFcn ={'mycallbacktest',s};
fopen(s);
pause;
To test my callback function I got this code
function [] = mycallbacktest(s)
ardudata = fscan(s);
%set buttons
butsym{1,1} = '!';
butsym{1,2} = '?';
if strcmp(ardudata(1),butsym{1,1})
disp('case1')
elseif strcmp(ardudata(1),butsym{1,2})
disp('case2')
else
disp('case3')
% led_num = str2num(ardudata);
pause(1)
end
The Data from the Arduino:
case 1:
!!!
case 2:
???
case 3:
end of line char
XXX
in case 3 I get a new line Char and in the next line a 3 digit number in the range from 001-090

采纳的回答

Sean de Wolski
Sean de Wolski 2015-2-9
Using the string syntax for mycallback doesn't allow you to change the inputs. You'll probably want something like this
s.BytesAvailableFcn =@(~,~)mycallbacktest(s};
Now we ignore (~) the two default arguments given by every callback function (why you're getting the error), and pass in s, what your function needs.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by