uart_test.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. clc
  2. close all;
  3. clear all;
  4. dt = 1;
  5. t=[0];
  6. % m=[0;0;0;0]
  7. m=[0;0;0;0;0;0;0;0;0;]
  8. p = plot(t,m)
  9. data = [];
  10. buf = [];
  11. state = 0;
  12. x=0;
  13. %legend('璘신폭箕','塘신폭箕','r2','r3','b0','b1','b2','b3')
  14. legend('품신','빈신')
  15. grid on;
  16. set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.7]);
  17. delete(instrfindall('Type','serial'));%헌잿눔왯
  18. object = serial('com3','BaudRate',115200);%토零눔왯
  19. fopen(object);
  20. while true
  21. data=fread(object,230);%뗍혤鑒앴
  22. while ~isempty(data)
  23. buf = [buf data(1)];
  24. data(1) = [];
  25. switch state
  26. case 0
  27. if length(buf)>=5
  28. if buf(1)==170
  29. Len = buf(2);
  30. Lenf = 255 - buf(2);
  31. if buf(3) == Lenf
  32. state = 1;
  33. else
  34. buf(1) = [];
  35. end
  36. else
  37. buf(1) = [];
  38. end
  39. end
  40. case 1
  41. if length(buf)>=Len
  42. ver = 0;
  43. for i=1:(Len-1)
  44. ver = ver + buf(i);
  45. end
  46. ver = mod(ver,256);
  47. % disp(ver);
  48. % disp(buf(Len));
  49. if ver==buf(Len)
  50. if buf(4) == 0
  51. disp(buf);
  52. ax_n = double(bitshift(int16(buf(5)),8)+bitshift(int16(buf(6)),0))/100;
  53. ay_n = double(bitshift(int16(buf(7)),8)+bitshift(int16(buf(8)),0))/100;
  54. az_n = double(bitshift(int16(buf(9)),8)+bitshift(int16(buf(10)),0))/100;
  55. gx_n = double(bitshift(int16(buf(11)),8)+bitshift(int16(buf(12)),0))/100;
  56. gy_n = double(bitshift(int16(buf(13)),8)+bitshift(int16(buf(14)),0))/100;
  57. gz_n = double(bitshift(int16(buf(15)),8)+bitshift(int16(buf(16)),0))/100;
  58. mx_n = double(bitshift(int16(buf(17)),8)+bitshift(int16(buf(18)),0))/100;
  59. my_n = double(bitshift(int16(buf(19)),8)+bitshift(int16(buf(20)),0))/100;
  60. mz_n = double(bitshift(int16(buf(21)),8)+bitshift(int16(buf(22)),0))/100;
  61. m = [m [ax_n;ay_n;az_n;gx_n;gy_n;gz_n;mx_n;my_n;mz_n;]];
  62. dt = dt + 1;
  63. t=[t dt];
  64. if dt>250
  65. x=x+1;
  66. end
  67. end
  68. % set(p(1),'XData',t,'YData',m(1,:))
  69. set(p(2),'XData',t,'YData',m(2,:))
  70. % set(p(3),'XData',t,'YData',m(3,:))
  71. % set(p(4),'XData',t,'YData',m(4,:))
  72. % set(p(5),'XData',t,'YData',m(5,:))
  73. % set(p(6),'XData',t,'YData',m(6,:))
  74. % set(p(7),'XData',t,'YData',m(7,:))
  75. % set(p(8),'XData',t,'YData',m(8,:))
  76. % set(p(9),'XData',t,'YData',m(9,:))
  77. buf = buf(Len:end);
  78. else
  79. buf(1) = [];
  80. end
  81. state = 0;
  82. end
  83. otherwise
  84. state = 0;
  85. end
  86. end
  87. drawnow limitrate nocallbacks
  88. axis([x x+300 -5 5]);
  89. end
  90. fclose(object);
  91. delete(object);
  92. clear object;