forked from mayeranalytics/pyUBX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UBXtool.py
executable file
·222 lines (197 loc) · 6.17 KB
/
UBXtool.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
#!/usr/bin/env python3
"""UBXTool is a command line app for sending and receiving UBX commands."""
import sys
import os
import serial
from time import sleep
from threading import Lock
from enum import Enum
import argparse
import datetime
import UBX
from UBXManager import UBXManager
from FSM import *
@FSM_Get(UBX.MON.VER)
class FSM_VER_Get:
pass
@FSM_Get(UBX.CFG.GNSS)
class FSM_GNSS_Get:
pass
@FSM_Get(UBX.CFG.PMS)
class FSM_PMS_Get:
pass
@FSM_Get(UBX.CFG.PM2)
class FSM_PM2_Get:
pass
@FSM_Get(UBX.CFG.RATE)
class FSM_RATE_Get:
pass
class FSM_RXM_Set:
"""FSM handling MON.VER.Get"""
_class = UBX.CFG.RXM._class
_id = UBX.CFG.RXM._id
class STATE(Enum):
START = 0
WAIT_GET = 1
WAIT_GET_ACK = 2
WAIT_SET_ACK = 3
DONE = 4
def __init__(self, lpMode):
self.state = FSM_RXM_Set.STATE.START
self.ver = None
self._gottenObj = None
self.lpMode = lpMode
def done(self):
return self.state == FSM_RXM_Set.STATE.DONE
def onUBX(self, obj, manager):
print(obj)
if self.state == FSM_RXM_Set.STATE.START:
if isObj(obj, UBX.CFG.RXM):
self._gottenObj = obj
self.state = FSM_RXM_Set.STATE.WAIT_GET_ACK
else:
print("Didnt' get a UBX.CFG.RXM")
self.state = FSM_RXM_Set.STATE.DONE
elif self.state == FSM_RXM_Set.STATE.WAIT_GET_ACK:
if isACK(obj):
self._gottenObj.lpMode = self.lpMode
manager.send(self._gottenObj.serialize())
self.state = FSM_RXM_Set.STATE.WAIT_SET_ACK
else:
print("Ooops that went wrong, I was expecting an ACK")
self.state = FSM_RXM_Set.STATE.DONE
elif self.state == FSM_RXM_Set.STATE.WAIT_SET_ACK:
if isACK(obj):
self.state = FSM_RXM_Set.STATE.DONE
else:
print("Ooops that went wrong, I was expecting an ACK")
self.state = FSM_RXM_Set.STATE.DONE
else:
raise Exception("The FSM went boink.")
class Manager(UBXManager):
def __init__(self, ser, debug=False):
UBXManager.__init__(self, ser, debug)
self._lock = Lock()
self._dumpNMEA = True # with _lock
self._fsm = None # with _lock
def setDumpNMEA(self, val):
with self._lock:
self._dumpNMEA = val
if self.debug:
print("dumpNMEA={}".format(val))
def onUBX(self, obj):
with self._lock:
if self._fsm is not None:
self._fsm.onUBX(obj, self)
if self._fsm.done():
self._fsm = None
else:
print(obj)
def onUBXError(self, msgClass, msgId, errMsg):
print(msgClass, msgId, errMsg)
def onNMEA(self, buffer):
with self._lock:
dump = self._dumpNMEA
if dump:
print("{} {}".format(datetime.datetime.now().isoformat(), buffer))
def done(self):
with self._lock:
return self._fsm is None
def waitUntilDone(self, timeout=None):
sleepTime = 0
while not self.done():
sleep(0.5)
sleepTime += 0.5
if timeout is not None and sleepTime > timeout:
return False
return True
def VER_GET(self):
msg = UBX.MON.VER.Get().serialize()
with self._lock:
self._fsm = FSM_VER_Get()
self.send(msg)
def GNSS_GET(self):
msg = UBX.CFG.GNSS.Get().serialize()
with self._lock:
self._fsm = FSM_GNSS_Get()
self.send(msg)
def PMS_GET(self):
msg = UBX.CFG.PMS.Get().serialize()
with self._lock:
self._fsm = FSM_PMS_Get()
self.send(msg)
def PM2_GET(self):
msg = UBX.CFG.PM2.Get().serialize()
with self._lock:
self._fsm = FSM_PM2_Get()
self.send(msg)
def RATE_GET(self):
msg = UBX.CFG.RATE.Get().serialize()
with self._lock:
self._fsm = FSM_RATE_Get()
self.send(msg)
def RXM_SET(self, lpMode):
msg = UBX.CFG.RXM.Get().serialize()
with self._lock:
self._fsm = FSM_RXM_Set(lpMode)
self.send(msg)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Send UBX commands to u-blox M8 device.'
)
parser.add_argument(
'--VER-GET', dest='VER_GET', action='store_true',
help='Get the version string'
)
parser.add_argument(
'--GNSS-GET', dest='GNSS_GET', action='store_true',
help='Get CFG-GNSS'
)
parser.add_argument(
'--PMS-GET', dest='PMS_GET', action='store_true',
help='Get CFG-PMS'
)
parser.add_argument(
'--PM2-GET', dest='PM2_GET', action='store_true',
help='Get CFG-PM2'
)
parser.add_argument(
'--RATE-GET', dest='RATE_GET', action='store_true',
help='Get CFG-RATE'
)
parser.add_argument(
'--RXM', dest='RXM', action='store', type=int,
help='Set the power mode (0=cont, 1=save)'
)
parser.add_argument(
'--NMEA', dest='NMEA', action='store_true',
help='Dump NMEA messages.'
)
parser.add_argument(
'-d', '--debug', dest='debug', action='store_true',
help='Turn on debug mode'
)
args = parser.parse_args()
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=None)
debug = (os.environ.get("DEBUG") is not None) or args.debug
manager = Manager(ser, debug=debug)
manager.setDumpNMEA(False) # temporarily turn off NMEA print
if debug:
sys.stderr.write("Starting UBXManager...\n")
manager.start()
sleep(1)
# do all getters
for argName in filter(lambda s: s.endswith("_GET"), args.__dict__.keys()):
if args.__dict__[argName]:
getattr(manager, argName)()
manager.waitUntilDone()
if args.RXM is not None:
manager.RXM_SET(args.RXM)
manager.waitUntilDone()
if args.NMEA:
manager.setDumpNMEA(True)
manager.join()
else:
manager.shutdown()
sys.exit(0)
sys.exit(1)