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
import subprocess
import sys
try:
import serial
import serial.tools
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", 'pyserial'])
finally:
import serial
import serial.tools
import time
import serial.tools.list_ports
import serial.tools.miniterm
CTRL_C = bytes(0x03)
CTRL_D = bytes(0x04)
CTRL_C_SUB = bytes(0x7F)
def get_trinkey_port():
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
if 'Trinkey' in desc or '239A' in desc.upper() or '239A' in hwid.upper():
s = serial.Serial(port, baudrate=1152000)
return s
return None
def connect_to_serial(s):
class NoCTRLC(serial.tools.miniterm.Transform):
def tx(self, text):
return text.replace('k', chr(0x03)).replace('r', chr(0x4))
serial.tools.miniterm.TRANSFORMATIONS['ctrlc'] = NoCTRLC
miniterm = serial.tools.miniterm.Miniterm(
s, echo=False, filters=('ctrlc',))
miniterm.exit_character = chr(0x03)
miniterm.menu_character = chr(0x0D)
miniterm.raw = True
miniterm.set_rx_encoding('UTF-8')
miniterm.set_tx_encoding('UTF-8')
sys.stderr.write('--- Miniterm on {p.name} {p.baudrate},{p.bytesize},{p.parity},{p.stopbits} ---\n'.format(
p=miniterm.serial))
sys.stderr.write('--- Quit: {} ---\n'.format(
serial.tools.miniterm.key_description(miniterm.exit_character)))
miniterm.start()
miniterm.join()
sys.stderr.write('\n--- exit ---\n')
miniterm.close()