Matlab实时读取串口数据并实时画图方法

发布时间:2023年12月26日

**

Matlab实时读取串口数据并实时画图方法

**
按照数据串口协议如:$KT2,1.80,88.18,39.54,42.86,LO=[0.72,-1.04,0.35],举例。

s = serialport("COM12",115200,"Timeout",5);
pos=zeros(100000,3);
j=1;
data1= read(s,1,"uint8");
% while (data1 ~= 10)
while (j<500)
  data1= read(s,1,"uint8");  
while (data1 == '[')
  for i=1:3
     sum=0;
     sign=1;
     data1= read(s,1,"uint8");
     if (data1=='-') 
         sign=-1;
         data1= read(s,1,"uint8");
     end
     while (data1~='.')
        data3=data1-48;
        sum=sum*10+data3;
        data1= read(s,1,"uint8");         
     end

     data1= read(s,1,"uint8"); 
     data3=data1-48;
     sum=sum+data3*0.1;
     data1= read(s,1,"uint8"); 
     data3=data1-48;
     sum=sum+data3*0.01;
     data1= read(s,1,"uint8"); 
     sum=sum*sign;
     pos(j,i)=sum;
    
  end
  j=j+1;
end

end
delete(s);
plot(pos(1:500,1),pos(1:500,2))
文章来源:https://blog.csdn.net/der_power/article/details/135206418
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。