Project

General

Profile

New Model #4787 » v.py

Daniel Clemmensen, 02/25/2019 11:27 AM

 
# Copyright 2019 Dan Clemmensen <DanClemmensen@Gmail.com>
#
# Read the initial response from an FT-65 or FT-4 radio and print it.
# This response is assumed to be some sort of radio type ID.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Version string reader for Yaesu FT-4 and FT-65 radios.
Works on Python 2 and Python 3.
"""
import serial
import os
import time
import sys

def sendcmd(pipe, cmd):
"""
send a command bytelist to radio,receive and return the resulting bytelist.
Input: pipe - serial port object to use
cmd - bytes to send
This cable is "two-wire": The TxD and RxD are "or'ed" so we receive
whatever we send and then whatever response the radio sends. We check the
echo and strip it, then read characters one at a time until we get an ack.
"""
pipe.write(cmd)
echo = pipe.read(len(cmd))
if echo != cmd:
msg = "Bad echo. Sent:" + repr(cmd) + ", "
msg += "Received:" + repr(echo)
print(msg)
raise ("Incorrect echo on serial port.")
response = b""
i = 0
toolong = 256 # arbitrary
while True:
b = pipe.read(1)
if b == b'\x06':
break
else:
response += b
i += 1
if i > toolong:
LOG.debug("Response too long. got" + repr(response))
raise ("Response too long.")
return(response)
# main program
port = sys.argv[1] #get serial port name from commandline
try:
pipe = serial.Serial(port, 9600, timeout=4)
pipe.dtr = True
pipe.flushInput()
pipe.flushOutput()
except Exception as e:
print("failed to open serial port %s" %port)
print('should be e.g. "com0" on Windows or e.g. "/dev/ttyUSB0" on Linux')
raise e
print("turn radio on")
time.sleep(3)
pipe.flushInput()
pipe.flushOutput()
if b"QX" != sendcmd(pipe, b"PROGRAM"):
raise errors.RadioError("expected QX from radio.")
version = sendcmd(pipe, b'\x02')
sendcmd(pipe, b"END")
pipe.close()
print("Version= %s" % repr(version))
print("turn radio off")

(20-20/22)