Problem with fwrite command matlab in serial communication
1 次查看(过去 30 天)
显示 更早的评论
Thanks for answering !
I have a problem with fwrite command matlab in serial communication.
First, these shows you my code
in file testx.m ---------------- s=serial('COM4');
set(s,'BytesAvailableFcn',@BytesAvailable_Callback);
set(s,'BaudRate',38400);
set(s,'BytesAvailableFcnMode','terminator');
set(s,'Terminator','LF');
set(s,'ReadAsyncMode','continuous');
flushinput(s);
global x
global k
k=0;
x=cell(0);
fopen(s);
-------------
this in in BytesAvailable_Callback.m
-----------------------
function BytesAvailable_Callback(obj,event)
if (get(obj,'BytesAvailable')~=5)
disp('Error data');
else
disp('Data is 5 bytes (4 bytes a float and a byte LF)');
d=fread(obj,1,'float');
% fread(obj,1,'char');
global x
num=numel(x);
x{num+1,1}=d;
end
flushinput(obj);
-----------------
I am trying communicating with my Atmega16 chip. When starting, my chip sends through USART 4 bytes of a float and finishes with a LF byte (10 in char).
When I program, this float is 3.454. Matlab received this number (3.454). But when running, I command these:
flushoutput(s);
fwrite(s,10000000.647767567567,'float','async');
Matlab was not call the BytesAvailable_Callback although when I uses the fread command in workspace, I saw there had many LF bytes. (I programmed my chip send the float number had received through USART).
Thanks for help.
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Instrument Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!