Problem with fwrite command matlab in serial communication

3 views (last 30 days)
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.

Answers (0)

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!