-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgi2c_Dngl_ELV.py
213 lines (153 loc) · 7.62 KB
/
gi2c_Dngl_ELV.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
Code for the ELV USB-I2C Dongle
(adapted from file: I2Cpytools_dgl_ELV.py)
"""
###############################################################################
# This file is part of GeigerLog.
#
# GeigerLog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GeigerLog is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GeigerLog. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
__author__ = "ullix"
__copyright__ = "Copyright 2016, 2017, 2018, 2019, 2020"
__credits__ = [""]
__license__ = "GPL3"
from gutils import *
class ELVdongle:
"""Code for the ELV USB-I2C Dongle"""
# ELV name xX time reqB wrt/rec info rec
pTemplate = "ELV {:7s} {:2s} {:10s} [{:3d}] [{:3d}] {:20s} == {}"
def __init__(self):
"""make instance of dongle"""
self.name = "ELVdongle"
self.short = "dongle"
def ELVinit(self):
"""opening the serial port and testing for correct dongle"""
# open serial port
msg = "port: {} (baudrate:{}, timeout:{}s, timeout_write:{}s)"\
.format(gglobs.I2Cusbport, gglobs.I2Cbaudrate, gglobs.I2Ctimeout, gglobs.I2Ctimeout_write)
try:
gglobs.I2Cser = serial.Serial(gglobs.I2Cusbport, gglobs.I2Cbaudrate, timeout=gglobs.I2Ctimeout, write_timeout=gglobs.I2Ctimeout_write)
dprint("ELV dongle opened at " + msg)
except Exception as e:
exceptPrint(e, sys.exc_info(), "ERROR Serial port for ELV dongle could not be opened")
return False, msg
# stop macro with '<' and set 'y30' in order to NOT get ACK and NACK
# and then get info by '?', all in single command
self.ELVwriteAdmin(b'<y30?', name=self.short, info="Init Dongle")
rec = self.ELVreadAdmin(140, name=self.short, info="").strip()
if rec.startswith(b"ELV"):
return True, "" # ought to be an ELV dongle
else:
return False, "Not an ELV dongle!<br>at {}".format(msg) # not an ELV dongle
def ELVreset(self):
"""Reset the ELV dongle (takes 2 sec!)"""
#print("\n---- Reset")
self.ELVwriteAdmin(b'z4b', name=self.short, info="Reset Dongle")
time.sleep(2) # 1 sec is NOT enough
rec = self.ELVreadAdmin(name=self.short, info="")
#print(rec.decode("utf-8") )
def ELVgetInfo(self):
"""Show the startup info"""
self.ELVwriteAdmin(b'?', name=self.short, info="Show Info")
rec = self.ELVreadAdmin(140, name=self.short, info="").strip()
return rec.decode("utf-8").split("\r\n")
def ELVgetMacro(self):
""" Print the Macro in Memory """
self.ELVwriteAdmin(b'U', name=self.short, info="Show Macrocode" )
rec = self.ELVreadAdmin(255+26, name=self.short, info="")
rd = (rec.decode("utf-8")).split("\r\n")
return rec.decode("utf-8")
def ELVwriteAdmin (self, command, name= "-", info = "no info"):
"""write to the ELV USB-I2C - only for its interal admin functions"""
command = command.upper()
wrt = gglobs.I2Cser.write(command) # wrt = no of bytes written
# print( self.pTemplate.format(name, "TA", stime()[11:], len(command), wrt, info, command))
def ELVreadAdmin (self, length = 100, name = "-", info = "no info"):
"""read from the ELV USB-I2C - only for its internal admin functions"""
rec = gglobs.I2Cser.read(length)
cnt = gglobs.I2Cser.in_waiting
if cnt > 0:
print("ELVreadAdmin: Bytes waiting:", cnt)
while True: # read single byte until nothing is returned
x = gglobs.I2Cser.read(1)
if len(x) == 0: break
rec += x
# print("ELVreadAdmin: len(rec), rec:", len(rec), rec)
rec = rec.strip()
# print( self.pTemplate.format(name, "RA", stime()[11:], length, len(rec), info, rec))
return rec
def ELVaskDongle(self, addr, data, rbytes, name="no name", info="no info", doPrint=True, end="\n"):
""" Function doc """
doPrint = False
self.ELVwriteData(addr, data, name=name, info=info, doPrint=doPrint)
if rbytes > 0:
self.ELVinitializeRead(addr, rbytes, name=name, doPrint=doPrint)
answ = self.ELVreadData(length=rbytes, name=name, doPrint=doPrint)
if doPrint: print(end=end)
else:
answ = None
return answ
def ELVwriteData (self, addr, data, name="", info ="", doPrint=True):
"""write commands for the sensors via the ELV USB-I2C"""
doPrint = False
wA = "{:02X}".format((addr << 1) + 0)
wdata = ""
for a in data: wdata += " {:02X}".format(a) # puts a space at the beginning
command = bytes('S {}{} P'.format(wA, wdata), 'ASCII').upper()
wrt = gglobs.I2Cser.write(command) # wrt = no of bytes written
if doPrint: print(self.pTemplate.format(name, "TX", stime()[11:], len(command), wrt, info, command))
def ELVinitializeRead(self, addr, rbytes, name="", info="", doPrint=True):
"""initialize reading from the sensor via the ELV USB-I2C"""
doPrint = False
rA = "{:02X}".format((addr << 1) + 1) # read address
rb = "{:02X}".format(rbytes) # no of bytes
command = bytes('S {} {} P'.format(rA, rb), 'ASCII').upper()
wrt = gglobs.I2Cser.write(command) # wrt = no of bytes written
if doPrint: print(self.pTemplate.format("", "iR", stime()[11:], len(command), wrt, info, command))
def ELVreadData (self, length=1, name="", info="", doPrint=True):
""" read data from the ELV USB-I2C """
doPrint=False
rec = gglobs.I2Cser.read(length * 3 + 2) # 3 chars per byte('FF ') + CR, LF
cnt = gglobs.I2Cser.in_waiting
if cnt > 0:
print("ELVreadData Bytes waiting:", cnt)
while True: # read single byte until nothing is returned
x = gglobs.I2Cser.read(1)
if len(x) == 0: break
rec += x
rec = rec.rstrip(b"\r\n") # remove carridge return, linefeed, keep last space
if doPrint: print( self.pTemplate.format("", "RX", stime()[11:], length, len(rec), info, rec), end="")
if not rec.strip().startswith(b"Solve ") or \
not rec.strip().startswith(b"Err: "): # conversion unless an error msg from dongle
rlist = self.__getListfromRec(rec)
return rlist
else:
return rec
def ELVclose(self):
""" Close the serial port """
gglobs.I2Cser.close()
wprint("ELV is closed")
def __getListfromRec(self, rec):
""" convert ASCII string from terminal to list of integer """
# rec : b'BE 6E 9A 69 32 00 ...'
# returns: [190, 110, 154, 105, 50, 0, ...]
rlist = []
if len(rec) >= 3:
for i in range(0, len(rec), 3):
rlist.append(int(rec[i:i+3], 16))
else: # empty record
pass
return rlist