Differences
This shows you the differences between two versions of the page.
| public:radio:dormant:netradio_control [26/04/23 14:56 BST] – created john | public:radio:dormant:netradio_control [31/07/26 00:10 BST] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | < | ||
| - | |||
| - | |||
| - | ====== Netradio Control ====== | ||
| - | |||
| - | ** Python code for TCP/IP control of multiple radios ** | ||
| - | |||
| - | ===== Server ===== | ||
| - | |||
| - | This provides TCP/IP access to one or more radios, allowing control of Frequency, Mode, Pre-amp and Attenuator. It also gives remote access to S-meter readings. | ||
| - | |||
| - | There are separate rig-dependant command modules that then provide a standard interface for commands sent by a client, regardless of the particular radio connected. | ||
| - | |||
| - | There are modules for | ||
| - | |||
| - | * Icom C-IV radios such as IC-R75, IC-718, IC-7200 etc. | ||
| - | * AOR AR7030 | ||
| - | * Icom IC-M710 marine | ||
| - | |||
| - | ==== Server ==== | ||
| - | |||
| - | ++++ server.py | | ||
| - | |||
| - | <file python server.py> | ||
| - | from aor import * | ||
| - | from icom import * | ||
| - | from conf import * | ||
| - | from m710 import * | ||
| - | import SocketServer | ||
| - | import time | ||
| - | |||
| - | try: | ||
| - | import readline | ||
| - | except: | ||
| - | pass | ||
| - | |||
| - | lock = threading.Lock() | ||
| - | |||
| - | radios = [] | ||
| - | |||
| - | |||
| - | |||
| - | r1 = Icom(n1, a1, cal1) | ||
| - | radios.append(n1) | ||
| - | |||
| - | r2 = m710(n4) | ||
| - | radios.append(n4) | ||
| - | |||
| - | #r2 = Icom(n2, a2, cal2) | ||
| - | # | ||
| - | |||
| - | r3 = Ar7030(n3) | ||
| - | radios.append(n3) | ||
| - | |||
| - | print radios | ||
| - | #print r1.digi_off() | ||
| - | |||
| - | #print r2.remote_on() | ||
| - | |||
| - | def count_radios(): | ||
| - | count = len(radios) | ||
| - | return count | ||
| - | |||
| - | |||
| - | def list_radios(): | ||
| - | radiolist = "" | ||
| - | for n in range(0, len(radios)): | ||
| - | r = radios[n] | ||
| - | radiolist += (r + " ") | ||
| - | return radiolist | ||
| - | |||
| - | |||
| - | def write_file(text): | ||
| - | filename = ' | ||
| - | f = open(filename, | ||
| - | timenow = time.strftime(" | ||
| - | log = " " | ||
| - | f.write(log) | ||
| - | f.close() | ||
| - | |||
| - | |||
| - | def write_con(text): | ||
| - | filename = ' | ||
| - | f = open(filename, | ||
| - | timenow = time.strftime(" | ||
| - | log = " " | ||
| - | f.write(log) | ||
| - | f.close() | ||
| - | |||
| - | |||
| - | # The Server | ||
| - | class ThreadedRequestHandler(SocketServer.StreamRequestHandler): | ||
| - | def handle(self): | ||
| - | # we find the current thread for the client connection just set up, to | ||
| - | # use in the log file | ||
| - | cur_thread = threading.currentThread() | ||
| - | # log the new connection details | ||
| - | write_con(" | ||
| - | # print to the server' | ||
| - | print self.client_address | ||
| - | # loop to handle client requests.... | ||
| - | while True: | ||
| - | # using StreamRequestHandler means our input from the client | ||
| - | # is " | ||
| - | # we read a line at a time, using readline() | ||
| - | cmd = self.rfile.readline().lower() | ||
| - | # to keep things clean, we remove any characters that aren't | ||
| - | # " | ||
| - | # these are between 32 and 127 in the ASCII table | ||
| - | # we look at each character, and then make a new word by | ||
| - | # .join()ing each accepted character with no space in between | ||
| - | asccmd = "" | ||
| - | # we make a list called " | ||
| - | # will be inspected by various functions | ||
| - | words = asccmd.split() | ||
| - | # If a client uses sock.close() itself, to disconnect, it appears that | ||
| - | # we read a continuous stream of "" | ||
| - | # socket, which puts CPU to 100%. | ||
| - | # | ||
| - | # The " | ||
| - | # way to keep the connection up for multiple commands. | ||
| - | # | ||
| - | # Further connection are accepted due to the Threaded nature of the server. | ||
| - | # The CPU load is unacceptable though | ||
| - | # HACK ?>>>>> | ||
| - | # Looking for "" | ||
| - | # the connection from the server end (even though the client has | ||
| - | # gone) cures this. | ||
| - | if cmd == "": | ||
| - | break | ||
| - | else: | ||
| - | pass | ||
| - | # if the words list is empty, go back and get more input | ||
| - | if not words: | ||
| - | continue | ||
| - | # we have input.... | ||
| - | # filter based on the first word - these are the | ||
| - | # pre-set commands the server will accept | ||
| - | # the client wants to know the currently available | ||
| - | # radio names - held in the variable " | ||
| - | elif words[0] == " | ||
| - | self.wfile.write(rnames) | ||
| - | # words[-1] (the last word in the list) will always be the | ||
| - | # radio name. We give the variable " | ||
| - | # identifying which radio object to apply the method to | ||
| - | elif words[0] == " | ||
| - | count = count_radios() | ||
| - | self.wfile.write(count) | ||
| - | elif words[0] == " | ||
| - | ident_text = " | ||
| - | radio_list = list_radios() | ||
| - | self.wfile.write(ident_text + " | ||
| - | |||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | mode = my_radio.get_mode() | ||
| - | self.wfile.write(mode) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | freq = words[1] | ||
| - | freq = my_radio.get_freq() | ||
| - | self.wfile.write(freq) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | mode = words[1] | ||
| - | newmode = my_radio.set_mode(mode) | ||
| - | self.wfile.write(newmode) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | try: | ||
| - | freq = float(words[1]) | ||
| - | newfreq = my_radio.set_freq(freq) | ||
| - | self.wfile.write(newfreq) | ||
| - | except ValueError: | ||
| - | #freq = float(my_radio.get_freq()) | ||
| - | self.wfile.write(" | ||
| - | | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | smeter = round(float(my_radio.get_smeter()), | ||
| - | self.wfile.write(smeter) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | s = my_radio.get_s() | ||
| - | self.wfile.write(s) | ||
| - | elif words[0] == " | ||
| - | radios = list_radios() | ||
| - | self.wfile.write(radios) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | preamp = my_radio.get_pre() | ||
| - | self.wfile.write(preamp) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | preamp = my_radio.pre_on() | ||
| - | self.wfile.write(preamp) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | preamp = my_radio.pre_2_on() | ||
| - | self.wfile.write(preamp) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | preamp = my_radio.pre_off() | ||
| - | self.wfile.write(preamp) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | att = my_radio.get_att() | ||
| - | self.wfile.write(att) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | att = my_radio.att_on() | ||
| - | self.wfile.write(att) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | att = my_radio.att_off() | ||
| - | self.wfile.write(att) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | tune = my_radio.tune() | ||
| - | self.wfile.write(tune) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | pwr = my_radio.get_pwr() | ||
| - | self.wfile.write(pwr) | ||
| - | elif words[0] == " | ||
| - | my_radio = eval(words[-1]) | ||
| - | spwr = words[1] | ||
| - | pwr = my_radio.set_pwr(spwr) | ||
| - | self.wfile.write(pwr) | ||
| - | elif words[0] == " | ||
| - | write_con(" | ||
| - | self.wfile.write(" | ||
| - | break | ||
| - | else: # nothing in words[0] matches a pre-set command.... | ||
| - | write_file(" | ||
| - | self.wfile.write(" | ||
| - | |||
| - | |||
| - | class ThreadedIcomServer(SocketServer.ThreadingMixIn, | ||
| - | pass | ||
| - | |||
| - | |||
| - | if __name__ == ' | ||
| - | # define the lock to be used on the serial port access | ||
| - | lock = threading.Lock() | ||
| - | |||
| - | # address ('' | ||
| - | address = ('', | ||
| - | server = ThreadedIcomServer(address, | ||
| - | server.allow_reuse_address = True | ||
| - | |||
| - | # define that the server will be threaded, and will serve " | ||
| - | t = threading.Thread(target=server.serve_forever) | ||
| - | # start the server thread | ||
| - | t.start() | ||
| - | |||
| - | write_con( | ||
| - | " | ||
| - | |||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | ==== Configurtion Module ==== | ||
| - | |||
| - | ++++ conf.py | | ||
| - | |||
| - | <file python cpnf.py> | ||
| - | # header | ||
| - | preamble = " | ||
| - | controller = " | ||
| - | |||
| - | # commands/ | ||
| - | set_freq_cmd = " | ||
| - | set_mode_cmd = " | ||
| - | get_freq_cmd = " | ||
| - | get_mode_cmd = " | ||
| - | get_smeter_cmd = " | ||
| - | get_swr_cmd = " | ||
| - | digi_off_cmd = " | ||
| - | |||
| - | set_pre_cmd = " | ||
| - | |||
| - | set_pre_off = " | ||
| - | set_pre_on = " | ||
| - | set_pre_2_on = " | ||
| - | |||
| - | set_att_cmd = " | ||
| - | set_att_on = " | ||
| - | set_att_off = " | ||
| - | |||
| - | ptt_on_cmd = " | ||
| - | ptt_off_cmd = " | ||
| - | |||
| - | pwr_cmd = " | ||
| - | |||
| - | # end of message | ||
| - | eom = " | ||
| - | |||
| - | # controller responses | ||
| - | ack = " | ||
| - | nak = " | ||
| - | |||
| - | a1 = " | ||
| - | n1 = " | ||
| - | cal1 = ( 25, 1, 36, 47, 31, 18, 34, 35 ) | ||
| - | |||
| - | a2 = " | ||
| - | # a2 = " | ||
| - | n2 = " | ||
| - | cal2 = ( 27, 28, 58, 10, 14, 14, 35, 42 ) | ||
| - | |||
| - | n3 = " | ||
| - | |||
| - | n4 = " | ||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | |||
| - | ==== Icom C-IV module ==== | ||
| - | |||
| - | ++++ icom.py | | ||
| - | |||
| - | <file python icom.py> | ||
| - | import serial | ||
| - | import threading | ||
| - | from conf import * | ||
| - | import time | ||
| - | #sport = " | ||
| - | sport = "/ | ||
| - | sbaud = 9600 | ||
| - | |||
| - | lock = threading.Lock() | ||
| - | |||
| - | |||
| - | class Icom(object): | ||
| - | def __init__(self, | ||
| - | self.ser = serial.Serial(sport, | ||
| - | self.model = model | ||
| - | self.radio_address = radio_address | ||
| - | self.cal = cal | ||
| - | | ||
| - | def digi_off(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + digi_off_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | def get_pre(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_pre_cmd + eom | ||
| - | |||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | if result[6] == " | ||
| - | return 0 | ||
| - | elif result[6] == " | ||
| - | return 1 | ||
| - | elif result[6] == " | ||
| - | return 2 | ||
| - | |||
| - | def get_pwr(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + pwr_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | p1 = ord(result[7]) / 16 | ||
| - | p2 = ord(result[7]) % 16 | ||
| - | p3 = ord(result[6]) / 16 | ||
| - | p4 = ord(result[6]) % 16 | ||
| - | pwr = float(100 * (10 * p3 + p4) + (10 * p1 + p2)) | ||
| - | return int(pwr*100/ | ||
| - | |||
| - | def set_pwr(self, | ||
| - | #if pwr == " | ||
| - | # spwr = " | ||
| - | #elif pwr == " | ||
| - | # spwr = " | ||
| - | #elif pwr == " | ||
| - | # spwr = " | ||
| - | #elif pwr == " | ||
| - | # spwr = " | ||
| - | rigpwr = int(pwr) * 255 / 100 | ||
| - | print " | ||
| - | pwr1 = rigpwr / 100 | ||
| - | pwr2 = rigpwr % 100 | ||
| - | spwr1 = (pwr1 / 10 * 16) | ||
| - | spwr2 = (pwr1 % 10) | ||
| - | spwr3 = (pwr2 / 10 * 16) | ||
| - | spwr4 = (pwr2 % 10) | ||
| - | spwr = chr(spwr1+spwr2) + chr(spwr3+spwr4) | ||
| - | #print "spwr ", spwr | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + pwr_cmd + spwr + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return self.get_pwr() | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | def pre_on(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_pre_cmd + set_pre_on + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | | ||
| - | def pre_2_on(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_pre_cmd + set_pre_2_on + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | |||
| - | def pre_off(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_pre_cmd + set_pre_off + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | def ptt_on(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + ptt_on_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | #print result[5] | ||
| - | if not result[4] == ack: | ||
| - | return "ptt on" | ||
| - | elif result[4] == nak: | ||
| - | return " | ||
| - | | ||
| - | def ptt_off(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + ptt_off_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | #print result[5] | ||
| - | if not result[4] == ack: | ||
| - | return "ptt off" | ||
| - | elif result[4] == nak: | ||
| - | return " | ||
| - | |||
| - | def get_att(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_att_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | if result[5] == " | ||
| - | return 0 | ||
| - | elif result[5] == " | ||
| - | return 1 | ||
| - | |||
| - | def att_on(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_att_cmd + set_att_on + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | def att_off(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_att_cmd + set_att_off + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return " | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received" | ||
| - | |||
| - | def set_freq(self, | ||
| - | fdig = " | ||
| - | bcd = () | ||
| - | for i in (8, 6, 4, 2, 0): | ||
| - | bcd += self.freq_bcd(int(fdig[i]), | ||
| - | set_freq_val = "" | ||
| - | for byte in bcd: | ||
| - | set_freq_val += chr(byte) | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_freq_cmd + set_freq_val + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return "Set Freq success" | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received / Freq not supported" | ||
| - | |||
| - | def get_freq(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + get_freq_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | if len(result) > 0: | ||
| - | f = 0 | ||
| - | for k in [18, 19, 16, 17, 14, 15, 12, 13, 10, 11]: | ||
| - | f = 10 * f + self.nib(result, | ||
| - | self.freq = (float(f) / 1000) | ||
| - | return " | ||
| - | |||
| - | def set_mode(self, | ||
| - | print "in set_mode() with ", mode | ||
| - | if mode == " | ||
| - | mode = " | ||
| - | |||
| - | if mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | else: | ||
| - | return "Mode not recognized" | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + set_mode_cmd + set_mode_val + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result[4] == ack: | ||
| - | return "Set Mode Success" | ||
| - | elif result[4] == nak: | ||
| - | return "NAK received / Mode not supported" | ||
| - | |||
| - | def get_mode(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + get_mode_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | mode = "" | ||
| - | if result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | elif result[5] == " | ||
| - | mode = " | ||
| - | |||
| - | if mode == " | ||
| - | mode = " | ||
| - | |||
| - | self.mode = mode | ||
| - | return self.mode.upper() | ||
| - | |||
| - | def get_s(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + get_smeter_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | sm1 = ord(result[7]) / 16 | ||
| - | sm2 = ord(result[7]) % 16 | ||
| - | sm3 = ord(result[6]) / 16 | ||
| - | sm4 = ord(result[6]) % 16 | ||
| - | s = float(100 * (10 * sm3 + sm4) + (10 * sm1 + sm2)) | ||
| - | return s | ||
| - | | ||
| - | def get_swr(self): | ||
| - | sendStr = preamble + preamble + self.radio_address + controller + get_swr_cmd + eom | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if not result: | ||
| - | return " | ||
| - | sm1 = ord(result[7]) / 16 | ||
| - | sm2 = ord(result[7]) % 16 | ||
| - | sm3 = ord(result[6]) / 16 | ||
| - | sm4 = ord(result[6]) % 16 | ||
| - | swr = float(100 * (10 * sm3 + sm4) + (10 * sm1 + sm2)) | ||
| - | return swr | ||
| - | |||
| - | def get_smeter(self): | ||
| - | s = float(self.get_s()) | ||
| - | cal = self.cal | ||
| - | s1 = s - cal[0] | ||
| - | s2 = s1 - cal[1] | ||
| - | s3 = s2 - cal[2] | ||
| - | s4 = s3 - cal[3] | ||
| - | s5 = s4 - cal[4] | ||
| - | s6 = s5 - cal[5] | ||
| - | s7 = s6 - cal[6] | ||
| - | if s1 <= 0: | ||
| - | dbm = -123 | ||
| - | adj = s / cal[0] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s2 <= 0: | ||
| - | dbm = -113 | ||
| - | adj = s1 / cal[1] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s3 <= 0: | ||
| - | dbm = -103 | ||
| - | adj = s2 / cal[2] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s4 <= 0: | ||
| - | dbm = -93 | ||
| - | adj = s3 / cal[3] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s5 <= 0: | ||
| - | dbm = -83 | ||
| - | adj = s4 / cal[4] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s6 <= 0: | ||
| - | dbm = -73 | ||
| - | adj = s5 / cal[5] * 10 | ||
| - | return str(dbm + adj) | ||
| - | elif s7 <= 0: | ||
| - | dbm = -63 | ||
| - | adj = s6 / cal[6] * 20 | ||
| - | return str(dbm + adj) | ||
| - | else: | ||
| - | dbm = -43 | ||
| - | adj = s7 / cal[7] * 20 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | def get_name(self): | ||
| - | return self.model | ||
| - | |||
| - | def tune(self): | ||
| - | print " | ||
| - | curmode = self.get_mode().lower() | ||
| - | #print " | ||
| - | |||
| - | curpwr = self.get_pwr() | ||
| - | if curpwr < 98: | ||
| - | curpwr = curpwr + 1 | ||
| - | |||
| - | #print " | ||
| - | | ||
| - | #print " | ||
| - | self.set_mode(" | ||
| - | self.set_pwr(25) | ||
| - | #print " | ||
| - | #print "PTT On" | ||
| - | self.ptt_on() | ||
| - | time.sleep(2) | ||
| - | swr = self.get_swr() | ||
| - | #print "SWR :", swr | ||
| - | time.sleep(1) | ||
| - | self.ptt_off() | ||
| - | #print "PTT Off" | ||
| - | self.set_mode(curmode) | ||
| - | #print "Mode reset ", | ||
| - | self.set_pwr(curpwr) | ||
| - | print "Tuned : (ref pwr : %s)" % swr | ||
| - | return "Tuned : (ref pwr : %s)" % swr | ||
| - | | ||
| - | def tx_rx(self, sendStr): | ||
| - | lock.acquire() | ||
| - | self.ser.write(sendStr) | ||
| - | echo = self.ser.read(len(sendStr)) | ||
| - | if len(echo) != len(sendStr): | ||
| - | return " | ||
| - | byte = " | ||
| - | result = "" | ||
| - | count = 0 | ||
| - | while byte != eom: | ||
| - | byte = self.ser.read() | ||
| - | #print " | ||
| - | result += byte | ||
| - | count += 1 | ||
| - | if count > 10: | ||
| - | break | ||
| - | lock.release() | ||
| - | #print "" | ||
| - | return result | ||
| - | |||
| - | |||
| - | def nib(self, s, i): | ||
| - | k = ord(s[i / 2]) | ||
| - | if i % 2 == 0: | ||
| - | k = k >> 4 | ||
| - | return k & 0xf | ||
| - | |||
| - | |||
| - | def freq_bcd(self, | ||
| - | return (16 * d1 + d2), | ||
| - | |||
| - | |||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | ==== AOR AR7030 module ==== | ||
| - | |||
| - | ++++ aor.py | | ||
| - | |||
| - | < | ||
| - | import serial | ||
| - | import threading | ||
| - | |||
| - | |||
| - | #sport = "/ | ||
| - | sport = "/ | ||
| - | sbaud = 1200 | ||
| - | |||
| - | lock = threading.Lock() | ||
| - | fract = float(2 ** 24) / 44545 | ||
| - | |||
| - | |||
| - | class Ar7030(object): | ||
| - | def __init__(self, | ||
| - | self.ser = serial.Serial(sport, | ||
| - | self.model = model | ||
| - | |||
| - | |||
| - | def check_bit(self, | ||
| - | mask = 1 << bit | ||
| - | result = (ord(byte) & mask) | ||
| - | if result: | ||
| - | return 1 | ||
| - | else: | ||
| - | return 0 | ||
| - | |||
| - | def set_bit(self, | ||
| - | mask = 1 << bit | ||
| - | result = ord(byte) | mask | ||
| - | return result | ||
| - | |||
| - | def clear_bit(self, | ||
| - | mask = ~(1 << bit) | ||
| - | result = (ord(byte) & mask) | ||
| - | return result | ||
| - | |||
| - | def get_ident(self): | ||
| - | sendStr = ' | ||
| - | ident = self.tx_rx(sendStr, | ||
| - | return ident | ||
| - | |||
| - | def get_pre(self): | ||
| - | sendStr = ' | ||
| - | byte = self.tx_rx(sendStr, | ||
| - | p = self.check_bit(byte, | ||
| - | if p: | ||
| - | return 1 | ||
| - | else: | ||
| - | return 0 | ||
| - | return | ||
| - | |||
| - | |||
| - | def pre_on(self): | ||
| - | #get current 8-bit rxcon byte : | ||
| - | # bit0 = filter FS3 | ||
| - | # bit1 = filter FS2 | ||
| - | # bit2 = filter FS1 | ||
| - | # bit3 = filter FS4 | ||
| - | # bit4 = preamp enabled | ||
| - | # bit5 = atten 0 = 20dB / 1 = 40dB | ||
| - | # bit6 = input filter 0 = HF / 1 = LF | ||
| - | # bit7 = attenuator enabled | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | byte = self.tx_rx(sendStr, | ||
| - | |||
| - | # set bit 4 ON = preamp ON and get the new 8-bit rxcon byte | ||
| - | pon = self.set_bit(byte, | ||
| - | |||
| - | # split new rxcon byte into two 4-bit nibbles, add 48/96 (\x30 and \x60) | ||
| - | high = 48 + (pon >> 4) | ||
| - | low = 96 + (pon & 15) | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | |||
| - | return " | ||
| - | |||
| - | |||
| - | def pre_off(self): | ||
| - | #get current 8-bit rxcon byte | ||
| - | sendStr = ' | ||
| - | byte = self.tx_rx(sendStr, | ||
| - | |||
| - | # set bit 4 OFF = preamp OFF and get the new 8-bit rxcon byte | ||
| - | pon = self.clear_bit(byte, | ||
| - | |||
| - | |||
| - | # split new rxcon byte into two 4-bit nibbles, add 48/96 (\x30 and \x60) | ||
| - | high = 48 + (pon >> 4) | ||
| - | low = 96 + (pon & 15) | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | return " | ||
| - | |||
| - | |||
| - | def get_att(self): | ||
| - | sendStr = ' | ||
| - | |||
| - | byte = self.tx_rx(sendStr, | ||
| - | |||
| - | a = self.check_bit(byte, | ||
| - | |||
| - | if a: | ||
| - | return 1 | ||
| - | else: | ||
| - | return 0 | ||
| - | |||
| - | |||
| - | def att_on(self): | ||
| - | #get current 8-bit rxcon byte | ||
| - | sendStr = ' | ||
| - | byte = self.tx_rx(sendStr, | ||
| - | |||
| - | # set bit 7 ON = ATT ON and get the new 8-bit rxcon byte | ||
| - | pon = self.set_bit(byte, | ||
| - | |||
| - | |||
| - | # split new rxcon byte into two 4-bit nibbles, add 48/96 (\x30 and \x60) | ||
| - | high = 48 + (pon >> 4) | ||
| - | low = 96 + (pon & 15) | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | return " | ||
| - | |||
| - | |||
| - | def att_off(self): | ||
| - | #get current 8-bit rxcon byte | ||
| - | sendStr = ' | ||
| - | byte = self.tx_rx(sendStr, | ||
| - | |||
| - | # set bit 7 OFF = att OFF and get the new 8-bit rxcon byte | ||
| - | pon = self.clear_bit(byte, | ||
| - | |||
| - | |||
| - | # split new rxcon byte into two 4-bit nibbles, add 48/96 (\x30 and \x60) | ||
| - | high = 48 + (pon >> 4) | ||
| - | low = 96 + (pon & 15) | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | return " | ||
| - | |||
| - | |||
| - | def set_freq(self, | ||
| - | fval = freq * fract | ||
| - | #print fval | ||
| - | b1 = 48 + int(fval / 1048576) | ||
| - | fval = fval % 1048576 | ||
| - | b2 = 96 + int(fval / 65536) | ||
| - | fval = fval % 65536 | ||
| - | b3 = 48 + int(fval / 4096) | ||
| - | fval = fval % 4096 | ||
| - | b4 = 96 + int(fval / 256) | ||
| - | fval = fval % 256 | ||
| - | b5 = 48 + int(fval / 16) | ||
| - | b6 = 96 + int(fval % 16) | ||
| - | |||
| - | f_tuple = ( b1, b2, b3, b4, b5, b6 ) | ||
| - | |||
| - | freqStr = "" | ||
| - | for byte in f_tuple: | ||
| - | freqStr += chr(byte) | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | return "Freq Set" | ||
| - | |||
| - | def get_freq(self): | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | freqStr = self.tx_rx(sendStr, | ||
| - | |||
| - | f_val = 0 | ||
| - | for k in freqStr: | ||
| - | f_val = f_val * 256 + ord(k) | ||
| - | |||
| - | return " | ||
| - | | ||
| - | |||
| - | |||
| - | def set_mode(self, | ||
| - | |||
| - | if mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | elif mode == " | ||
| - | set_mode_val = " | ||
| - | else: | ||
| - | return "Mode not recognized" | ||
| - | |||
| - | sendStr = ' | ||
| - | |||
| - | self.tx_rx(sendStr, | ||
| - | |||
| - | return "Mode sent" | ||
| - | |||
| - | def get_mode(self): | ||
| - | |||
| - | sendStr = " | ||
| - | m = self.tx_rx(sendStr, | ||
| - | |||
| - | mode = "" | ||
| - | if m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | elif m == " | ||
| - | mode = " | ||
| - | |||
| - | return mode.upper() | ||
| - | |||
| - | |||
| - | def get_s(self): | ||
| - | sendStr = ' | ||
| - | sraw = self.tx_rx(sendStr, | ||
| - | |||
| - | return ord(sraw) | ||
| - | |||
| - | def get_cal(self): | ||
| - | |||
| - | sendStr = ' | ||
| - | #print " | ||
| - | cbytes = self.tx_rx(sendStr, | ||
| - | |||
| - | cal = [] | ||
| - | |||
| - | for c in cbytes: | ||
| - | cal.append(ord(c)) | ||
| - | |||
| - | return cal | ||
| - | |||
| - | def get_smeter(self): | ||
| - | s = float(self.get_s()) | ||
| - | cal = self.get_cal() | ||
| - | |||
| - | s1 = s - cal[0] | ||
| - | s2 = s1 - cal[1] | ||
| - | s3 = s2 - cal[2] | ||
| - | s4 = s3 - cal[3] | ||
| - | s5 = s4 - cal[4] | ||
| - | s6 = s5 - cal[5] | ||
| - | s7 = s6 - cal[6] | ||
| - | |||
| - | if s1 <= 0: | ||
| - | dbm = -123 | ||
| - | adj = s / cal[0] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s2 <= 0: | ||
| - | dbm = -113 | ||
| - | adj = s1 / cal[1] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s3 <= 0: | ||
| - | dbm = -103 | ||
| - | adj = s2 / cal[2] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s4 <= 0: | ||
| - | dbm = -93 | ||
| - | adj = s3 / cal[3] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s5 <= 0: | ||
| - | dbm = -83 | ||
| - | adj = s4 / cal[4] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s6 <= 0: | ||
| - | dbm = -73 | ||
| - | adj = s5 / cal[5] * 10 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | elif s7 <= 0: | ||
| - | dbm = -63 | ||
| - | adj = s6 / cal[6] * 20 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | else: | ||
| - | dbm = -43 | ||
| - | adj = s7 / cal[7] * 20 | ||
| - | return str(dbm + adj) | ||
| - | |||
| - | |||
| - | def tx_rx(self, sendStr, reply, n): | ||
| - | # apply thread lock | ||
| - | lock.acquire() | ||
| - | self.ser.write(sendStr) | ||
| - | |||
| - | if reply: | ||
| - | result = "" | ||
| - | while not result: | ||
| - | result = self.ser.read(1) | ||
| - | lock.release() | ||
| - | return result | ||
| - | else: | ||
| - | |||
| - | result = "" | ||
| - | byte = "" | ||
| - | while n != 0: | ||
| - | self.ser.write(' | ||
| - | #byte = "" | ||
| - | |||
| - | while not byte: | ||
| - | byte = self.ser.read(1) | ||
| - | |||
| - | result += byte | ||
| - | |||
| - | byte = "" | ||
| - | n -= 1 | ||
| - | lock.release() | ||
| - | return result | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | </ | ||
| - | |||
| - | |||
| - | ++++ | ||
| - | |||
| - | ==== Icom IC-M710 module ==== | ||
| - | |||
| - | ++++ m710.py | | ||
| - | |||
| - | < | ||
| - | ''' | ||
| - | Python NMEA Radio Functions module for Icom IC-M710 Marine HF SSB Transceiver | ||
| - | |||
| - | Copyright (C) 2015 John Pumford-Green | ||
| - | |||
| - | 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. | ||
| - | GNU General Public License for more details. | ||
| - | |||
| - | You should have received a copy of the GNU General Public License | ||
| - | along with this program. | ||
| - | ''' | ||
| - | |||
| - | # serial port for Icom IC-M710 Rig Control | ||
| - | |||
| - | import serial | ||
| - | import threading | ||
| - | import time | ||
| - | version = " | ||
| - | |||
| - | sport = '/ | ||
| - | sbaud = 4800 | ||
| - | |||
| - | #ser = serial.Serial(sport, | ||
| - | lock = threading.Lock() | ||
| - | |||
| - | # NMEA Codes | ||
| - | preamble = " | ||
| - | controller = " | ||
| - | radio = " | ||
| - | cr = " | ||
| - | lf = " | ||
| - | |||
| - | |||
| - | # Commands are sent as NMEA private sentences: | ||
| - | # | ||
| - | # $PICOA, | ||
| - | # | ||
| - | # where HH is the 2 digit ECC value below: | ||
| - | # | ||
| - | # The protocol document states that for messages FROM the controller TO the radio | ||
| - | # the ECC bytes are optional, and may be omitted. This appears to be false information, | ||
| - | # the ECC bytes seem to be necessary. | ||
| - | # | ||
| - | # The ECC checksum, is a two-digit hex value found by XORing the hex values of the characters | ||
| - | # between " | ||
| - | # | ||
| - | # The first part of the message is always: | ||
| - | # " | ||
| - | # we then XOR this with each character' | ||
| - | # and convert the result to a 2-digit hex value | ||
| - | # | ||
| - | |||
| - | class m710(object): | ||
| - | def __init__(self, | ||
| - | self.ser = serial.Serial(sport, | ||
| - | self.model = model | ||
| - | |||
| - | |||
| - | def get_ecc(self, | ||
| - | ecc = 112 | ||
| - | for c in command: | ||
| - | ecc = ord(c) ^ ecc | ||
| - | hecc = ' | ||
| - | | ||
| - | return hecc | ||
| - | |||
| - | # we must force the radio into " | ||
| - | def remote_on(self): | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | return | ||
| - | else: | ||
| - | return self.remote_on() | ||
| - | | ||
| - | | ||
| - | |||
| - | # we can leave the radio in " | ||
| - | # but we must close the " | ||
| - | # restored after Remote mode is closed. | ||
| - | def remote_off(self): | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | # | ||
| - | return | ||
| - | else: | ||
| - | return self.remote_off() | ||
| - | | ||
| - | |||
| - | def ptt_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return | ||
| - | else: | ||
| - | return ptt_on() | ||
| - | |||
| - | def ptt_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return | ||
| - | else: | ||
| - | return ptt_off() | ||
| - | |||
| - | |||
| - | |||
| - | def get_freq(self): | ||
| - | fkhz = self.get_rxfreq() | ||
| - | #fkhz = " | ||
| - | return fkhz | ||
| - | |||
| - | def set_freq(self, | ||
| - | | ||
| - | self.set_rxfreq(freq) | ||
| - | self.set_txfreq(freq) | ||
| - | return "Set freq success" | ||
| - | | ||
| - | def get_rxfreq(self): | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | f = list[4].split(" | ||
| - | fkhz = " | ||
| - | return fkhz | ||
| - | else: | ||
| - | return self.get_rxfreq() | ||
| - | | ||
| - | | ||
| - | def get_txfreq(self): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | f = list[4].split(" | ||
| - | fkhz = " | ||
| - | return fkhz | ||
| - | else: | ||
| - | return self.get_txfreq() | ||
| - | | ||
| - | |||
| - | def get_mode(self): | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | mode = list[4].split(" | ||
| - | if mode == " | ||
| - | mode = " | ||
| - | return mode | ||
| - | else: | ||
| - | return self.get_mode() | ||
| - | | ||
| - | |||
| - | |||
| - | def set_mode(self, | ||
| - | mode = mode.upper() | ||
| - | if mode == " | ||
| - | mode = " | ||
| - | |||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | | ||
| - | sendStr = preamble +controller+"," | ||
| - | #print "in set_mode with ", sendStr | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return self.set_mode(mode) | ||
| - | |||
| - | | ||
| - | def set_rxfreq(self, | ||
| - | | ||
| - | fmhz = float(freq) / 1000 | ||
| - | f = str(fmhz) | ||
| - | | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return self.set_rxfreq(freq) | ||
| - | | ||
| - | | ||
| - | def set_txfreq(self, | ||
| - | | ||
| - | fmhz = float(freq) / 1000 | ||
| - | f = str(fmhz) | ||
| - | | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return self.set_txfreq(freq) | ||
| - | |||
| - | def get_txpower(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | power = list[4].split(" | ||
| - | return power | ||
| - | else: | ||
| - | return get_txpower() | ||
| - | |||
| - | def set_txpower(p): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return set_txpower(p) | ||
| - | |||
| - | def get_smeter(self): | ||
| - | command = " | ||
| - | ecc = self.get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = self.tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | smeter = (float(list[4].split(" | ||
| - | return smeter | ||
| - | else: | ||
| - | return self.get_smeter() | ||
| - | |||
| - | def speaker_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return speaker_on() | ||
| - | | ||
| - | | ||
| - | def speaker_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return speaker_off() | ||
| - | |||
| - | def sql_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return sql_on() | ||
| - | |||
| - | def sql_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return sql_off() | ||
| - | |||
| - | def nb_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return nb_on() | ||
| - | |||
| - | def nb_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return nb_off() | ||
| - | | ||
| - | def dim_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return dim_on() | ||
| - | |||
| - | def dim_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return dim_off() | ||
| - | | ||
| - | def agc_on(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return agc_on() | ||
| - | |||
| - | def agc_off(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return agc_off() | ||
| - | |||
| - | def get_vol(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | vol = list[4].split(" | ||
| - | return vol | ||
| - | else: | ||
| - | return get_vol() | ||
| - | | ||
| - | | ||
| - | |||
| - | def set_vol(v): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return set_vol(v) | ||
| - | |||
| - | def get_rfg(): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble+controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | list = result.split("," | ||
| - | rf = list[4].split(" | ||
| - | return rf | ||
| - | else: | ||
| - | return get_rfg() | ||
| - | | ||
| - | def get_att(self): | ||
| - | return | ||
| - | def att_on(self): | ||
| - | return | ||
| - | def att_off(self): | ||
| - | return | ||
| - | | ||
| - | def pre_on(self): | ||
| - | return | ||
| - | def pre_off(self): | ||
| - | return | ||
| - | |||
| - | def get_pre(self): | ||
| - | return | ||
| - | |||
| - | def set_rfg(v): | ||
| - | command = " | ||
| - | ecc = get_ecc(command) | ||
| - | sendStr = preamble +controller+"," | ||
| - | result = tx_rx(sendStr) | ||
| - | if result: | ||
| - | return result | ||
| - | else: | ||
| - | return set_rfg(v) | ||
| - | |||
| - | # The message FROM the radio may be corrupted so we do a check on the received ECC versus our calculated cECC | ||
| - | # from the received characters - which are everything AFTER the " | ||
| - | # | ||
| - | # starting the ECC calculation at message[1] ignores the " | ||
| - | # character' | ||
| - | # into a two-digit hex value. This is then compared to the received (rECC) value from the radio' | ||
| - | # If they match the message is acceptable and check_ecc() returns True to tx_rx(), | ||
| - | # otherwise check_ecc() function returns " | ||
| - | # the True/False value is tested in tx_rx(). | ||
| - | # If True then tx_rx() returns the incoming string to the calling function | ||
| - | # otherwise it returns " | ||
| - | # The calling function then checkS the boolean state of the returned value. | ||
| - | # If True (ie it has the radio' | ||
| - | # is sent back to the client. | ||
| - | # If False the function calls itself again, and attempts to get an error-free reply from the radio, via tx_rx() | ||
| - | |||
| - | def check_ecc(self, | ||
| - | i = 1 | ||
| - | cecc = 0 | ||
| - | | ||
| - | while i < len(message): | ||
| - | cecc = ord(message[i]) ^ cecc | ||
| - | i += 1 | ||
| - | | ||
| - | cecc = ' | ||
| - | | ||
| - | if cecc == recc: | ||
| - | return True | ||
| - | else: | ||
| - | return False | ||
| - | | ||
| - | def tx_rx(self, | ||
| - | #print "in tx_rx, sendStr = " , sendStr | ||
| - | lock.acquire() | ||
| - | | ||
| - | self.ser.write(sendStr) | ||
| - | time.sleep(0.1) | ||
| - | result = self.ser.readline() | ||
| - | |||
| - | #print result | ||
| - | reply = result.split(" | ||
| - | ecc = reply[1][0: | ||
| - | message = reply[0] | ||
| - | lock.release() | ||
| - | | ||
| - | if self.check_ecc(message, | ||
| - | return result | ||
| - | else: | ||
| - | return False | ||
| - | | ||
| - | | ||
| - | |||
| - | |||
| - | |||
| - | # | ||
| - | | ||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | |||
| - | ===== Text-mode client ===== | ||
| - | |||
| - | ++++ client.py | | ||
| - | |||
| - | <file python client.py> | ||
| - | __author__ = ' | ||
| - | # client to work with server_oo.py | ||
| - | # | ||
| - | |||
| - | # # v0.1 | ||
| - | |||
| - | import socket | ||
| - | |||
| - | try: | ||
| - | import readline | ||
| - | except ImportError: | ||
| - | pass | ||
| - | |||
| - | import threading | ||
| - | import time | ||
| - | |||
| - | |||
| - | HOST, PORT = " | ||
| - | |||
| - | smlog = " | ||
| - | log_active = [] | ||
| - | |||
| - | |||
| - | def make_con(): | ||
| - | global sock | ||
| - | sock = socket.socket(socket.AF_INET, | ||
| - | sock.connect((HOST, | ||
| - | |||
| - | |||
| - | def get_rnum(): | ||
| - | global num | ||
| - | global radios | ||
| - | |||
| - | names = connect(" | ||
| - | |||
| - | radios = names.split() | ||
| - | num = len(radios) | ||
| - | return num | ||
| - | |||
| - | |||
| - | def get_rname(i): | ||
| - | r = radios[i] | ||
| - | return r | ||
| - | |||
| - | |||
| - | def list_radios(): | ||
| - | num = get_rnum() | ||
| - | global radio | ||
| - | print " | ||
| - | for i in range(0, num): | ||
| - | r = get_rname(i) | ||
| - | print "Radio %d is %s" % (i + 1, r) | ||
| - | |||
| - | |||
| - | def get_lradio(): | ||
| - | num = get_rnum() | ||
| - | |||
| - | lradio = raw_input(" | ||
| - | |||
| - | if not lradio: | ||
| - | print " | ||
| - | return False | ||
| - | elif int(lradio) > num: | ||
| - | print " | ||
| - | return False | ||
| - | |||
| - | else: | ||
| - | |||
| - | return lradio | ||
| - | |||
| - | |||
| - | def set_radio(): | ||
| - | num = get_rnum() | ||
| - | # global radio | ||
| - | global radio_num | ||
| - | global rname | ||
| - | print "There are currently %d radios connected." | ||
| - | for i in range(0, num): | ||
| - | r = get_rname(i) | ||
| - | print "Radio %d is %s" % (i + 1, r) | ||
| - | |||
| - | radio = raw_input(" | ||
| - | try: | ||
| - | if not radio: | ||
| - | print " | ||
| - | return False, False | ||
| - | |||
| - | elif int(radio) > num: | ||
| - | print " | ||
| - | return False, False | ||
| - | except ValueError: | ||
| - | print " | ||
| - | return False, False | ||
| - | |||
| - | else: | ||
| - | radio_num = "" | ||
| - | rname = get_rname(int(radio) - 1) | ||
| - | return radio_num, rname | ||
| - | |||
| - | |||
| - | def prompt(): | ||
| - | print "" | ||
| - | print "The available commands are:" | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print "p2on : Set Pre-amp 2 On" | ||
| - | print "poff : Set Pre-amp Off" | ||
| - | print "gatt : Get Attn" | ||
| - | print "aton : Set Attn On" | ||
| - | print " | ||
| - | print " | ||
| - | print "sync : Sync freq/mode on two radios" | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print "" | ||
| - | |||
| - | |||
| - | def start(): | ||
| - | global radio_num | ||
| - | global rname | ||
| - | global sock | ||
| - | pfreq = connect(" | ||
| - | pmode = connect(" | ||
| - | | ||
| - | data = raw_input(rname + " (" + pfreq + " " + pmode + ") " + " > " | ||
| - | if len(data.split()) > 1: | ||
| - | if (data.split())[0] == " | ||
| - | sfreq = (data.split())[1] | ||
| - | freq = connect(" | ||
| - | print "%s replied: %s" % (rname, freq) | ||
| - | start() | ||
| - | elif (data.split())[0] == " | ||
| - | smode = (data.split())[1] | ||
| - | mode = connect(" | ||
| - | print "%s replied: %s" % (rname, mode) | ||
| - | start() | ||
| - | else: | ||
| - | | ||
| - | print "only one command at a time please" | ||
| - | start() | ||
| - | elif data == " | ||
| - | oldf = connect(" | ||
| - | newf = str(float(oldf) + 1) | ||
| - | freq = connect(" | ||
| - | print "%s replied: %s" % (rname, freq) | ||
| - | start() | ||
| - | | ||
| - | elif data == " | ||
| - | oldf = connect(" | ||
| - | newf = str(float(oldf) - 1) | ||
| - | freq = connect(" | ||
| - | print "%s replied: %s" % (rname, freq) | ||
| - | start() | ||
| - | | ||
| - | elif data == " | ||
| - | list_radios() | ||
| - | start() | ||
| - | elif data == " | ||
| - | |||
| - | radio_num, rname = set_radio() | ||
| - | |||
| - | while not radio_num: | ||
| - | radio_num, rname = set_radio() | ||
| - | |||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | print "Radio selected is %s" % rname | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | mode = connect(" | ||
| - | print "%s replied: %s" % (rname, mode) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | smode = raw_input(" | ||
| - | mode = connect(" | ||
| - | print "%s replied: %s" % (rname, mode) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | freq = connect(" | ||
| - | print "%s replied: %s kHz" % (rname, freq) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | sfreq = raw_input(" | ||
| - | freq = connect(" | ||
| - | print "%s replied: %s" % (rname, freq) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | smeter = connect(" | ||
| - | print "%s replied: %sdBm" % (rname, smeter) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | preamp = connect(" | ||
| - | print "%s replied: %s" % (rname, preamp) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | preamp = connect(" | ||
| - | print "%s replied: %s" % (rname, preamp) | ||
| - | start() | ||
| - | | ||
| - | elif data == " | ||
| - | preamp = connect(" | ||
| - | print "%s replied: %s" % (rname, preamp) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | preamp = connect(" | ||
| - | print "%s replied: %s" % (rname, preamp) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | preamp = connect(" | ||
| - | print "%s replied: %s" % (rname, preamp) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | att = connect(" | ||
| - | print "%s replied: %s" % (rname, att) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | att = connect(" | ||
| - | print "%s replied: %s" % (rname, att) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | get_all() | ||
| - | start() | ||
| - | |||
| - | |||
| - | elif data == " | ||
| - | fname = raw_input(" | ||
| - | if fname == "": | ||
| - | fname = smlog | ||
| - | # check file is valid | ||
| - | try: | ||
| - | f = open(fname, ' | ||
| - | f.close() | ||
| - | except IOError: | ||
| - | print " | ||
| - | start() | ||
| - | |||
| - | list_radios() | ||
| - | |||
| - | lradio = get_lradio() | ||
| - | |||
| - | while not lradio: | ||
| - | lradio = get_lradio() | ||
| - | |||
| - | rname = get_rname(int(lradio) - 1) | ||
| - | if lradio in log_active: | ||
| - | print " | ||
| - | else: | ||
| - | tlog = int(raw_input(" | ||
| - | p = threading.Thread(target=log, | ||
| - | p.setDaemon(True) | ||
| - | p.start() | ||
| - | log_active.append(lradio) | ||
| - | start() | ||
| - | |||
| - | elif data == " | ||
| - | sync_result = sync() | ||
| - | print sync_result | ||
| - | start() | ||
| - | |||
| - | |||
| - | elif data == " | ||
| - | prompt() | ||
| - | start() | ||
| - | |||
| - | |||
| - | elif data == " | ||
| - | rx = connect(" | ||
| - | print " | ||
| - | |||
| - | else: | ||
| - | #prompt() | ||
| - | start() | ||
| - | |||
| - | |||
| - | def get_all(): | ||
| - | num = get_rnum() | ||
| - | global radio | ||
| - | print "There are currently %d radios connected." | ||
| - | print " | ||
| - | for i in range(0, num): | ||
| - | r = get_rname(i) | ||
| - | n = "" | ||
| - | freq = connect(" | ||
| - | mode = connect(" | ||
| - | smeter = connect(" | ||
| - | preamp = connect(" | ||
| - | att = connect(" | ||
| - | |||
| - | print " | ||
| - | print " | ||
| - | print "Mode: %s" % mode | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | print " | ||
| - | |||
| - | print "" | ||
| - | |||
| - | |||
| - | def log(p, t, f): | ||
| - | print " | ||
| - | tlog = t | ||
| - | sradio = get_rname(int(p) - 1) | ||
| - | |||
| - | sr = "" | ||
| - | while True: | ||
| - | try: | ||
| - | frequency = connect(" | ||
| - | smeter = connect(" | ||
| - | mode = connect(" | ||
| - | write_file(f, | ||
| - | time.sleep(tlog) | ||
| - | finally: | ||
| - | pass | ||
| - | |||
| - | |||
| - | def get_mradio(): | ||
| - | num = get_rnum() | ||
| - | |||
| - | mradio = raw_input(" | ||
| - | |||
| - | if not mradio: | ||
| - | print " | ||
| - | return False | ||
| - | elif int(mradio) > num: | ||
| - | print " | ||
| - | return False | ||
| - | |||
| - | else: | ||
| - | |||
| - | return mradio | ||
| - | |||
| - | |||
| - | def get_sradio(): | ||
| - | num = get_rnum() | ||
| - | sradio = raw_input(" | ||
| - | |||
| - | if not sradio: | ||
| - | print " | ||
| - | return False | ||
| - | elif int(sradio) > num: | ||
| - | print " | ||
| - | return False | ||
| - | |||
| - | else: | ||
| - | |||
| - | return sradio | ||
| - | |||
| - | |||
| - | def sync(): | ||
| - | num = get_rnum() | ||
| - | print "" | ||
| - | print "Set SLAVE to the same Frequency and Mode as MASTER.\r\n" | ||
| - | print " | ||
| - | for i in range(0, num): | ||
| - | r = get_rname(i) | ||
| - | print "%d is %s" % (i + 1, r) | ||
| - | |||
| - | mradio = get_mradio() | ||
| - | while not mradio: | ||
| - | mradio = get_mradio() | ||
| - | |||
| - | sradio = get_sradio() | ||
| - | while not sradio: | ||
| - | sradio = get_sradio() | ||
| - | sr = "" | ||
| - | mr = "" | ||
| - | mfreq = connect(" | ||
| - | mmode = connect(" | ||
| - | |||
| - | sfreq = connect(" | ||
| - | smode = connect(" | ||
| - | |||
| - | return (sfreq + " | ||
| - | |||
| - | |||
| - | # Try to send and receive in one-go, to prevent the logging thread and the main prog | ||
| - | # getting the wrong receive data | ||
| - | |||
| - | def connect(data): | ||
| - | try: | ||
| - | lock.acquire() | ||
| - | global sock | ||
| - | sock.sendall(data + " | ||
| - | received = sock.recv(2048) | ||
| - | finally: | ||
| - | lock.release() | ||
| - | |||
| - | return received | ||
| - | |||
| - | |||
| - | def write_file(fname, | ||
| - | filename = fname | ||
| - | f = open(filename, | ||
| - | timenow = time.strftime(" | ||
| - | log = " " | ||
| - | f.write(log) | ||
| - | f.close() | ||
| - | |||
| - | |||
| - | make_con() | ||
| - | |||
| - | lock = threading.Lock() | ||
| - | get_all() | ||
| - | print " | ||
| - | radio_num, rname = set_radio() | ||
| - | while not radio_num: | ||
| - | radio_num, rname = set_radio() | ||
| - | |||
| - | prompt() | ||
| - | start() | ||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | === Example session === | ||
| - | |||
| - | <code bash> | ||
| - | There are currently 3 radios connected. | ||
| - | ================================= | ||
| - | Status of Radio 1 (IC-R75) | ||
| - | |||
| - | Frequency : 2187.500 kHz | ||
| - | Mode: DATA | ||
| - | S-Meter: -114.2dBm | ||
| - | Preamp = 0 | ||
| - | Attenuator = 0 | ||
| - | ================================= | ||
| - | Status of Radio 2 (IC-M710) | ||
| - | |||
| - | Frequency : 16804.500 kHz | ||
| - | Mode: DATA | ||
| - | S-Meter: -120.0dBm | ||
| - | Preamp = None | ||
| - | Attenuator = None | ||
| - | ================================= | ||
| - | Status of Radio 3 (AR7030) | ||
| - | |||
| - | Frequency : 2182.000 kHz | ||
| - | Mode: USB | ||
| - | S-Meter: -110.3dBm | ||
| - | Preamp = 0 | ||
| - | Attenuator = 0 | ||
| - | ================================= | ||
| - | |||
| - | Please choose a radio | ||
| - | |||
| - | There are currently 3 radios connected. | ||
| - | Radio 1 is IC-R75 | ||
| - | Radio 2 is IC-M710 | ||
| - | Radio 3 is AR7030 | ||
| - | Choose a radio number from the list : 3 | ||
| - | |||
| - | The available commands are: | ||
| - | lr : List Radios | ||
| - | sr : Select the Radio to control | ||
| - | gr : Get currently selected Radio name | ||
| - | gm : Get Mode | ||
| - | sm : Set Mode | ||
| - | gf : Get Freq | ||
| - | sf : Set Freq | ||
| - | gs : Get S-meter | ||
| - | gp : Get Pre-amp | ||
| - | pon : Set Pre-amp On | ||
| - | p2on : Set Pre-amp 2 On | ||
| - | poff : Set Pre-amp Off | ||
| - | gatt : Get Attn | ||
| - | aton : Set Attn On | ||
| - | atoff: Set Attn Off | ||
| - | ga : Get All (status of all radios) | ||
| - | sync : Sync freq/mode on two radios | ||
| - | log : Setup background logging to file | ||
| - | h : Help (show this command list) | ||
| - | q : quit | ||
| - | |||
| - | AR7030 (2182.000 USB) > sf 5680 | ||
| - | AR7030 replied: Freq Set | ||
| - | AR7030 (5680.000 USB) > | ||
| - | |||
| - | </ | ||
| - | |||
| - | |||
| - | ===== GUI Client ===== | ||
| - | |||
| - | {{: | ||
| - | ==== No S-meter version ==== | ||
| - | |||
| - | |||
| - | ++++ gui_nosmeter.py | | ||
| - | |||
| - | <file python gui_nosmeter.py> | ||
| - | # # new class-based gui | ||
| - | |||
| - | from Tkinter import * | ||
| - | import socket | ||
| - | import threading | ||
| - | import time | ||
| - | |||
| - | |||
| - | |||
| - | class Network(object): | ||
| - | def __init__(self): | ||
| - | self.sock = self.make_con() | ||
| - | | ||
| - | def make_con(self): | ||
| - | self.sock = socket.socket(socket.AF_INET, | ||
| - | self.sock.connect((" | ||
| - | return self.sock | ||
| - | |||
| - | def connect(self, | ||
| - | try: | ||
| - | lock.acquire() | ||
| - | self.sock.sendall(data + " | ||
| - | self.received = self.sock.recv(2048) | ||
| - | finally: | ||
| - | lock.release() | ||
| - | return self.received | ||
| - | | ||
| - | class Dash(object): | ||
| - | def __init__(self, | ||
| - | self.master = master | ||
| - | | ||
| - | self.s = (n1.sock.getsockname()) | ||
| - | self.p = (n1.sock.getpeername()) | ||
| - | | ||
| - | | ||
| - | | ||
| - | dash_frame = Toplevel(master, | ||
| - | dash_frame.title(" | ||
| - | dash_frame.protocol(" | ||
| - | dash_frame.resizable(0, | ||
| - | dash_frame.geometry(" | ||
| - | dash_frame.grid() | ||
| - | | ||
| - | # | ||
| - | # | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | |||
| - | self.server_msg_l = StringVar() | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | q_button = Button(dash_frame, | ||
| - | q_button.grid(row = 3, column = 0, sticky = W) | ||
| - | | ||
| - | def handler(self): | ||
| - | pass | ||
| - | | ||
| - | def up_dash(self): | ||
| - | self.server_msg = n1.connect(" | ||
| - | self.server_msg_l.set(self.server_msg) | ||
| - | # | ||
| - | return | ||
| - | |||
| - | class nRadio(object): | ||
| - | def __init__(self, | ||
| - | self.master = master | ||
| - | | ||
| - | self.radio = radio | ||
| - | self.num = "" | ||
| - | |||
| - | | ||
| - | radio_frame = Frame(master, | ||
| - | | ||
| - | radio_frame.grid() | ||
| - | self.smeter_list = [] | ||
| - | |||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | # | ||
| - | # | ||
| - | # | ||
| - | # | ||
| - | |||
| - | self.name_l = StringVar() | ||
| - | self.l_name = Label(radio_frame, | ||
| - | self.l_name.grid(row=1, | ||
| - | | ||
| - | self.freq_l = StringVar() | ||
| - | self.l_freq = Label(radio_frame, | ||
| - | self.l_freq.grid(row=1, | ||
| - | | ||
| - | self.mode_l = StringVar() | ||
| - | self.l_mode = Label(radio_frame, | ||
| - | self.l_mode.grid(row=1, | ||
| - | ''' | ||
| - | self.e_mode = Entry(radio_frame, | ||
| - | self.e_mode.grid(row=2, | ||
| - | self.e_mode.bind('< | ||
| - | ''' | ||
| - | # | ||
| - | self.b_mode_usb = Button(radio_frame, | ||
| - | self.b_mode_usb.grid(row = 3, column = 0) | ||
| - | | ||
| - | self.b_mode_lsb = Button(radio_frame, | ||
| - | self.b_mode_lsb.grid(row = 3, column = 1) | ||
| - | | ||
| - | self.b_mode_cw = Button(radio_frame, | ||
| - | self.b_mode_cw.grid(row = 4, column = 0) | ||
| - | | ||
| - | self.b_mode_am = Button(radio_frame, | ||
| - | self.b_mode_am.grid(row = 4, column = 1) | ||
| - | | ||
| - | self.b_mode_data = Button(radio_frame, | ||
| - | self.b_mode_data.grid(row = 3, column = 2) | ||
| - | |||
| - | self.b_qsy_up = Button(radio_frame, | ||
| - | self.b_qsy_up.grid(row = 3, column = 3) | ||
| - | |||
| - | self.b_qsy_down = Button(radio_frame, | ||
| - | self.b_qsy_down.grid(row = 4, column = 3) | ||
| - | |||
| - | self.b_update = Button(radio_frame, | ||
| - | self.b_update.grid(row = 0, column = 3) | ||
| - | | ||
| - | | ||
| - | self.smeter_l = StringVar() | ||
| - | self.l_smeter = Label(radio_frame, | ||
| - | # | ||
| - | |||
| - | self.max_var = StringVar() | ||
| - | self.l_max = Label(radio_frame, | ||
| - | # | ||
| - | | ||
| - | self.ave_var = StringVar() | ||
| - | self.l_average = Label(radio_frame, | ||
| - | # | ||
| - | |||
| - | self.min_var = StringVar() | ||
| - | self.l_min = Label(radio_frame, | ||
| - | # | ||
| - | |||
| - | self.e_freq = Entry(radio_frame, | ||
| - | self.e_freq.grid(row=2, | ||
| - | self.e_freq.focus() | ||
| - | self.e_freq.bind('< | ||
| - | | ||
| - | | ||
| - | | ||
| - | self.pre = IntVar() | ||
| - | self.cb_pre = Checkbutton(radio_frame, | ||
| - | self.cb_pre.grid(row=5, | ||
| - | |||
| - | self.l_pre = Label(radio_frame, | ||
| - | self.l_pre.grid(row=5, | ||
| - | |||
| - | self.att = IntVar() | ||
| - | self.cb_att = Checkbutton(radio_frame, | ||
| - | self.cb_att.grid(row=6, | ||
| - | | ||
| - | self.l_att = Label(radio_frame, | ||
| - | self.l_att.grid(row=6, | ||
| - | | ||
| - | self.set_log = IntVar() | ||
| - | self.cb_log = Checkbutton(radio_frame, | ||
| - | # | ||
| - | | ||
| - | self.l_log = Label(radio_frame, | ||
| - | # | ||
| - | |||
| - | self.c1 = Canvas(radio_frame, | ||
| - | # | ||
| - | |||
| - | name = self.get_name(radio) | ||
| - | |||
| - | |||
| - | def get_all(self): | ||
| - | | ||
| - | self.get_freq(self.num) | ||
| - | self.get_mode(self.num) | ||
| - | # | ||
| - | self.get_preamp(self.num) | ||
| - | self.get_atten(self.num) | ||
| - | # | ||
| - | #self.avg() | ||
| - | #self.max() | ||
| - | #self.min() | ||
| - | | ||
| - | if self.set_log.get() == 1: | ||
| - | if int(time.time()) % 10.0 == 0: | ||
| - | self.write_file() | ||
| - | return | ||
| - | | ||
| - | def get_freq(self, | ||
| - | |||
| - | self.freq = n1.connect(" | ||
| - | self.freq_l.set(self.freq) | ||
| - | return | ||
| - | |||
| - | def set_freq(self, | ||
| - | self.freq = str(self.e_freq.get()) | ||
| - | self.newfreq = n1.connect(" | ||
| - | self.e_freq.delete(0, | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | def qsy_up(self, | ||
| - | | ||
| - | oldf = n1.connect(" | ||
| - | newf = str(float(oldf) + 1) | ||
| - | freq = n1.connect(" | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | def qsy_down(self, | ||
| - | oldf = n1.connect(" | ||
| - | newf = str(float(oldf) - 1) | ||
| - | freq = n1.connect(" | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | def get_mode(self, | ||
| - | self.mode = n1.connect(" | ||
| - | self.mode_l.set(self.mode) | ||
| - | return | ||
| - | |||
| - | def set_mode(self, | ||
| - | #self.mode = str(self.e_mode.get()) | ||
| - | self.newmode = n1.connect(" | ||
| - | # | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | def get_smeter(self, | ||
| - | self.smeter = n1.connect(" | ||
| - | self.smeter_l.set(self.smeter) | ||
| - | self.smeter_list.append(float(self.smeter)) | ||
| - | if len(self.smeter_list) > 120: | ||
| - | self.smeter_list.pop(0) | ||
| - | return | ||
| - | |||
| - | def avg(self): | ||
| - | s = self.smeter_list | ||
| - | total = 0 | ||
| - | for i in s: | ||
| - | total += i | ||
| - | self.av = round((total / len(s)), 1) | ||
| - | self.ave_var.set(str(self.av)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.av | ||
| - | |||
| - | def min(self): | ||
| - | s = self.smeter_list | ||
| - | self.mn = s[0] | ||
| - | for i in s: | ||
| - | if i < self.mn: | ||
| - | self.mn = i | ||
| - | self.min_var.set(str(self.mn)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.mn | ||
| - | |||
| - | def max(self): | ||
| - | s = self.smeter_list | ||
| - | self.mx = s[0] | ||
| - | for i in s: | ||
| - | if i > self.mx: | ||
| - | self.mx = i | ||
| - | self.max_var.set(str(self.mx)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.mx | ||
| - | |||
| - | def get_preamp(self, | ||
| - | self.preamp = n1.connect(" | ||
| - | if self.preamp == " | ||
| - | self.cb_pre.select() | ||
| - | elif self.preamp == " | ||
| - | self.cb_pre.deselect() | ||
| - | return | ||
| - | |||
| - | def preamp_onoff(self, | ||
| - | self.prestate = self.pre.get() | ||
| - | if self.prestate: | ||
| - | n1.connect(" | ||
| - | else: | ||
| - | n1.connect(" | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | def att_onoff(self, | ||
| - | self.attstate = self.att.get() | ||
| - | if self.attstate: | ||
| - | n1.connect(" | ||
| - | else: | ||
| - | n1.connect(" | ||
| - | self.get_all() | ||
| - | return | ||
| - | |||
| - | |||
| - | def get_atten(self, | ||
| - | self.atten = n1.connect(" | ||
| - | if self.atten == " | ||
| - | self.cb_att.select() | ||
| - | elif self.atten == " | ||
| - | self.cb_att.deselect() | ||
| - | return | ||
| - | |||
| - | def get_name(self, | ||
| - | print radio | ||
| - | self.all_names = n1.connect(" | ||
| - | self.radios = self.all_names.split() | ||
| - | print self.radios | ||
| - | self.name = self.radios[int(radio) - 1] | ||
| - | self.name_l.set(self.name) | ||
| - | return | ||
| - | |||
| - | def graph_points(self): | ||
| - | seq = self.smeter_list | ||
| - | y_stretch = 1 | ||
| - | y_gap = 5 | ||
| - | x_stretch = 0 | ||
| - | x_width = 2 | ||
| - | x_gap = 2 | ||
| - | height = 100 | ||
| - | self.c1.delete(ALL) | ||
| - | self.c1.create_line(0, | ||
| - | for x, y in enumerate(seq): | ||
| - | yd = y + 123 | ||
| - | x0 = x * x_stretch + x * x_width + x_gap | ||
| - | y0 = height - (yd * y_stretch + y_gap) | ||
| - | x1 = x * x_stretch + x * x_width + x_width + x_gap | ||
| - | y1 = height - y_gap | ||
| - | self.c1.create_rectangle(x0, | ||
| - | |||
| - | def write_file(self): | ||
| - | self.filename = self.name+" | ||
| - | self.f = open(self.filename, | ||
| - | timenow = time.strftime(" | ||
| - | log = " " | ||
| - | self.f.write(log) | ||
| - | self.f.close() | ||
| - | |||
| - | def update(): | ||
| - | #loops = 0 | ||
| - | |||
| - | try: | ||
| - | nRadio1.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio2.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio3.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio4.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | d1.up_dash() | ||
| - | return | ||
| - | |||
| - | def main(): | ||
| - | #loops = 0 | ||
| - | while True: | ||
| - | try: | ||
| - | nRadio1.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio2.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio3.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio4.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | d1.up_dash() | ||
| - | #loops += 1 | ||
| - | #print threading.currentThread().name, | ||
| - | time.sleep(5) | ||
| - | |||
| - | def close(): | ||
| - | n1.connect(" | ||
| - | root.destroy() | ||
| - | | ||
| - | | ||
| - | if __name__ == " | ||
| - | version = " | ||
| - | lock = threading.Lock() | ||
| - | root = Tk() | ||
| - | #w, h = root.winfo_screenwidth(), | ||
| - | # | ||
| - | # | ||
| - | root.title(" | ||
| - | # | ||
| - | n1 = Network() | ||
| - | # | ||
| - | # | ||
| - | | ||
| - | radio_count = int(n1.connect(" | ||
| - | if radio_count > 0: | ||
| - | nRadio1 = nRadio(root, | ||
| - | if radio_count > 1: | ||
| - | nRadio2 = nRadio(root, | ||
| - | if radio_count > 2: | ||
| - | nRadio3 = nRadio(root, | ||
| - | if radio_count > 3: | ||
| - | nRadio4 = nRadio(root, | ||
| - | | ||
| - | d1 = Dash(root) | ||
| - | | ||
| - | #print threading.currentThread().name | ||
| - | #m1 = threading.Thread(target = main) | ||
| - | # | ||
| - | #m1.start() | ||
| - | | ||
| - | update() | ||
| - | | ||
| - | root.mainloop() | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | {{: | ||
| - | |||
| - | |||
| - | |||
| - | ==== S-meter display version ==== | ||
| - | |||
| - | |||
| - | ++++ gui.py | | ||
| - | |||
| - | <file python gui.py> | ||
| - | # # new class-based gui | ||
| - | |||
| - | from Tkinter import * | ||
| - | import socket | ||
| - | import threading | ||
| - | import time | ||
| - | |||
| - | |||
| - | |||
| - | class Network(object): | ||
| - | def __init__(self): | ||
| - | self.sock = self.make_con() | ||
| - | | ||
| - | def make_con(self): | ||
| - | self.sock = socket.socket(socket.AF_INET, | ||
| - | self.sock.connect((" | ||
| - | return self.sock | ||
| - | |||
| - | def connect(self, | ||
| - | try: | ||
| - | lock.acquire() | ||
| - | self.sock.sendall(data + " | ||
| - | self.received = self.sock.recv(2048) | ||
| - | finally: | ||
| - | lock.release() | ||
| - | return self.received | ||
| - | | ||
| - | class Dash(object): | ||
| - | def __init__(self, | ||
| - | self.master = master | ||
| - | | ||
| - | self.s = (n1.sock.getsockname()) | ||
| - | self.p = (n1.sock.getpeername()) | ||
| - | | ||
| - | | ||
| - | | ||
| - | dash_frame = Toplevel(master, | ||
| - | dash_frame.title(" | ||
| - | dash_frame.protocol(" | ||
| - | dash_frame.resizable(0, | ||
| - | dash_frame.geometry(" | ||
| - | dash_frame.grid() | ||
| - | | ||
| - | self.utc_time = StringVar() | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | Label(dash_frame, | ||
| - | |||
| - | self.server_msg_l = StringVar() | ||
| - | Label(dash_frame, | ||
| - | | ||
| - | q_button = Button(dash_frame, | ||
| - | q_button.grid(row = 3, column = 0, sticky = W) | ||
| - | | ||
| - | def handler(self): | ||
| - | pass | ||
| - | | ||
| - | def up_dash(self): | ||
| - | self.server_msg = n1.connect(" | ||
| - | self.server_msg_l.set(self.server_msg) | ||
| - | self.utc_time.set(time.strftime(" | ||
| - | return | ||
| - | |||
| - | class nRadio(object): | ||
| - | def __init__(self, | ||
| - | self.master = master | ||
| - | | ||
| - | self.radio = radio | ||
| - | self.num = "" | ||
| - | |||
| - | | ||
| - | radio_frame = Frame(master, | ||
| - | | ||
| - | radio_frame.grid() | ||
| - | self.smeter_list = [] | ||
| - | |||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | Label(radio_frame, | ||
| - | |||
| - | self.name_l = StringVar() | ||
| - | self.l_name = Label(radio_frame, | ||
| - | self.l_name.grid(row=1, | ||
| - | | ||
| - | self.freq_l = StringVar() | ||
| - | self.l_freq = Label(radio_frame, | ||
| - | self.l_freq.grid(row=1, | ||
| - | | ||
| - | self.mode_l = StringVar() | ||
| - | self.l_mode = Label(radio_frame, | ||
| - | self.l_mode.grid(row=1, | ||
| - | ''' | ||
| - | self.e_mode = Entry(radio_frame, | ||
| - | self.e_mode.grid(row=2, | ||
| - | self.e_mode.bind('< | ||
| - | ''' | ||
| - | # | ||
| - | self.b_mode_usb = Button(radio_frame, | ||
| - | self.b_mode_usb.grid(row = 3, column = 0) | ||
| - | | ||
| - | self.b_mode_lsb = Button(radio_frame, | ||
| - | self.b_mode_lsb.grid(row = 3, column = 1) | ||
| - | | ||
| - | self.b_mode_cw = Button(radio_frame, | ||
| - | self.b_mode_cw.grid(row = 4, column = 0) | ||
| - | | ||
| - | self.b_mode_am = Button(radio_frame, | ||
| - | self.b_mode_am.grid(row = 4, column = 1) | ||
| - | | ||
| - | self.b_mode_data = Button(radio_frame, | ||
| - | self.b_mode_data.grid(row = 3, column = 2) | ||
| - | | ||
| - | self.smeter_l = StringVar() | ||
| - | self.l_smeter = Label(radio_frame, | ||
| - | self.l_smeter.grid(row=1, | ||
| - | |||
| - | self.max_var = StringVar() | ||
| - | self.l_max = Label(radio_frame, | ||
| - | self.l_max.grid(row=1, | ||
| - | | ||
| - | self.ave_var = StringVar() | ||
| - | self.l_average = Label(radio_frame, | ||
| - | self.l_average.grid(row=1, | ||
| - | |||
| - | self.min_var = StringVar() | ||
| - | self.l_min = Label(radio_frame, | ||
| - | self.l_min.grid(row=1, | ||
| - | |||
| - | self.e_freq = Entry(radio_frame, | ||
| - | self.e_freq.grid(row=2, | ||
| - | self.e_freq.focus() | ||
| - | self.e_freq.bind('< | ||
| - | | ||
| - | | ||
| - | | ||
| - | self.pre = IntVar() | ||
| - | self.cb_pre = Checkbutton(radio_frame, | ||
| - | self.cb_pre.grid(row=5, | ||
| - | |||
| - | self.l_pre = Label(radio_frame, | ||
| - | self.l_pre.grid(row=5, | ||
| - | |||
| - | self.att = IntVar() | ||
| - | self.cb_att = Checkbutton(radio_frame, | ||
| - | self.cb_att.grid(row=6, | ||
| - | | ||
| - | self.l_att = Label(radio_frame, | ||
| - | self.l_att.grid(row=6, | ||
| - | | ||
| - | self.set_log = IntVar() | ||
| - | self.cb_log = Checkbutton(radio_frame, | ||
| - | self.cb_log.grid(row = 6, column = 1, sticky = W) | ||
| - | | ||
| - | self.l_log = Label(radio_frame, | ||
| - | self.l_log.grid(row = 6, column = 0, sticky = E) | ||
| - | |||
| - | self.c1 = Canvas(radio_frame, | ||
| - | self.c1.grid(row=2, | ||
| - | |||
| - | name = self.get_name(radio) | ||
| - | |||
| - | |||
| - | def get_all(self): | ||
| - | | ||
| - | self.get_freq(self.num) | ||
| - | self.get_mode(self.num) | ||
| - | self.get_smeter(self.num) | ||
| - | self.get_preamp(self.num) | ||
| - | self.get_atten(self.num) | ||
| - | self.graph_points() | ||
| - | self.avg() | ||
| - | self.max() | ||
| - | self.min() | ||
| - | | ||
| - | if self.set_log.get() == 1: | ||
| - | if int(time.time()) % 10.0 == 0: | ||
| - | self.write_file() | ||
| - | return | ||
| - | | ||
| - | def get_freq(self, | ||
| - | |||
| - | self.freq = n1.connect(" | ||
| - | self.freq_l.set(self.freq) | ||
| - | return | ||
| - | |||
| - | def set_freq(self, | ||
| - | self.freq = str(self.e_freq.get()) | ||
| - | self.newfreq = n1.connect(" | ||
| - | self.e_freq.delete(0, | ||
| - | | ||
| - | return | ||
| - | |||
| - | def get_mode(self, | ||
| - | self.mode = n1.connect(" | ||
| - | self.mode_l.set(self.mode) | ||
| - | return | ||
| - | |||
| - | def set_mode(self, | ||
| - | #self.mode = str(self.e_mode.get()) | ||
| - | self.newmode = n1.connect(" | ||
| - | # | ||
| - | return | ||
| - | |||
| - | def get_smeter(self, | ||
| - | self.smeter = n1.connect(" | ||
| - | self.smeter_l.set(self.smeter) | ||
| - | self.smeter_list.append(float(self.smeter)) | ||
| - | if len(self.smeter_list) > 120: | ||
| - | self.smeter_list.pop(0) | ||
| - | return | ||
| - | |||
| - | def avg(self): | ||
| - | s = self.smeter_list | ||
| - | total = 0 | ||
| - | for i in s: | ||
| - | total += i | ||
| - | self.av = round((total / len(s)), 1) | ||
| - | self.ave_var.set(str(self.av)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.av | ||
| - | |||
| - | def min(self): | ||
| - | s = self.smeter_list | ||
| - | self.mn = s[0] | ||
| - | for i in s: | ||
| - | if i < self.mn: | ||
| - | self.mn = i | ||
| - | self.min_var.set(str(self.mn)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.mn | ||
| - | |||
| - | def max(self): | ||
| - | s = self.smeter_list | ||
| - | self.mx = s[0] | ||
| - | for i in s: | ||
| - | if i > self.mx: | ||
| - | self.mx = i | ||
| - | self.max_var.set(str(self.mx)) | ||
| - | self.c1.create_line(0, | ||
| - | return self.mx | ||
| - | |||
| - | def get_preamp(self, | ||
| - | self.preamp = n1.connect(" | ||
| - | if self.preamp == " | ||
| - | self.cb_pre.select() | ||
| - | elif self.preamp == " | ||
| - | self.cb_pre.deselect() | ||
| - | return | ||
| - | |||
| - | def preamp_onoff(self, | ||
| - | self.prestate = self.pre.get() | ||
| - | if self.prestate: | ||
| - | n1.connect(" | ||
| - | else: | ||
| - | n1.connect(" | ||
| - | return | ||
| - | |||
| - | def att_onoff(self, | ||
| - | self.attstate = self.att.get() | ||
| - | if self.attstate: | ||
| - | n1.connect(" | ||
| - | else: | ||
| - | n1.connect(" | ||
| - | return | ||
| - | |||
| - | |||
| - | def get_atten(self, | ||
| - | self.atten = n1.connect(" | ||
| - | if self.atten == " | ||
| - | self.cb_att.select() | ||
| - | elif self.atten == " | ||
| - | self.cb_att.deselect() | ||
| - | return | ||
| - | |||
| - | def get_name(self, | ||
| - | print radio | ||
| - | self.all_names = n1.connect(" | ||
| - | self.radios = self.all_names.split() | ||
| - | print self.radios | ||
| - | self.name = self.radios[int(radio) - 1] | ||
| - | self.name_l.set(self.name) | ||
| - | return | ||
| - | |||
| - | def graph_points(self): | ||
| - | seq = self.smeter_list | ||
| - | y_stretch = 1 | ||
| - | y_gap = 5 | ||
| - | x_stretch = 0 | ||
| - | x_width = 2 | ||
| - | x_gap = 2 | ||
| - | height = 100 | ||
| - | self.c1.delete(ALL) | ||
| - | self.c1.create_line(0, | ||
| - | for x, y in enumerate(seq): | ||
| - | yd = y + 123 | ||
| - | x0 = x * x_stretch + x * x_width + x_gap | ||
| - | y0 = height - (yd * y_stretch + y_gap) | ||
| - | x1 = x * x_stretch + x * x_width + x_width + x_gap | ||
| - | y1 = height - y_gap | ||
| - | self.c1.create_rectangle(x0, | ||
| - | |||
| - | def write_file(self): | ||
| - | self.filename = self.name+" | ||
| - | self.f = open(self.filename, | ||
| - | timenow = time.strftime(" | ||
| - | log = " " | ||
| - | self.f.write(log) | ||
| - | self.f.close() | ||
| - | |||
| - | |||
| - | |||
| - | def main(): | ||
| - | #loops = 0 | ||
| - | while True: | ||
| - | try: | ||
| - | nRadio1.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio2.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio3.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | try: | ||
| - | nRadio4.get_all() | ||
| - | except: | ||
| - | pass | ||
| - | d1.up_dash() | ||
| - | #loops += 1 | ||
| - | #print threading.currentThread().name, | ||
| - | time.sleep(1) | ||
| - | |||
| - | def close(): | ||
| - | n1.connect(" | ||
| - | root.destroy() | ||
| - | | ||
| - | | ||
| - | if __name__ == " | ||
| - | version = " | ||
| - | lock = threading.Lock() | ||
| - | root = Tk() | ||
| - | #w, h = root.winfo_screenwidth(), | ||
| - | # | ||
| - | root.geometry(" | ||
| - | root.title(" | ||
| - | # | ||
| - | n1 = Network() | ||
| - | # | ||
| - | # | ||
| - | | ||
| - | radio_count = int(n1.connect(" | ||
| - | if radio_count > 0: | ||
| - | nRadio1 = nRadio(root, | ||
| - | if radio_count > 1: | ||
| - | nRadio2 = nRadio(root, | ||
| - | if radio_count > 2: | ||
| - | nRadio3 = nRadio(root, | ||
| - | if radio_count > 3: | ||
| - | nRadio4 = nRadio(root, | ||
| - | | ||
| - | d1 = Dash(root) | ||
| - | | ||
| - | #print threading.currentThread().name | ||
| - | m1 = threading.Thread(target = main) | ||
| - | m1.setDaemon(True) | ||
| - | m1.start() | ||
| - | | ||
| - | |||
| - | | ||
| - | root.mainloop() | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | </ | ||
| - | |||
| - | ++++ | ||
| - | |||
| - | {{: | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | ===== Further Information ===== | ||
| - | |||
| - | |||
| - | {{tag>}} | ||
| - | |||