Reading two different formated data types from serial port

2 次查看(过去 30 天)
Hey, I have been trying to read some data sent from an Arduino into Matlab. It works fine for my first type of data, but when i try to read the second set of data with fscanf, Matlab just shows me 2 empty chars. Below I included a hex dump of my data types and the corresponding Ascii Values (the dots representing new line chars or carriage returns):
21 21 21 0D 0A !!!..
2nd set of data
0D 0A 30 33 36 0D 0A ..036..
Thanks in advance
The relevant Matlab code
Initalizing serial Port:
function [] = s_init_run()
global s
s = serial('com6'); set(s, 'BaudRate', 38400,'Terminator', 'CR/LF',... 'InputBufferSize', 1024, 'OutputBufferSize', 1024,'timeout',1000);
s.BytesAvailableFcnCount = 5;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn =@(~,~)mycallback(s);
ready = 'on';
fopen(s);
pause(2);
fprintf(s,'199');
pause(2);
fprintf(s,'189');
end
callback function
function [] = mycallback(s)
global u
global erg
%callback from serial port
ardudata1 = fscanf(s);
zeichen = ''; r=1; for ll=1:length(ardudata1)
if isspace(ardudata1(ll))==0
zeichen(1,r) = ardudata1(ll);
r=r+1;
end
end
ardudata = zeichen
butsym{1,1} = '!';
butsym{1,2} = '?';
butsym{1,3} = 'q';
flushinput(s);
if u == size(erg.conds,3)
disp('done')
else
if strcmp(ardudata(1),butsym{1,1})
fprintf(s,'191')
gp_signal; %generates signal
pause(1)
elseif strcmp(ardudata(1),butsym{1,2})
u = u+1; %sets counter+1 and sends some data to the arduino
fprintf(s,'189')
pause(2);
fprintf(s,'199')
elseif strcmp(ardudata(1),butsym{1,3})
gp_signal; %generates signal
pause(1)
else
led_num = str2num(ardudata);
s_fnct(led_num) %saves values
pause(1)
end
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by