-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRead_ArduSerial_old.m
127 lines (91 loc) · 3.72 KB
/
Read_ArduSerial_old.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
%For GUI, see http://stackoverflow.com/questions/8751590/matlab-gui-using-guide-want-to-dynamically-update-graphs?rq=1
global Ardu_serial_input session;
Ardu_serial_input = serial('COM4');%,'baudrate',9600,'terminator','CR');%'tag','Quad'
% You will have to set the serial port settings
Ardu_serial_input.BaudRate=9600;
% Termination character for data sequences
Ardu_serial_input.Terminator='CR';
% The byte order is important for interpreting binary data
Ardu_serial_input.ByteOrder='bigEndian';
%% Prepare for the data stream
Ardu_serial_input.InputBufferSize=512; % in bytes
% The "BytesAvailableFcn" function will be called whenever
% BytesAvailableFcnCount number of bytes have been received from the USB
% device.
Ardu_serial_input.BytesAvailableFcnMode='byte';
Ardu_serial_input.BytesAvailableFcnCount=1; % 1 kB of data
% The name of the BytesAvailableFcn function in this example is
% "getNewData", and it has one additional input argument ("arg1").
% Ardu_serial_input.BytesAvailableFcn = {@storeDataFromSerial,arg1};
%% Setup device
% The serial port object must be opened for communication
if strcmp(Ardu_serial_input.Status,'closed'), fopen(Ardu_serial_input); end
% Send a command. The terminator character set above will be appended.
% fprintf(Ardu_serial_input,'WAKEUP');
%
%% Read the response
session.MouseData.rew=0;
session.MouseData.frontcount=0;
session.MouseData.leftcount=0;
session.MouseData.lefttime=[];
session.MouseData.rightcount=0;
session.MouseData.righttime=[];
session.gameon = true;
tic
session.timestart=toc;
timer_ardu = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',{@storeDataFromSerial}); %'TasksToExecute', 10, ... % Number of times to run the timer object %'timerOn=false; disp(''Updating GUI!'')', 'TimerFcn', {@GUIUpdate,handles});
start(timer_ardu);
disp ('Connection established.');
if session.gameon == false
stop(timer_ardu);
delete(timer_ardu);
% Close the serial port
fclose(Ardu_serial_input);
delete(Ardu_serial_input);
clear Ardu_serial_input;
disp ('End of session');
end
% Use the serial port object to pass data between your main function
% and the serial port function ("getNewData").
%% 5. Process the incoming data
% In this example, we use a loop to plot the data stream that is sent by
% the USB device.
% A global variable is used to exit the loop
global PLOTLOOP; PLOTLOOP=1;
% Initialize data for plotting. "plotWindow" will be the length of the
% x-axis in the data plot.
plotData=zeros(plotWindow);
newData=[];
% Create figure for plotting
pfig = figure;
% This allows us to stop the test by pressing a key
set(pfig,'KeyPressFcn', @stopStream);
% Send commands to the device to start the data stream.
fprintf(Ardu_serial_input,'START');
while PLOTLOOP
% wait until we have new data
if Ardu_serial_input.UserData.isNew==1
% get the data from serial port object (data will be row-oriented)
newData=mr.UserData.newData';
% indicate that data has been read
mr.UserData.isNew=0;
% concatenate new data for plotting
plotData=[plotData(size(newData,1)+1:end,:); newData];
% plot the data
plot(pfig,plotData);
drawnow;
end
% The loop will exit when the user presses return, using the
% KeyPressFcn of the plot window
end
%% 6. Finish & Cleanup
% Add whatever commands are required for closing your device.
% Send commands to the device stop the data transmission
fprintf(Ardu_serial_input,'STOP');
% flush the input buffer
ba=get(Ardu_serial_input,'BytesAvailable');
if ba > 0, fread(mr,ba); end
% Close the serial port
fclose(Ardu_serial_input);
delete(Ardu_serial_input);
return