-
Notifications
You must be signed in to change notification settings - Fork 2
/
hardware_usbserial.py
124 lines (102 loc) · 4.21 KB
/
hardware_usbserial.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
# Please do not change this hardware control module for Quisk.
# It provides USB control of RS-HFIQ hardware.
# Updated to work with python3 by
# Ernesto Perez Estevez - HC6PE <ecualinux@gmail.com>
from __future__ import print_function
import struct, threading, time, traceback, math
from quisk_hardware_model import Hardware as BaseHardware
import _quisk as QS
import serial
DEBUG = 0
serialport = serial.Serial()
class Hardware(BaseHardware):
def __init__(self, app, conf):
BaseHardware.__init__(self, app, conf)
# Default port settings for RS-HFIQ
serialport.port = "/dev/ttyUSB0"
serialport.baudrate = 57600
serialport.bytesize = serial.EIGHTBITS #number of bits per bytes
serialport.parity = serial.PARITY_NONE #set parity check: no parity
serialport.stopbits = serial.STOPBITS_ONE #number of stop bits
serialport.timeout = 1 #non-block read
serialport.rtscts = False
try:
serialport.open()
except Exception as e:
print(e)
raise Exception
self.vfo = None
return None
def open(self): # Called once to open the Hardware
if serialport.isOpen():
#Wait for an initial response to verify we're connected
if DEBUG == 1:
print("Attempting handshake with RS-HFIQ using *W command...")
ver = ''
attemptCount = 0
while not ( "HFIQ" in ver ):
if DEBUG == 1:
print("\t...Attempt "+str(attemptCount+1)+".")
if attemptCount > 10:
print("Could not find the RS-HFIQ device. Perhaps wrong usb port ?\nTerminating...")
exit()
attemptCount += 1
time.sleep(.25)
serialport.flushInput()
serialport.flushOutput()
command='*W\r'
serialport.write(command.encode())
ver = serialport.readline().strip().decode()
if DEBUG == 1:
print("... Completed handshake with RS-HFIQ.")
serialport.flushInput()
serialport.flushOutput()
# Send init string
command='*OF2\r'
serialport.write(command.encode())
time.sleep(.25)#FIXME: Possibly some sort of race condition requiring this from time to time
serialport.flush()
return ver
else :
print("Failed to open the RS-HFIO serial port. Perhaps wrong usb port?\nTerminating...")
exit()
def close(self): # Called once to close the Hardware
if serialport.isOpen():
#Ensure we're not left keyed up and shut off the output level
cmdstr = '*x0\r*Of0\r'
serialport.write(cmdstr.encode())
time.sleep(.25)#FIXME: Possibly some sort of race condition requiring this from time to time
serialport.flush()
serialport.close()
return "Closed"
def ReturnFrequency(self):
if serialport.isOpen():
command='*F?\r'
serialport.write(command.encode())
self.current_freq = serialport.readline()
if DEBUG == 1:
print("Frequency:", self.current_freq)
return None, self.current_freq
def ChangeFrequency(self, tune, vfo, source='', band='', event=None):
if self.vfo != vfo :
if serialport.isOpen() :
self.vfo =vfo
vfo_string = '*F' + str(self.vfo) + '\r'
if DEBUG == 1:
print("Tuning to: ", vfo_string)
print("Tuning to: ", self.vfo)
serialport.write(vfo_string.encode())
return tune, self.vfo
def OnButtonPTT(self, event=None):
if event:
if event.GetEventObject().GetValue():
cmdstr='*x1\r'
if DEBUG: print('PTT pressed')
else:
if DEBUG: print('PTT released')
cmdstr='*x0\r'
if serialport.isOpen():
if DEBUG == 1:
print("Setting ptt, pttstring: ", cmdstr)
serialport.write(cmdstr.encode())
QS.set_key_down(event.GetEventObject().GetValue())