Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1.  
  2. %run('clean');
  3. clear all;
  4. close all;
  5.  
  6. s = serial('COM1'); %assigns the object s to serial port
  7.  
  8. set(s, 'InputBufferSize', 256); %number of bytes in inout buffer
  9. set(s, 'FlowControl', 'hardware');
  10. set(s, 'BaudRate', 9600);
  11. set(s, 'Parity', 'none');
  12. set(s, 'DataBits', 8);
  13. set(s, 'StopBit', 1);
  14. set(s, 'Timeout',10);
  15. %clc;
  16.  
  17. disp(get(s,'Name'));
  18. prop(1)=(get(s,'BaudRate'));
  19. prop(2)=(get(s,'DataBits'));
  20. prop(3)=(get(s, 'StopBit'));
  21. prop(4)=(get(s, 'InputBufferSize'));
  22.  
  23. disp(['Port Setup Done!!',num2str(prop)]);
  24.  
  25. fopen(s); %opens the serial port
  26. t=1;
  27. disp('Running');
  28. x=0;
  29. while(t < 200) %Runs for 200 cycles - if you cant see the symbol, it is "less than" sign. so while (t less than 200)
  30.  
  31. a =fread(s); %reads the data from the serial port and stores it to the matrix a
  32. a=max(a); % in this particular example, I'm plotting the maximum value of the 256B input buffer
  33.  
  34. x =[x a]; % Merging the value to an array, this is not very computationaly effective, as the array size is dynamic.
  35. %Consider pre allocation the size of the array to avoid this. But beware, You might loose some important
  36. %data at the end!
  37.  
  38. plot(x);
  39. axis auto;
  40. grid on;
  41.  
  42. disp([num2str(t),'th iteration max= ',num2str(a)]);
  43. hold on;
  44. t=t+1;
  45. a=0; %Clear the buffer
  46. drawnow;
  47. end
  48.  
  49. fclose(s); %close the serial port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement