| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import serial |
| 15 |
import time |
| 16 |
import sys |
| 17 |
import struct |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
filebase = 'codeplug' |
| 22 |
filecount = 0 |
| 23 |
comport = 'COM6' |
| 24 |
|
| 25 |
|
| 26 |
def hexDump(s): |
| 27 |
h = " ".join(map("{:02x}".format, s)) |
| 28 |
t = "" |
| 29 |
for i in range(len(s)): |
| 30 |
c = s[i] |
| 31 |
if c>=0x20 and c<0x7f: |
| 32 |
t += chr(c) |
| 33 |
else: |
| 34 |
t += "." |
| 35 |
return( h + " | " + t) |
| 36 |
|
| 37 |
|
| 38 |
if len(sys.argv) == 2: |
| 39 |
comport = sys.argv[1] |
| 40 |
elif len(sys.argv) >= 3: |
| 41 |
print("Usage: " + sys.argv[0] + ' [comport]') |
| 42 |
exit() |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
serialPort = None |
| 48 |
|
| 49 |
try: |
| 50 |
print("Trying comport " + comport) |
| 51 |
serialPort = serial.Serial(port = comport, baudrate=4000000, bytesize=8, timeout=1, stopbits=serial.STOPBITS_ONE) |
| 52 |
except (err): |
| 53 |
print('ERR: Could not open port ' + comport) |
| 54 |
print("Usage: " + sys.argv[0] + ' servername [comport]: ' + err) |
| 55 |
exit() |
| 56 |
|
| 57 |
|
| 58 |
out = None |
| 59 |
nextaddr = None |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
try: |
| 64 |
|
| 65 |
while 1: |
| 66 |
|
| 67 |
command = '' |
| 68 |
|
| 69 |
command = serialPort.read() |
| 70 |
while serialPort.in_waiting > 0: |
| 71 |
command += serialPort.read() |
| 72 |
|
| 73 |
|
| 74 |
if ( len(command) == 0 ): |
| 75 |
pass |
| 76 |
|
| 77 |
elif ( command == b'PROGRAM'): |
| 78 |
print("Program session requested.") |
| 79 |
resp = b'QX\x06' |
| 80 |
serialPort.write(resp) |
| 81 |
filename = "{}_{:04}.hex".format(filebase, filecount) |
| 82 |
out = open(filename, "w") |
| 83 |
nextaddr = None |
| 84 |
|
| 85 |
elif ( command == b'\x02' ): |
| 86 |
print("Device info requested.") |
| 87 |
resp = b'ID868UVE\x00V102\x00\x00\x06' |
| 88 |
serialPort.write(resp) |
| 89 |
|
| 90 |
elif ( command == b'R\x02\xfa\x00\x20\x10' ): |
| 91 |
print("Read special memory request.") |
| 92 |
resp = b'W\x02\xfa\x00\x20\x10\xff\xff\xff\xff\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x26\x06' |
| 93 |
serialPort.write(resp) |
| 94 |
|
| 95 |
elif ( command[0:4] == b'R\x02\xfa\x00' and command[5] == 16 ): |
| 96 |
|
| 97 |
print("Read local information.") |
| 98 |
|
| 99 |
resp = b'W\x02\xfa\x00' + bytes([command[4]]) + b'\x10' |
| 100 |
|
| 101 |
if ( command[4] == 0x00 ): |
| 102 |
resp += b'\x00\x00\x00\x00\x01\x01\x01\x00\x00\x01\x01\x20\x20\x20\x20\xff' |
| 103 |
|
| 104 |
elif ( command[4] == 0x10 ): |
| 105 |
resp += b'\x44\x38\x37\x38\x55\x56\x00\x01\x00\xff\xff\xff\xff\xff\xff\xff' |
| 106 |
|
| 107 |
elif ( command[4] == 0x30 ): |
| 108 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 109 |
elif ( command[4] == 0x40 ): |
| 110 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 111 |
elif ( command[4] == 0x50 ): |
| 112 |
resp += b'\x31\x32\x33\x34\x35\x36\x37\x38\xff\xff\xff\xff\xff\xff\xff\xff' |
| 113 |
elif ( command[4] == 0x60 ): |
| 114 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 115 |
elif ( command[4] == 0x70 ): |
| 116 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 117 |
elif ( command[4] == 0x80 ): |
| 118 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 119 |
elif ( command[4] == 0x90 ): |
| 120 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 121 |
elif ( command[4] == 0xa0 ): |
| 122 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 123 |
|
| 124 |
elif ( command[4] == 0xb0 ): |
| 125 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 126 |
elif ( command[4] == 0xc0 ): |
| 127 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 128 |
elif ( command[4] == 0xd0 ): |
| 129 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 130 |
elif ( command[4] == 0xe0 ): |
| 131 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 132 |
elif ( command[4] == 0xf0 ): |
| 133 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 134 |
|
| 135 |
else: |
| 136 |
resp += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
| 137 |
|
| 138 |
resp = resp + bytes( [sum(resp[1:]) & 0xff] ) + b'\x06' |
| 139 |
|
| 140 |
serialPort.write(resp) |
| 141 |
|
| 142 |
elif ( command[0] == ord('W') ) : |
| 143 |
resp = b'\x06' |
| 144 |
serialPort.write(resp) |
| 145 |
res = struct.unpack(">cIB16sBB", command) |
| 146 |
addr = res[1] |
| 147 |
if nextaddr != addr: |
| 148 |
out.write((8+3+16*3+16+2)*"-" + "\n") |
| 149 |
out.write("{:08X} : {}\n".format(addr, hexDump(res[3]))) |
| 150 |
nextaddr = addr+16 |
| 151 |
|
| 152 |
elif ( command == b'END' ): |
| 153 |
print("End session.") |
| 154 |
resp = b'\x06' |
| 155 |
serialPort.write(resp) |
| 156 |
out.close() |
| 157 |
filecount += 1 |
| 158 |
|
| 159 |
elif ( command == b'UPDATE' ): |
| 160 |
|
| 161 |
print("Start Firmware Update. Only useful if device is in update receiving mode. (Switch on while pressing PF3 (blue button on top) and PTT keys)") |
| 162 |
resp = b'\x06' |
| 163 |
serialPort.write(resp) |
| 164 |
|
| 165 |
elif ( command == b'\x18' ): |
| 166 |
print("Firmware Update Send Complete. Switch device on while pressing PF2 (top left side) and PTT keys to start installer.") |
| 167 |
resp = b'\x06' |
| 168 |
serialPort.write(resp) |
| 169 |
|
| 170 |
elif ( command[0] == 0x01 ): |
| 171 |
print("Firmware data.") |
| 172 |
resp = b'\x06' |
| 173 |
serialPort.write(resp) |
| 174 |
else: |
| 175 |
|
| 176 |
pass |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
finally: |
| 181 |
print('QRT') |
| 182 |
serialPort.close() |
| 183 |
|