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

**

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))

你可能感兴趣的:(matlab,java,开发语言)