%读取
clear;clc;
Ports = instrhwinfo('serial');
lenP = length(Ports.SerialPorts);
Ports.SerialPorts
if(lenP<=1)
fprintf('error: no serialPort found');
return;
end
if(lenP<3)
fprintf('error: need 3 sp');
return;
end
delete(instrfindall)
comport = lower(Ports.SerialPorts{3})
sp=serial(comport,'baudrate',115200,'parity','none','databits',8,'stopbits',1);%初始化串口
sp.status
sp.timeout = 20;
fopen(sp)
N = 200;
data = zeros(N,8);
for cnt=1:200
t1 = clock;
data(cnt,1:7) = fread(sp,7,'uint8');
t2 = clock;
data(cnt,8) = etime(t2,t1);
%fwrite(sp, data,'uint8');
end
fclose(sp);
data
%写
clear;clc;
Ports = instrhwinfo('serial');
lenP = length(Ports.SerialPorts);
Ports.SerialPorts
if(lenP<=1)
fprintf('no serialPort found');
return;
end
delete(instrfindall)
comport = lower(Ports.SerialPorts{2})
sp=serial(comport,'baudrate',115200,'parity','none','databits',8,'stopbits',1);%初始化串口
sp.status
sp.timeout = 20;
fopen(sp)
t1 = clock;
data = ['EA';'EA';'00';'01';'02';'03';'04'];
data = hex2dec(data);
for cnt=1:200
data(3) = cnt;
fwrite(sp, data,'uint8');
end
fclose(sp);