-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLink
More file actions
executable file
·307 lines (283 loc) · 9.36 KB
/
Copy pathDataLink
File metadata and controls
executable file
·307 lines (283 loc) · 9.36 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#! /bin/bash
################################################################################
# Arduino Data Link #
################################################################################
# #
# Version 1.0 #
# Written by: Tibor Áser Veres #
# Source: https://github.com/RPBCACUEAIIBH/Arduino-DataLink #
# License: BSD License 2.0 (see LICENSE.md file) #
# #
################################################################################
# Constants
Files="/usr/share/DataLink"
if [[ -d $1 ]]
then
RunDir=$1
shift
else
RunDir=$(pwd)
fi
# Processing options
SendRequest=false
Quit=false
while [[ ! -z $@ ]]
do
case $1 in
--help ) cat $Files/Help.md
exit
;;
--version ) echo ''
echo 'Arduino DataLink - Version 1.0'
echo ''
cat $Files/LICENSE.md
echo ''
exit
;;
-s ) SendRequest=true
shift
Request=$1
shift
;;
-e ) Quit=true
shift
Over=$1
shift
;;
* ) if [[ -z $Port ]]
then
Port=$1
shift
else
Speed=$1
shift
fi
;;
esac
done
# Root check
if [[ $(whoami) != "root" ]]
then
echo 'This script needs to run as root! Please use "sudo" in front.'
exit
fi
# Checking and creating log file if necessary.
DT=$(date -Is)
Time=${DT:11:8}
Success=true
if [[ ! -f $RunDir/Log ]]
then
touch $RunDir/Log
chown $SUDO_USER:$SUDO_USER $RunDir/Log
echo "$Time N: Starting log..." | tee $RunDir/Log
DT=$(date -Is); echo "${DT:11:8} W: Log file was missing!" | tee -a $RunDir/Log
else
echo "$Time N: Starting log..." | tee $RunDir/Log
fi
# Checking Parts and initialising
if [[ ! -f $RunDir/Controls ]]
then
DT=$(date -Is); echo "${DT:11:8} W: Controls file missing!" | tee -a $RunDir/Log
touch $RunDir/Controls
chown $SUDO_USER:$SUDO_USER $RunDir/Controls
# Read is default, but may not be desired depending on your program to start reading immediately... Change "Read" to "Stop" to put it on stendby by default...(both the condition, and the default value.)
echo -n "Read" > $RunDir/Controls # << Here
else
State=$(cat $RunDir/Controls) # Reseting state to read...
if [[ $State != "Read" ]] # << here
then
echo -n "Read" > $RunDir/Controls # << here
fi
fi
if [[ ! -f $RunDir/Input ]]
then
DT=$(date -Is); echo "${DT:11:8} W: Input file missing!" | tee -a $RunDir/Log
touch $RunDir/Input
chown $SUDO_USER:$SUDO_USER $RunDir/Input
fi
if [[ ! -z $(cat $RunDir/Input) ]]
then
DT=$(date -Is); echo "${DT:11:8} W: Input file not empty... Cleaning!" | tee -a $RunDir/Log
echo -n "" > $RunDir/Input
fi
if [[ ! -f $RunDir/Output ]]
then
DT=$(date -Is); echo "${DT:11:8} W: Output file missing!" | tee -a $RunDir/Log
touch $RunDir/Output
chown $SUDO_USER:$SUDO_USER $RunDir/Output
fi
if [[ ! -z $(cat $RunDir/Output) ]]
then
DT=$(date -Is); echo "${DT:11:8} W: Output file not empty... Cleaning!" | tee -a $RunDir/Log
echo -n "" > $RunDir/Output
fi
# Saving previous port configuration and reconfiguring port
PPC=$(stty -F $Port -g)
function Configure
{
# Checking and setting port
if [[ -z $Port ]]
then
Port=$(grep "Port:" $RunDir/Config | awk '{ print $2 }')
fi
if [[ -z $Port ]]
then
DT=$(date -Is); echo "${DT:11:8} E: Port not specified!" | tee -a $RunDir/Log
Success=false
else
if [[ ! -e $Port ]]
then
DT=$(date -Is); echo "${DT:11:8} E: Device not found!" | tee -a $RunDir/Log
Success=false
fi
fi
# Checking baudrate
if [[ -z $Speed ]]
then
Speed=$(grep "BaudRate:" $RunDir/Config | awk '{ print $2 }')
fi
if [[ ! -z $Speed ]]
then
case $Speed in
300 ) Speed=300
;;
600 ) Speed=600
;;
1200 ) Speed=1200
;;
2400 ) Speed=2400
;;
4800 ) Speed=4800
;;
9600 ) Speed=9600
;;
19200 ) Speed=19200
;;
57600 ) Speed=57600
;;
115200 ) Speed=115200
;;
500000 ) Speed=500000
;;
1000000 ) Speed=1000000
;;
2000000 ) Speed=2000000
;;
* ) DT=$(date -Is); echo "${DT:11:8} W: Unkown baud rate! Falling back to 9600..." | tee -a $RunDir/Log
Speed=9600
;;
esac
else
Speed=9600
fi
if [[ ! -z $(grep "E:" $RunDir/Log) ]]
then
DT=$(date -Is); echo "${DT:11:8} N: Fatal error happened! Aborting..." | tee -a $RunDir/Log
sleep 5
exit
fi
# Configuring port
stty -F $Port cs8 $Speed min 0 time 0 -parenb -parodd -cmspar -hupcl -cstopb cread clocal -crtscts ignbrk -brkint -ignpar -parmrk inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig -icanon -iexten -echo -echoe -echok -echonl noflsh -xcase -tostop -echoprt -echoctl -echoke -flusho -extproc
# Opening channel (It appears that "cat" command can read and clear the buffer but can not request data from the microcontroller. On the other hand "tail" does not seem to clear the buffer so neither will keep reading data indefinitely alone... They seem to work together just fine if tail is started first and keeps running in the background...
tail -f $Port&
}
# Calling function Configure
Configure
if [[ $(cat $RunDir/Log) == "$Time N: Starting log..." ]]
then
DT=$(date -Is); echo "${DT:11:8} N: Successful initialization!" | tee -a $RunDir/Log
else
if [[ ! -z $(grep "E:" $RunDir/Log) ]]
then
DT=$(date -Is); echo "${DT:11:8} N: Fatal error happened! Aborting..." | tee -a $RunDir/Log
exit
else
DT=$(date -Is); echo "${DT:11:8} N: One or more errors ware corrected!" | tee -a $RunDir/Log
fi
fi
# -s option
if [[ $SendRequest == true ]]
then
if [[ -z $Request ]]
then
DT=$(date -Is); echo "${DT:11:8} E: Request signal is not specified! Please edit the Output file!" | tee -a $RunDir/Log
else
DT=$(date -Is); echo "${DT:11:8} N: Requesting..." | tee -a $RunDir/Log
echo $Request > $RunDir/Output
fi
fi
# -e option
if [[ $Quit == true ]]
then
if [[ -z $Over ]]
then
DT=$(date -Is); echo "${DT:11:8} E: Over signal is not specified! Please edit the Controls file!" | tee -a $RunDir/Log
fi
fi
# Waiting for the arduino to reset
sleep 7 # my arduino runs at 16MHz it resets in somewhat less then 3s, but I'm not sure if a 8MHz one would reset so soon...
# Main section
State="Read"
while [[ $State != "Exit" ]]
do
LastState=$State
State=$(awk '{ print $1 }' $RunDir/Controls)
if [[ $Quit == true ]] # -e option
then
if [[ ! -z $(grep "$Over" $RunDir/Input) ]]
then
State="Exit"
fi
fi
if [[ ! -z $(cat $RunDir/Output) ]]
then
echo -n $(cat $RunDir/Output) > $Port # Remove the "-n" option for sending new line character...
echo -n "" > $RunDir/Output
fi
if [[ $State == "Read" ]]
then
Discard=$(cat $Port) #The firts read stuff is junk in the buffer...
fi
if [[ $State == "Reading" ]]
then
cat $Port | tee -a $RunDir/Input
else
sleep 0.25
fi
case $State in
Read ) echo "Reading" > $RunDir/Controls
DT=$(date -Is); echo "${DT:11:8} N: Reading..." >> $RunDir/Log
;;
Stop ) echo "Stopped" > $RunDir/Controls
DT=$(date -Is); echo "${DT:11:8} N: Stopping..." >> $RunDir/Log
;;
Exit ) DT=$(date -Is); echo "${DT:11:8} N: Exiting..." >> $RunDir/Log
;;
Clear ) echo -n "" > $RunDir/Input
State=$LastState
echo -n $State > $RunDir/Controls
;;
Reading ) :
;;
Reconfigure ) echo -n "Stopped" > $RunDir/Controls
kill $(ps -e | grep tail | awk '{ print $1 }')
Port=$(grep "Port:" $RunDir/Config | awk '{ print $2 }')
Speed=$(grep "BaudRate:" $RunDir/Config | awk '{ print $2 }')
Configure
echo -n " Reconfigured" >> $RunDir/Controls
;;
Stopped ) :
;;
* ) DT=$(date -Is); echo "${DT:11:8} W: Unknown state request! Falling back to previous state!" >> $RunDir/Log
State=$LastState
echo -n $State > $RunDir/Controls
;;
esac
done
# Closing channel
kill $(ps -e | grep tail | awk '{ print $1 }') #Fix this! It will kill all tail operations including non-related ones!
# Restoring previous port settings... (arduino IDE may have problem uploading or using serial monitor if not restored...)
stty $PPC -F $Port # It gives me an error like crazy. but it does what it supposed to do...
# Ending log and exiting
DT=$(date -Is); echo "${DT:11:8} N: Ending log!" >> $RunDir/Log
echo "Exited" > $RunDir/Controls