uart_TEST.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. clc
  2. close all;
  3. dt = 1;
  4. t=[0];
  5. % m=[0;0;0;0]
  6. m=[0;0;0;0;0;0;0;0]
  7. p = plot(t,m)
  8. data = [];
  9. buf = [];
  10. x=0;
  11. legend('前后','左右','上下','压力')
  12. grid on;
  13. % set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7]);
  14. set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7],'Interruptible','off');
  15. delete(instrfindall('Type','serial'));%清理串口
  16. object = serial('com5','BaudRate',115200);%配置串口
  17. fopen(object);
  18. state = 0;
  19. Len = 0;
  20. Lenf = 0;
  21. Zarr = [];
  22. Zarr(1) = 0;
  23. Zarr_en = 0;
  24. run = 0;
  25. left = 0;
  26. right = 0;
  27. acc3_b = 0;
  28. acc3_bb = 0;
  29. % for i=1:shuliang
  30. while true
  31. data=fread(object);%读取数据
  32. while ~isempty(data)
  33. buf = [buf data(1)];
  34. data(1) = [];
  35. switch state
  36. case 0
  37. if length(buf)>=5
  38. if buf(1)==170 && buf(2)==187 && buf(3)==204
  39. Len = buf(4);
  40. Lenf = 255 - buf(5);
  41. if Len==Lenf
  42. state = 1;
  43. else
  44. buf(1) = [];
  45. end
  46. else
  47. buf(1) = [];
  48. end
  49. end
  50. case 1
  51. if length(buf)>=Len
  52. ver = 0;
  53. for i=1:(Len-1)
  54. ver = ver + buf(i);
  55. end
  56. ver = mod(ver,256);
  57. % disp(ver);
  58. % disp(buf(Len));
  59. if ver==buf(Len)
  60. if buf(6)==0 && buf(7)==1
  61. clc
  62. disp(buf);
  63. if buf(8)==0
  64. disp('MOTION_STOP');
  65. elseif buf(8)==1
  66. disp('MOTION_RUN');
  67. elseif buf(8)==2
  68. disp('MOTION_JUMP');
  69. elseif buf(8)==3
  70. disp('MOTION_DOWN');
  71. elseif buf(8)==4
  72. disp('MOTION_LEFT');
  73. elseif buf(8)==5
  74. disp('MOTION_RIGHT');
  75. elseif buf(8)==6
  76. disp('MOTION_FRONT');
  77. elseif buf(8)==7
  78. disp('MOTION_BACK');
  79. end
  80. disp(double(bitshift(int16(buf(9)),8)+int16(buf(10))));
  81. end
  82. if buf(6)==0 && buf(7)==0
  83. % acc(1)=double(bitshift(int32(buf(8)),24)+bitshift(int32(buf(9)),16)+bitshift(int32(buf(10)),8)+int32(buf(11)));
  84. % acc(2)=double(bitshift(int32(buf(12)),24)+bitshift(int32(buf(13)),16)+bitshift(int32(buf(14)),8)+int32(buf(15)));
  85. % acc(3)=double(bitshift(int32(buf(16)),24)+bitshift(int32(buf(17)),16)+bitshift(int32(buf(18)),8)+int32(buf(19)));
  86. press(1)=double(bitshift(uint16(buf(8)),8)+uint16(buf(9)));
  87. press(2)=double(bitshift(uint16(buf(10)),8)+uint16(buf(11)));
  88. disp(press);
  89. press(1) = (press(1) - 39900)/1000.0;
  90. press(2) = (press(2) - 35000)/1000.0;
  91. acc(1)=double(bitshift(int16(buf(12)),8)+int16(buf(13)));
  92. acc(2)=double(bitshift(int16(buf(14)),8)+int16(buf(15)));
  93. acc(3)=double(bitshift(int16(buf(16)),8)+int16(buf(17)));
  94. acc(4)=double(bitshift(int16(buf(18)),8)+int16(buf(19)));
  95. acc(5)=double(bitshift(int16(buf(20)),8)+int16(buf(21)));
  96. acc(6)=double(bitshift(int16(buf(22)),8)+int16(buf(23)));
  97. acc = acc/8192.0;
  98. disp(acc);
  99. dt = dt + 1;
  100. t=[t dt];
  101. if dt>450
  102. x=x+1;
  103. end
  104. m=[m [acc(1)+6;acc(2)+6;acc(3)+6;acc(4)+2;acc(5)+2;acc(6)+2;press(1)-8;press(2)-8]];
  105. set(p(1),'XData',t,'YData',m(1,:))
  106. set(p(2),'XData',t,'YData',m(2,:))
  107. set(p(3),'XData',t,'YData',m(3,:))
  108. set(p(4),'XData',t,'YData',m(4,:))
  109. set(p(5),'XData',t,'YData',m(5,:))
  110. set(p(6),'XData',t,'YData',m(6,:))
  111. set(p(7),'XData',t,'YData',m(7,:))
  112. set(p(8),'XData',t,'YData',m(8,:))
  113. end
  114. buf = buf(Len:end);
  115. else
  116. buf(1) = [];
  117. end
  118. state = 0;
  119. end
  120. otherwise
  121. state = 0;
  122. end
  123. end
  124. axis([x x+500 -10 10]);
  125. drawnow limitrate nocallbacks
  126. end
  127. fclose(object);
  128. delete(object);
  129. clear object;