-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScouseTom_Init.m
More file actions
96 lines (61 loc) · 2.19 KB
/
ScouseTom_Init.m
File metadata and controls
96 lines (61 loc) · 2.19 KB
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
function [ Ard ] = ScouseTom_Init( ArdComPortStr )
%ScouseTom_Init Initialises Arduino Due for ScouseTom v2
% Resets the Arduino and establishes connection
% Input - ArdComPortStr string for com port i.e. 'COM11'
% Output - Ard - serial object for arduino
% smashed together by jimmy 2015
%% Some variables
CScommerrmsg='!E';
CSpmarkerrmsg='!P';
CScommOKmsg='+OK';
%% close ports if any already open
if ~isempty(instrfind)
fclose(instrfind);
end
%% Initialise Arduino Serial Object
% in case the arduino communication was opened already
if exist('Ard','var')
close(Ard);
clear('Ard');
end
% Reset and Connect to Arduino
[Ard,commgood]=ScouseTom_ard_init(ArdComPortStr);
% check if is all well
if commgood == 0
fclose(Ard);
clear('Ard');
error('Problem with arduino communication at some point');
end
disp('Arduino Connected Fine');
pause(0.2);
%% check CS test response
[resp,numflg,cscommok]=ScouseTom_ard_getresp(Ard);
if (~cscommok)
warning('Didnt get OK message from Arduino....');
end
if strcmp(resp,CScommerrmsg)
warning('CS COMM ERROR - CHECK FRONT PANEL - autoretry when sending settings to ard');
end
if strcmp(resp,CSpmarkerrmsg)
warning('WARNING! PHASE MARKER NOT DETECTED! - Stim will not work');
end
if strcmp(resp, CScommOKmsg)
disp('Current Source connected OK! yay!');
end
%% Check Switching test response
SwOK=ScouseTom_ard_readswitchcheck(Ard);
%% Finish init
FlushSerialBuffer(Ard);
disp('Initilialisation finished - this only needs to work once, new settings can be sent repeatedly');
end
function FlushSerialBuffer(Ard)
%remove anything in the serial buffer - otherwise matters are super
%confused
while (Ard.BytesAvailable >0) %whilst there are bytes to read
jnk=fread(Ard,Ard.BytesAvailable,'uchar'); %read as much as possible from buffer
% jnkstr=sprintf(char(jnk)); %convert to string
% jnkstr=strrep(jnkstr,sprintf('\r\n'),''); %remove newlines
% fprintf(logfid,'%.2fs\t\tSerial Buffer flushed: %s\n',toc(tstart),jnkstr); %write to log
pause(0.2); %wait a bit to fill up Serial buffer is necessary - not needed on my PC but added in case related problems to the pause needed at the start
end
end