-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathScouseTom_ard_init.m
69 lines (50 loc) · 1.42 KB
/
ScouseTom_ard_init.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
function [ Ard,goodnessflag ] = ScouseTom_ard_init( COMPORTSTR )
%ScouseTom_ard_initialise Setup connection to arduino using
%establishcontact() function
%which sends 'A' (65) until soemthing is writtten to it
% Inputs:
% COMPORTSTR - string for com port i.e. 'COM11'
% timeout - timeout in s for communication (optional)
% Outputs:
% S - Serial object
% flag - goodnessflag flag. 1 is good
%
% written by the dissolute yet munificent Jimmy 2015
if ~isempty(instrfind)
fclose(instrfind);
end
%arduino resets on opening serial port
%open serial com
Ard=serial(COMPORTSTR,'BaudRate',115200);
% disp('Resetting Arduino');
fopen(Ard); %arduino now reset
%declare timing variables
start=tic;
elapsed=0;
%this has to be set to more than 1 s as fopen resets arduino and
%some other stuff runs before it establishes contact
timeout =5;
goodnessflag=0;
in=0;%inbyte
while elapsed < timeout
elapsed=toc(start);
if (Ard.BytesAvailable > 0)
%read a byte
in=fread(Ard,1);
%disp(in)
end
%input should be 'A' or 65
if in==65
% disp('i can hear arduino');
fprintf(Ard,'h'); % this can be anything, if its not a capital letter it is ignored
goodnessflag=1;
elapsed=timeout+1;
end
end
if goodnessflag==1
% disp('Communication with Arduino was OK');
else
disp('Commuinocation with Arduino failed! BOOO');
fclose(Ard);
end
end