-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbservices.py
366 lines (309 loc) · 11.9 KB
/
bservices.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# coding: utf-8
import binascii
import ctypes
import datetime
import os
import shlex
import subprocess
import sys
import time
from struct import pack
import bluetooth
import serial
from model.model import *
# Linux location of serial ports (rfcomm0, rfcomm1, rfcomm2, etc)
DEFAULT_DEVICE = "/dev/rfcomm"
# Class to discover near bluetooth devices
class Discoverer(bluetooth.DeviceDiscoverer):
def pre_inquiry(self):
self.done = False
self.devices = {}
def device_discovered(self, address, device_class, rssi, name):
if name.startswith('TSND121') and name not in self.devices:
self.devices[name] = address
def inquiry_complete(self):
self.done = True
# Class to manage the connections
class RFCOMMConection():
def __init__(self, bdaddr=None):
self.settings = connectionSettings.select().order_by(connectionSettings.id.desc()).get()
self.bdaddr = bdaddr
self.channel = 1
self.cmd_connect = "rfcomm connect"
self.cmd_release = "rfcomm release"
self.ser = None
self.device = ""
self.header = 0x9A
self.connected = False
self.paused = False
# Release the given port(port=0 releases /dev/rfcomm0)
def release(self, port):
cmd = self.cmd_release + " " + DEFAULT_DEVICE + str(port) + "&"
print cmd
os.system(cmd)
# Makes a new connection
def connect(self):
n = 0
ls = "ls /dev/ | grep rfcomm"
p = subprocess.Popen(ls, stdout=subprocess.PIPE, shell=True)
d, err = p.communicate()
if d != "":
ports = d.split("\n")
n = len(ports) - 1
self.device = self.settings.device + str(n)
cmd = self.cmd_connect + " " + str(self.device) + " " + str(self.bdaddr) + " " + str(self.channel) + "&"
print cmd
os.system(cmd)
# wait and verify if the file was created
cs = connectionSettings.select().order_by(connectionSettings.id.desc()).get()
sleep = cs.conectionTime
d1 = ""
while sleep > 0:
d1 = self.checkSerial(n)
if d1 != "": break
time.sleep(1)
sleep = sleep - 1
if d1 == "":
n = -1
return n
def checkSerial(self, n):
ls1 = "ls /dev/ | grep rfcomm" + str(n)
p1 = subprocess.Popen(ls1, stdout=subprocess.PIPE, shell=True)
d1, err1 = p1.communicate()
return d1
# Initializes the serial port with the action selected in the GUI
def initDevice(self):
try:
self.ser = serial.Serial(port=self.device, timeout=self.settings.timeout, baudrate=self.settings.baudrate)
print self.ser
except Exception as detail:
print detail
return self.ser
def comandSwitcher(self, action):
# 計測開始
mtsettings = MeasurementReservation.select().order_by(MeasurementReservation.id.desc()).get()
cmd = mtsettings.cmd # 0x13
smode = mtsettings.smode # 0x00 # start mode: 0 relative time, 1 absolute time
syear = mtsettings.syear # 0x00 # start year: Amount of years since 2000
smonth = mtsettings.smonth # 0x01 # start month
sday = mtsettings.sday # 0x01 # start day
shour = mtsettings.shour # 0x00 # start hour
smin = mtsettings.smin # 0x00 # start minute
ssec = mtsettings.ssec # 0x00 # start second
emode = mtsettings.emode # 0x00 # end mode: 0 relative time, 1 absolute time
eyear = mtsettings.eyear # 0x00 # end year: Amount of years since 2000
emonth = mtsettings.emonth # 0x01 # end month
eday = mtsettings.eday # 0x01 # end day
ehour = mtsettings.ehour # 0x00 # end hour
emin = mtsettings.emin # 0x00 # end minute
esec = mtsettings.esec # 0x00 # end second
check = self.checkSum(
[cmd, smode, syear, smonth, sday, shour, smin, ssec, emode, eyear, emonth, eday, ehour, emin, esec])
list = [chr(self.header), chr(cmd), chr(smode), chr(syear), chr(smonth), chr(sday), chr(shour), chr(smin),
chr(ssec), chr(emode), chr(eyear), chr(emonth), chr(eday), chr(ehour), chr(emin), chr(esec),
chr(check)]
# バッファクリア
self.clearBuffer()
self.ser.write(bytearray(list))
str = self.ser.readline()
if action == 1:
self.angularVelocity()
elif action == 2:
self.geomagnetic()
else:
self.atmospheric()
def angularVelocity(self):
xasettings = XASettings.select().order_by(XASettings.id.desc()).get()
# 加速度・角速度パラメータ設定
cmd = xasettings.cmd # 0x16
data = xasettings.mode # 0x01
data1 = xasettings.dataTransmission # 0x0A
data2 = xasettings.dataRecording # 0x00
check = self.checkSum([cmd, data, data1, data2])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(data1), chr(data2), chr(check)])
# self.clearBuffer()
self.ser.write(repr(list))
def geomagnetic(self):
gmsettings = GMSettings.select().order_by(GMSettings.id.desc()).get()
# 加速度・角速度パラメータ設定
cmd = gmsettings.cmd # 0x16
data = gmsettings.mode # 0x01
data1 = gmsettings.dataTransmission # 0x0A
data2 = gmsettings.dataRecording # 0x00
check = self.checkSum([cmd, data, data1, data2])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(data1), chr(data2), chr(check)])
# self.clearBuffer()
self.ser.write(repr(list))
def atmospheric(self):
apsettings = APSettings.select().order_by(APSettings.id.desc()).get()
# 加速度・角速度パラメータ設定
cmd = apsettings.cmd # 0x16
data = apsettings.mode # 0x01
data1 = apsettings.dataTransmission # 0x0A
data2 = apsettings.dataRecording # 0x00
check = self.checkSum([cmd, data, data1, data2])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(data1), chr(data2), chr(check)])
# self.clearBuffer()
self.ser.write(repr(list))
def clearBuffer(self):
# バッファクリア
if self.ser:
self.ser.read(1000)
# Read the response
def readResponse(self):
while self.connected:
try:
str = self.ser.readline()
# print repr(str)
# 計測開始通知
# Measurement start notification
str = self.ser.read(1)
# print "2"
# print repr(str)
# ヘッダ検索
# Retrieve header
while ord(str) != 0x9A:
str = self.ser.read(1)
# コマンド取得
# Retrieve command
str = self.ser.read(1)
# print repr(str)
# 加速度角速度計測データ通知のみ処理する
# Acceleration angular velocity measurement data notification
if ord(str) == 0x80:
yield self.angularVelocityResponse()
except Exception as e:
print e
self.connected = False
yield None
return
def angularVelocityResponse(self):
data = []
# タイムスタンプ
# timestamp
ts = ord(self.ser.read(1))
ts += ord(self.ser.read(1)) << 8
ts += ord(self.ser.read(1)) << 16
ts += ord(self.ser.read(1)) << 24
# print datetime.date.fromtimestamp(ts)
# 加速度X
# Acceleration X
data1 = self.ser.read(1)
data2 = self.ser.read(1)
data3 = self.ser.read(1)
# 3byteの値を4byteのint型としてマイナスのハンドリング
if ord(data3) & 0x80:
data4 = "\xFF"
else:
data4 = "\x00"
data.append(binascii.b2a_hex(data1))
data.append(binascii.b2a_hex(data2))
data.append(binascii.b2a_hex(data3))
data.append(binascii.b2a_hex(data4))
# エンディアン変換
# Endian conversion
accx = ord(data1)
accx += ord(data2) << 8
accx += ord(data3) << 16
accx += ord(data4) << 24
data.append(ctypes.c_int(accx).value)
data.append(time.time())
if self.checkSerial(self.device[-1:]) == "":
self.connected = False
return None
return ctypes.c_int(accx).value
def setTime(self, year, month, day, hour, min, sec, ms1, ms2):
# '\x9a\x8f\x01\x14' ERROR
cmd = 0x11
year = year - 2000
ms1 = (ms1 // 1000) & 0xFF
ms2 = ((ms2 // 1000) >> 8) & 0xFF
check = self.checkSum([cmd, year, month, day, hour, min, sec, ms1, ms2])
list = bytearray(
[chr(self.header), chr(cmd), chr(year), chr(month), chr(day), chr(hour), chr(min), chr(sec), chr(ms1),
chr(ms2), chr(check)])
self.clearBuffer()
self.ser.write(list)
print repr(self.ser.readline())
def stopMeassuring(self):
cmd = 0x15
data = 0x00
check = self.checkSum([cmd, data])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(check)])
self.clearBuffer()
self.ser.write(list)
print repr(self.ser.readline())
def batteryStatus(self):
cmd = 0x3B
data = 0x00
info = {}
check = self.checkSum([cmd, data])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(check)])
self.clearBuffer()
self.ser.write(list)
# '\x9a\xbb\x99\x01d\xdd'
# retrieve header
str = self.ser.read(1)
while ord(str) != 0x9A:
str = self.ser.read(1)
# response command
str = self.ser.read(1)
if ord(str) == 0xbb:
info["voltage"] = binascii.b2a_hex(self.ser.read(2))
info["percent"] = binascii.b2a_hex(self.ser.read(1))
return info
def timeAcq(self):
cmd = 0x12
data = 0x00
check = self.checkSum([cmd, data])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(check)])
self.clearBuffer()
self.ser.write(list)
# retrieve header
str = self.ser.read(1)
while ord(str) != 0x9A:
str = self.ser.read(1)
# response command
str = self.ser.read(1)
if ord(str) == 0x92:
year = binascii.b2a_hex(self.ser.read(1))
month = binascii.b2a_hex(self.ser.read(1))
day = binascii.b2a_hex(self.ser.read(1))
min = binascii.b2a_hex(self.ser.read(1))
sec = int(binascii.b2a_hex(self.ser.read(1)), 16)
print year
print month
print day
print min
print sec
def getInfo(self):
# "\x9a\x90AP06120208\x00\x07\x80K|'\x12\x11\x06\x17TSND121\x00\x00\x00\xae"
cmd = 0x10
data = 0x00
info = {}
check = self.checkSum([cmd, data])
list = bytearray([chr(self.header), chr(cmd), chr(data), chr(check)])
self.clearBuffer()
self.ser.write(list)
# retrieve header
str = self.ser.read(1)
while ord(str) != 0x9A:
str = self.ser.read(1)
# response command
str = self.ser.read(1)
if ord(str) == 0x90:
info["sn"] = self.ser.read(10)
info["mac"] = binascii.b2a_hex(self.ser.read(6))
info["swv"] = binascii.b2a_hex(self.ser.read(4))
info["name"] = ""
str = self.ser.read(1)
while ord(str) != 0x00:
info["name"] += str
str = self.ser.read(1)
return info
def checkSum(self, params):
check = self.header ^ params[0]
for idx, i in enumerate(params):
if idx != 0:
check = check ^ i
return check