-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheSB.py
277 lines (248 loc) · 7.8 KB
/
eSB.py
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
# Smart Box Interface Library
# By Phil
# http://stuffandnonsense.elephantandchicken.co.uk
# 20200307
# This is an early development version
# of a python module designed to control the
# Economatics Smart Box
# Testing is conducted on an "SB-04"
import time
import serial
libVersion = "v0.01a - Phil - http://stuffandnonsense.elephantandchicken.co.uk"
ser = None
def open(port):
global ser
ser = serial.Serial(port, 9600, timeout=1)
return ser.is_open
def close():
global ser
if ser.is_open:
ser.close()
ser = None
def flush():
global ser
readText = ""
if ser.is_open:
readTest = ""
while ser.inWaiting() > 0:
time.sleep(0.5)
while ser.inWaiting() > 0:
readText += ser.read(1)
def get_sb_copyright():
global ser
ser.write(chr(9))
time.sleep(0.1)
readText = ""
print "Debug note : There is something odd about this response..."
while ser.inWaiting() > 0:
readText += ser.read(1)
#time.sleep(0.1)
return readText
def get_sb_ver():
global ser
readValue = 0.0
if ser.is_open:
ser.write(chr(1))
time.sleep(0.1)
readByteL = ord(ser.read(1))
#time.sleep(0.1)
readByteH = ord(ser.read(1))
readByteH *= 256
readValue = readByteH+readByteL
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return str(float(readValue)/1000)
def get_command_name(cmd):
global ser
readText = ""
if ser.is_open:
ser.write(chr(4)+chr(cmd))
time.sleep(0.1)
#print "Debug note : There is something odd about this response..."
while ser.inWaiting() > 0:
readText += ser.read(1)
#time.sleep(0.1)
elif not(ser.is_open):
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return readText
def status():
global ser
global libVersion
msg = "Module version : "+libVersion+"\n"
if ser.is_open:
msg += "Port is Open\nSystem version : "
msg += getSBVer()
return msg
elif not(ser.is_open):
#raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return "Port is Not Open"
def d_out_on_all():
global ser
if ser.is_open:
#print "Sending All On"
ser.write(chr(20)+chr(255))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_out_off_all():
global ser
if ser.is_open:
#print "Sending All Off"
ser.write(chr(20)+chr(0))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_out_all(val1):
global ser
if ser.is_open:
#print "Sending ",val1
ser.write(chr(20)+chr(val1))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_out_on_chn(chn):
global ser
if ser.is_open:
ser.write(chr(28)+chr(chn+1))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_out_off_chn(chn):
global ser
if ser.is_open:
ser.write(chr(29)+chr(chn+1))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_out_chn(chn, val1):
global ser
if ser.is_open:
if val1==0:
ser.write(chr(29)+chr(chn+1))
elif val1==1:
ser.write(chr(28)+chr(chn+1))
else:
raise NameError('Digital outputs need to be either 0 or 1')
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def d_in_all():
global ser
readByte = -1
if ser.is_open:
ser.write(chr(90))
time.sleep(0.1)
readByte = ord(ser.read(1))
while ser.inWaiting() > 0:
print "Weird other data : " + str(ord(ser.read(1))) # no idea why these exist
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return readByte
def d_in_chn(chn):
global ser
readByte = -1
if ser.is_open:
ser.write(chr(91)+chr(chn+1))
time.sleep(0.1)
readByte = ord(ser.read(1))
while ser.inWaiting() > 0:
print "Weird other data : " + str(ord(ser.read(1))) # no idea why these exist
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return readByte
def m_fwd_chn(chn):
global ser
if ser.is_open:
ser.write(chr(12)+chr(chn))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def m_bkw_chn(chn):
global ser
if ser.is_open:
ser.write(chr(13)+chr(chn))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def m_stp_chn(chn):
global ser
if ser.is_open:
ser.write(chr(14)+chr(chn))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def m_pwr_chn(num, onT, offT):
global ser
if ser.is_open:
ser.write(chr(15)+chr(num)+chr(onT)+chr(offT))
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def read_byte(addr):
global ser
if ser.is_open:
highB = addr/256
lowB = addr-(highB*256)
ser.write(chr(56)+chr(lowB)+chr(highB))
time.sleep(0.01)
#while ser.inWaiting() > 0:
readByte = ord(ser.read(1))
return readByte
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
def a_sensors():
global ser
analogue = []
if ser.is_open:
ser.write(chr(43))
time.sleep(0.1)
#sen = ("A","B","C","D")
i=0
while ser.inWaiting() > 0:
#print "Sensor " + sen[i] + " : " + str(ord(ser.read(1)))
analogue.append(str(ord(ser.read(1))))
i+=1
return analogue
def a_sensors_new():
global ser
analogue = []
if ser.is_open:
ser.write(chr(23))
time.sleep(0.1)
#sen = ("A","B","C","D")
i=0
while ser.inWaiting() > 0:
#print "Sensor " + sen[i] + " : " + str(ord(ser.read(1))) # no idea why these exist
analogue.append(str(ord(ser.read(1))))
i+=1
return analogue
def a_in_chn(chn):
global ser
# for now, I'm going to make it work at low resolution for simplicity
readByte = -1
if ser.is_open:
ser.write(chr(45)) # set to 8 bit mode
time.sleep(0.1)
ser.write(chr(40)+chr(chn))
time.sleep(0.1)
readByte = ord(ser.read(1))
while ser.inWaiting() > 0:
print "Weird other data : " + str(ord(ser.read(1))) # no idea why these exist
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return readByte
def a_in_f_chn(chn):
global ser
# for now, I'm going to make it work at low resolution for simplicity
readByte = -1
if ser.is_open:
ser.write(chr(45)) # set to 8 bit mode
time.sleep(0.1)
ser.write(chr(42)+chr(chn))
time.sleep(0.1)
readByte = ord(ser.read(1))
while ser.inWaiting() > 0:
print "Weird other data : " + str(ord(ser.read(1))) # no idea why these exist
else:
raise NameError('Serial connection not open - try calling eSB.open(xyz) first')
return readByte
def a_sensor_table(val1):
global ser
ser.write(chr(25)+chr(val1))
time.sleep(0.5)
readText = ""
#print "Debug note : There is something odd about this response..."
while ser.inWaiting() > 0:
readText += ser.read(1)
#time.sleep(0.1)
return readText