This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
CAN-USB-stick.py
297 lines (257 loc) · 9.21 KB
/
CAN-USB-stick.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
#!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 by sailoog <https://github.com/sailoog/openplotter>
# e-sailing <https://github.com/e-sailing/openplotter>
# Openplotter 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 2 of the License, or
# any later version.
# Openplotter 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 Openplotter. If not, see <http://www.gnu.org/licenses/>.
import wx, socket, threading, time, webbrowser, serial, codecs, datetime, sys
from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
from classes.conf import Conf
from classes.language import Language
class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, ListCtrlAutoWidthMixin):
def __init__(self, parent, width,height):
wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT, size=(width, height))
CheckListCtrlMixin.__init__(self)
ListCtrlAutoWidthMixin.__init__(self)
class MyFrame(wx.Frame):
def __init__(self):
self.ttimer=40
self.conf = Conf()
self.home = self.conf.home
self.currentpath = self.home+self.conf.get('GENERAL', 'op_folder')+'/openplotter'
try:
self.ser = serial.Serial(self.conf.get('N2K', 'can_usb'), 115200, timeout=0.5)
except:
print 'failed to start N2K output setting on '+self.conf.get('N2K', 'can_usb')
sys.exit(0)
Language(self.conf)
Buf_ = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
self.Buffer = bytearray(Buf_)
self.Zustand=6
self.buffer=0
self.PGN_list=[]
self.list_N2K_txt=[]
self.list_count=[]
self.p=0
wx.Frame.__init__(self, None, title="N2K setting", size=(650,435))
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.timer_act, self.timer)
self.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
self.icon = wx.Icon(self.currentpath+'/openplotter.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(self.icon)
panel = wx.Panel(self, 100)
wx.StaticText(panel, wx.ID_ANY, label="TX PGN", style=wx.ALIGN_CENTER, pos=(10,5))
self.list_N2K = CheckListCtrl(panel, 400,260)
self.list_N2K.SetBackgroundColour((230,230,230))
self.list_N2K.SetPosition((10, 25))
self.list_N2K.InsertColumn(0, _('PGN'), width=90)
self.list_N2K.InsertColumn(1, _('info'), width=200)
wx.StaticText(panel, wx.ID_ANY, label="enabled transmit PGN:", style=wx.ALIGN_CENTER, pos=(10,300))
self.printing = wx.StaticText(panel, size=(600, 70), pos=(10, 320))
self.check_b = wx.Button(panel, label=_('check'),size=(70, 32), pos=(550, 20))
self.Bind(wx.EVT_BUTTON, self.check, self.check_b)
self.apply_b = wx.Button(panel, label=_('apply'),size=(70, 32), pos=(550, 60))
self.Bind(wx.EVT_BUTTON, self.apply, self.apply_b)
self.Centre()
self.read_N2K()
self.timer.Start(self.ttimer)
self.check(0)
self.Show(True)
def check(self,e):
self.Send_Command(1, 0x49, 0)
time.sleep(0.2)
self.read_stick_check()
def apply(self,e):
st=''
counter=0
for ii in self.list_N2K_txt:
if self.list_N2K.IsChecked(counter):
exist=0
for jj in self.PGN_list:
if ii[0]==str(jj):
exist=1
if exist==0:
st+=ii[0]+' '
if len(self.PGN_list)<36:
self.sendTX_PGN(int(ii[0]),1)
self.PGN_list.append(ii[0])
time.sleep(0.2)
counter+=1
print 'new PGNs=',st
st=''
for jj in self.PGN_list:
counter=0
for ii in self.list_N2K_txt:
exist=0
if ii[0]==str(jj):
if not self.list_N2K.IsChecked(counter):
exist=1
if exist==1:
st+=ii[0]+' '
self.sendTX_PGN(int(ii[0]),0)
time.sleep(0.2)
counter+=1
print 'del PGNs=',st
self.Send_Command(1, 0x01, 0)
def sendTX_PGN(self,lPGN,add):
if add:
data_ = (0,0,0,0,1,0xFE,0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF)
else:
data_ = (0,0,0,0,0,0,0,0,0,0,0,0,0)
data = bytearray(data_)
data[0]=lPGN&255
data[1]=(lPGN >> 8)&255
data[2]=(lPGN >> 16)&255
self.Send_Command(14, 0x47, data)
def Send_Command(self, length, command, arg):
data_ = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
data = bytearray(data_)
data[0] = 0xa1 #command
data[1] = length #Actisense length
data[2] = command
i=3
while i<length+2:
data[i] = arg[i-3]
i+=1
self.SendCommandtoSerial(data)
def timer_act(self,e):
self.getCharfromSerial()
def OnClose(self, event):
self.timer.Stop()
self.Destroy()
def SendCommandtoSerial(self, TXs):
crc = 0
#start = codecs.decode('1002', 'hex_codec')
start = (0x10, 0x02)
ende = codecs.decode('1003', 'hex_codec')
ende = (0x10, 0x03)
i = 0
while i < TXs[1] + 2:
crc += TXs[i]
i += 1
crc = (256 - crc) & 255
TXs[TXs[1] + 2] = crc
i = 0
self.ser.write(chr(start[0]))
self.ser.write(chr(start[1]))
while i < TXs[1] + 3:
self.ser.write(chr(TXs[i]))
#print format(TXs[i], '02x')
if TXs[i] == 0x10:
self.ser.write(chr(TXs[i]))
i += 1
self.ser.write(chr(ende[0]))
self.ser.write(chr(ende[1]))
def getCharfromSerial(self):
bytesToRead = self.ser.inWaiting()
if bytesToRead>0:
buffer=self.ser.read(bytesToRead)
for i in buffer:
#sys.stdout.write(' '+hex(ord(i)))
self.parse(ord(i))
def parse(self, b):
if self.Zustand == 6: # zu Beginn auf 0x10 warten
if b == 0x10:
self.Zustand = 0x10
elif self.Zustand == 0x10:
if b == 0x10: # 0x10 Schreiben wenn zweimal hintereinander
self.Buffer[self.p] = b
self.p += 1
self.Zustand = 0
elif b == 0x02: # Anfang gefunden
self.p = 0
self.Zustand = 0
elif b == 0x03: # Ende gefunden
if self.crcCheck():
#print "CRC"
self.output()
self.p = 0
self.Zustand = 6 # Auf Anfang zuruecksetzen
elif self.Zustand == 0:
if b == 0x10:
self.Zustand = 0x10
else:
self.Buffer[self.p] = b
self.p += 1
def crcCheck(self):
crc = 0
i = 0
while i < self.p:
crc =(crc+ self.Buffer[i]) & 255
i += 1
return (crc == 0)
def output(self):
k = 0
if self.Buffer[0] == 0x93 and self.Buffer[1] == self.p - 3:
#i=0
#while i < 10:
# sys.stdout.write(' '+hex(self.Buffer[i]))
# i += 1
#print ' '
pass
else:
if self.Buffer[2] == 0x49 and self.Buffer[3] == 0x01:
j = 0
st=''
self.PGN_list=[]
while j < self.Buffer[14]:
i=j*4
lPGN=self.Buffer[15+i]+self.Buffer[16+i]*256+self.Buffer[17+i]*256*256
self.PGN_list.append(lPGN)
j+=1
st+=str(lPGN)+' '
if ((j)%6)==0 and j>0:
st+='\n'
self.printing.SetLabel(st)
self.conf.set('N2K', 'pgn_output', st)
def getCommandfromSerial(self, RXs):
crc = 0
start = (0x10, 0x02)
ende = (0x10, 0x03)
i = 0
while i < RXs[1] + 2:
crc += RXs[i]
i += 1
crc = (256 - crc) & 255
RXs[RXs[1] + 2] = crc
i = 0
self.ser.write(chr(start[0]))
self.ser.write(chr(start[1]))
while i < RXs[1] + 3:
self.ser.write(chr(RXs[i]))
#print format(RXs[i], '02x')
if RXs[i] == 0x10:
self.ser.write(chr(RXs[i]))
i += 1
self.ser.write(chr(ende[0]))
self.ser.write(chr(ende[1]))
def read_N2K(self):
if self.list_N2K.GetItemCount()<3:
while self.list_N2K.GetItemCount()>3:
self.list_N2K.DeleteItem(self.list_N2K.GetItemCount()-1)
self.list_N2K_txt=[]
with open(self.currentpath+'/classes/N2K_PGN.csv') as f:
self.list_N2K_txt = [x.strip('\n\r').split(',') for x in f.readlines()]
for ii in self.list_N2K_txt:
pgn=int(ii[0])
self.list_N2K.Append([pgn,ii[1]])
def read_stick_check(self):
counter=0
for ii in self.list_N2K_txt:
for jj in self.PGN_list:
if ii[0]==str(jj):
self.list_N2K.CheckItem(counter)
counter+=1
app = wx.App()
MyFrame().Show()
app.MainLoop()