Project

General

Profile

New Model #4787 » v.py

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

 
1
# Copyright 2019 Dan Clemmensen <DanClemmensen@Gmail.com>
2
#
3
# Read the initial response from an FT-65 or FT-4 radio and print it.
4
# This response is assumed to be some sort of radio type ID.
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
"""
19
Version string reader for Yaesu FT-4 and FT-65 radios.
20
Works on Python 2 and Python 3. 
21
"""
22
import serial
23
import os
24
import time
25
import sys
26

    
27
def sendcmd(pipe, cmd):
28
    """
29
    send a command bytelist to radio,receive and return the resulting bytelist.
30
    Input: pipe         - serial port object to use
31
           cmd          - bytes to send
32
    This cable is "two-wire": The TxD and RxD are "or'ed" so we receive
33
    whatever we send and then whatever response the radio sends. We check the
34
    echo and strip it, then read characters one at a time until we get an ack.
35
    """
36
    pipe.write(cmd)
37
    echo = pipe.read(len(cmd))
38
    if echo != cmd:
39
        msg = "Bad echo. Sent:" + repr(cmd) + ", "
40
        msg += "Received:" + repr(echo)
41
        print(msg)
42
        raise ("Incorrect echo on serial port.")
43
    response = b""
44
    i = 0
45
    toolong = 256        # arbitrary
46
    while True:
47
        b = pipe.read(1)
48
        if b == b'\x06':
49
            break
50
        else:
51
            response += b
52
            i += 1
53
            if i > toolong:
54
                LOG.debug("Response too long. got" + repr(response))
55
                raise ("Response too long.")
56
    return(response)
57
  
58
# main program
59
port = sys.argv[1]    #get serial port name from commandline
60
try:
61
    pipe = serial.Serial(port, 9600, timeout=4)
62
    pipe.dtr = True
63
    pipe.flushInput()
64
    pipe.flushOutput()            
65
except Exception as e:
66
    print("failed to open serial port %s" %port)
67
    print('should be e.g. "com0" on Windows or e.g. "/dev/ttyUSB0" on Linux')
68
    raise e
69
print("turn radio on")
70
time.sleep(3)
71
pipe.flushInput()
72
pipe.flushOutput()            
73
if b"QX" != sendcmd(pipe, b"PROGRAM"):
74
    raise errors.RadioError("expected QX from radio.")
75
version = sendcmd(pipe, b'\x02')
76
sendcmd(pipe, b"END")
77
pipe.close()
78
print("Version= %s" % repr(version))
79
print("turn radio off")
80
	
81

    
(20-20/22)