-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeying.py
More file actions
executable file
·488 lines (381 loc) · 16.2 KB
/
keying.py
File metadata and controls
executable file
·488 lines (381 loc) · 16.2 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
################################################################################
#
# Keying.py - Rev 1.0
# Copyright (C) 2021-5 by Joseph B. Attili, joe DOT aa2il AT gmail DOT com
#
# Routines relating to keying the rig.
#
################################################################################
#
# This program 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.
#
# This program 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.
#
################################################################################
import sys
import os
from nano_io import *
from utilities import list_all_serial_devices,error_trap,get_PIDs,find_serial_device,find_serial_device_by_serial_id
from rig_io import BAUD,SERIAL_PORT2,SERIAL_PORT4
from tkinter import messagebox
################################################################################
class serial_dummy():
def __init__(self):
print('Serial dummy object substituted')
self.in_waiting = 0
self.out_waiting = 0
return
def setDTR(self,a):
return
def write(self,a=None,b=None):
return
def toggle_dtr(n=1):
print('Toggling DTR ...')
for i in range(n):
time.sleep(1)
print('DTR on')
ser.setDTR(True)
time.sleep(1)
print('DTR off')
ser.setDTR(False)
#sys.exit(0)
################################################################################
def find_keyer(P):
DEVICES = ['WINKEYER','NANO IO','K3NG']
BAUDS = [1200,NANO_BAUD,NANO_BAUD]
CMDS = [chr(0)+chr(2),'~?','\\?']
RESPONSES = [chr(0x17),'nanoIO ver','K3NG Keyer']
KEYER_DEVICE = P.SETTINGS["MY_KEYER_DEVICE"]
KEYER_DEVICE_ID = P.SETTINGS["MY_KEYER_DEVICE_ID"]
print('\nFIND KEYER: Looking for keyer device ...')
print('\tKEYER_DEVICE =',KEYER_DEVICE)
print('\tKEYER_DEVICE_ID =',KEYER_DEVICE_ID)
print('\tKEYER_PORT =',P.KEYER_PORT)
if KEYER_DEVICE_ID=='':
print('\n*** Fatal Error *** Need to set MY_KEYER_DEVICE_ID ',
'in ~/.keyerrc so we can find the keyer port :-(\n')
print('\nThese are the USB devices available:')
list_all_serial_devices(True)
sys.exit(0)
# There are a couple of ways to find the device - need to figure out what will work with winbloz
list_all_serial_devices(USB_ONLY=True)
device,vid_pid=find_serial_device(KEYER_DEVICE_ID,0,VERBOSITY=2,PORT=P.KEYER_PORT)
print('\tkeyer device=',device,'\tvid_pid=',vid_pid)
if not device:
print('... Not found - Looking for ESP32 keyer device ...')
device,vid_pid=find_serial_device('nanoIO32',0,VERBOSITY=2)
print('\tdevice=',device)
if device:
print(' ... There it is on port',device,' ...\n')
set_DTR_hangup(device,False)
#set_DTR_hangup(device,True)
#sys.exit(0)
# Forget the search until we get the winkeyer working for Lloyd
if len(KEYER_DEVICE)==0:
KEYER_DEVICE='WINKEYER'
if len(KEYER_DEVICE)>0:
return device,KEYER_DEVICE,vid_pid
else:
print('\nNo serial keyer device found\n')
return None,None,None
for i in range(len(DEVICES)):
print('\nFIND KEYER: Looking for',DEVICES[i],'device ...')
baud=BAUDS[i]
ser = serial.Serial(device,baud,timeout=1,
xonxoff=False,dsrdtr=False,rtscts=False)
print('\tser=',ser)
time.sleep(.1)
ser.reset_input_buffer()
#time.sleep(.1)
#ser.reset_input_buffer
#ser.reset_output_buffer
#time.sleep(.1)
cnt=ser.write(bytes(CMDS[i],'utf-8'))
time.sleep(.1)
txt2 = ser.read(256).decode("utf-8",'ignore')
print('FIND KEYER: \tCMD=',show_hex(CMDS[i]),
'\ntxt2=',txt2,'\ntxt2=',show_hex(txt2),
'\tlen=',len(txt2))
"""
if i==0 and len(txt2)==0 and False:
print('Try again ...')
time.sleep(5)
cnt=ser.write(bytes(CMDS[i],'utf-8'))
time.sleep(.1)
txt2 = ser.read(256).decode("utf-8",'ignore')
print('txt2=',txt2,'\n',show_hex(txt2),'\tlen=',len(txt2))
"""
ntries=1
while len(txt2)==256 and ntries<10:
ntries+=1
print('Try again - ',ntries,'of 10 ...')
ser.close()
time.sleep(.1)
ser.open()
time.sleep(.1)
ser.reset_input_buffer()
ser.reset_output_buffer()
time.sleep(.1)
txt2 = ser.read(256).decode("utf-8",'ignore')
print('txt2=',txt2,'\n',show_hex(txt2),'\tlen=',len(txt2))
if RESPONSES[i] in txt2:
print('Found',DEVICES[i],'Device')
return device,DEVICES[i],vid_pid
ser.close()
return device,None,vid_pid
################################################################################
# Function to open keying control port
def open_keying_port(P,sock,rig_num):
if not sock:
return None,None
vid_pid=None
print('\nOPEN KEYING PORT: USE_KEYER=',P.USE_KEYER,'\trig_num=',rig_num)
print('\tFIND_KEYER =',P.FIND_KEYER)
print('\tKEYER_PORT =',P.KEYER_PORT)
if P.gui:
P.gui.status_bar.setText("Opening Keying Port ...")
ser_direct = open_direct_keying_port(P,sock)
if P.USE_KEYER and rig_num==1:
if P.FIND_KEYER or P.KEYER_PORT!=None:
device,dev_type,vid_pid=find_keyer(P)
print('device=',device,'\tdev_type=',dev_type)
Done = dev_type!=None
while not Done:
print('\nOPEN_LEYING_PORT: Unable to find keyer device!')
pids = get_PIDs('pyKeyer.py') + get_PIDs('paddling.py')
print('\npids=',pids)
if len(pids)>1:
print("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
print( "@ Try killing other instances of this program! @")
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
else:
print("\n@@@@@@@@@@@@@@@@@@@@@@@@")
print( "@ Is it plugged in ??? @")
print( "@@@@@@@@@@@@@@@@@@@@@@@@\n")
# Let's see if another process is using the keyer
if True:
print('\nChecking device usage ...')
cmd="lsof "+device
print('\tcmd=',cmd)
os.system(cmd)
result = try_usb_reset(P,vid_pid)
else:
P.NANO_IO = dev_type=='NANO IO'
P.K3NG_IO = dev_type=='K3NG'
P.WINKEYER = dev_type=='WINKEYER'
time.sleep(.1)
else:
device=None
Done=False
while not Done:
try_again=False
FATAL_ERROR=False
if P.NANO_IO:
protocol='NANO_IO'
BAUD_KEYER=NANO_BAUD
elif P.K3NG_IO:
protocol='K3NG_IO'
BAUD_KEYER=NANO_BAUD
elif P.WINKEYER:
protocol='WINKEYER'
BAUD_KEYER=1200
else:
print('OPEN KEYING PORT - Unknown protocol')
sys.exit(0)
try:
print('OPEN KEYING PORT: device=',device,'\tprotocol=',protocol)
P.keyer_device = KEYING_DEVICE(P,device,protocol,baud=BAUD_KEYER)
ser = P.keyer_device.ser
if ser==None:
try_again=True
raise ValueError('OPEN KEYING PORT: Unable to open keying device')
else:
Done=True
except:
error_trap('KEYING->OPEN KEYING PORT',1)
print('\n*************************************')
print( '*** Unable to open Nano IO device ***')
print( '*** Make sure it is plugged in ***')
print( '*** and that no app is using it ***')
print( '*************************************')
print( '*** Giving up ***')
print('*************************************\n')
pids = get_PIDs('pyKeyer.py') + get_PIDs('paddling.py')
print('\npids=',pids)
if len(pids)>0:
print("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
print( "@ Try killing other instances of this program! @")
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
result=try_usb_reset(P,vid_pid)
print('\tresult=',result)
if result==None:
print('OPEN KEYING PORT - Try again?')
FATAL_ERROR=False
elif result=='CANCEL':
print('OPEN KEYING PORT - Bailing out!')
sys.exit(0)
elif result:
print('keyer_device=',P.keyer_device)
print('\tdevice=',P.keyer_device.device,'\tser=',P.keyer_device.ser)
else:
print('keyer_device=',P.keyer_device)
print('\tdevice=',P.keyer_device.device,'\tser=',P.keyer_device.ser)
if P.keyer_device.device==None and P.keyer_device.ser==None:
print('Proceeding without keyer')
return None,ser_direct
else:
FATAL_ERROR=not try_again
if FATAL_ERROR:
sys.exit(0)
else:
ser = ser_direct
return ser,ser_direct
def try_usb_reset(P,vid_pid):
print('\nTRY USB RESET: vid_pid=',vid_pid)
msg='Try Resetting USB Bus?'
lab="pyKeyer"
if P.gui:
P.gui.splash.hide()
#result=messagebox.askyesno(lab,msg)
result=messagebox.askyesnocancel(lab,msg)
print('\tresult=',result)
if result==True:
if vid_pid==None:
device,dev_type,vid_pid=find_keyer(P)
if vid_pid==None:
print('\nTRY USB RESET: Unable to find a device to reset - vid_pid=',vid_pid)
return None
cmd="sudo usbreset "+vid_pid
print('\tcmd=',cmd)
os.system(cmd)
Done = False
time.sleep(5)
elif result==False:
device,dev_type,vid_pid=find_keyer(P)
print('device=',device,'\tdev_type=',dev_type)
Done = dev_type!=None
if P.gui:
P.gui.splash.show()
else:
Done = 'CANCEL'
return Done # ,device,dev_type,vid_pid
def open_kenwood_keying_port(P,sock):
if P.PRACTICE_MODE and False:
ser=serial_dummy()
elif sock.connection=='DIRECT':
ser = sock.s
else:
ser = serial.Serial(SERIAL_PORT5,4800,timeout=0.1)
ser.setDTR(False)
#toggle_dtr(5)
#sys.exit(0)
return ser
def open_yaesu_keying_port(P,sock):
if True:
if True:
if sock.rig_type2=='FT991a':
port,vid_pid=find_serial_device('FT991a',1,VERBOSITY=1)
ser = serial.Serial(port,BAUD,timeout=0.1,dsrdtr=0,rtscts=0)
ser.setDTR(True)
time.sleep(.02)
ser.setDTR(False)
ser.setRTS(False) # Digi mode uses this for PTT?
# Set DTR keying and full QSK
if sock.connection=='HAMLIB':
# This type of command doesn't work for most versions of hamlib ???
# Need to test again
cmd = 'W EX0603;EX0561; 0'
else:
cmd = 'BY;EX0603;EX0561;'
buf=sock.get_response(cmd)
ser.PORT = SERIAL_PORT4
ser.BAUD = BAUD
if False:
print('buf=',buf)
time.sleep(1)
print('DTR off')
ser.setDTR(False)
time.sleep(1)
print('DTR on')
ser.setDTR(True)
time.sleep(1)
print('DTR off')
ser.setDTR(False)
time.sleep(1)
sys.exit(0)
elif sock.rig_type2=='FTdx3000':
port,vid_pid=find_serial_device('FTdx3000',1,VERBOSITY=1)
ser = serial.Serial(port,BAUD,timeout=0.1)
ser.setDTR(False)
ser.PORT = SERIAL_PORT2
ser.BAUD = BAUD
return ser
def open_icom_keying_port(P,sock):
if True:
if P.PRACTICE_MODE:
ser=serial_dummy()
elif sock.rig_type2 in ['IC9700','IC7300']:
# If direct connect, could use USB A ...
#ser = P.sock.s
# but, in general, we'll use USB B in case we are using hamlib for rig control
#print('OPEN KEYING PORT:',SERIAL_PORT10,BAUD)
try:
print('OPEN ICOM KEYING PORT: ICOM Rig =',sock.rig_type2,'...')
port,vid_pid=find_serial_device(sock.rig_type2,1,VERBOSITY=1)
print('\tport=',port,'\tvid_pid=',vid_pid)
ser = serial.Serial(port,BAUD,timeout=0.1,dsrdtr=0,rtscts=0)
print('\tser=',ser)
# Set full QSK, etc.
# This only works on DIRECT I/O for now since we no longer
# inhereit that as out base class
sock.init_keyer()
# For some reason, we need to clear the DTR and RTS lines
time.sleep(.1)
ser.setDTR(False)
ser.setRTS(False)
time.sleep(1)
ser.setDTR(False)
ser.setRTS(False)
except:
error_trap('KEYING->OPEN KEYING PORT')
print('\n*****************************************')
print('* Unable to open keying port for rig',rig_num,' *')
print('*****************************************')
ser=None
sys.exit(0)
else:
# DTR keying does not work for the IC706
# Instead, we need to connect the TS850 interface to the keying line
# It looks like DTR keying is supported by the IC9700 - needs work
print('Opening keying port on IC706 ...')
ser = serial.Serial(SERIAL_PORT5,19200,timeout=0.1)
ser.setDTR(False)
print('... Opened keying port on IC706 ...')
if False:
toggle_dtr()
return ser
def open_direct_keying_port(P,sock):
if P.PRACTICE_MODE:
ser=serial_dummy()
elif sock.rig_type2=='TS850':
ser = open_kenwood_keying_port(P,sock)
elif sock.rig_type=='Icom' or \
(sock.rig_type in ['Hamlib','FLRIG'] and sock.rig_type2 in ['IC9700','IC7300']):
ser = open_icom_keying_port(P,sock)
elif sock.rig_type=='Yaesu' or \
(sock.rig_type in ['Hamlib','FLRIG'] and sock.rig_type2 in ['FTdx3000','FT991a']):
ser = open_yaesu_keying_port(P,sock)
elif sock.rig_type2 in ['None','TYT9000d','Hamlib','FLDIGI','KC505']:
ser=serial_dummy()
print('KEYING->OPEN DIRECT KEYING PORT: *** WARNING *** Using DUMMY serial port for keying - wont work!')
else:
print('KEYING->OPEN DIRECT KEYING PORT: Unknown rig type:',\
sock.rig_type,'\ttype1=',sock.rig_type1,'\ttype2=',sock.rig_type2)
sys.exit(0)
return ser