-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtxDevCH340TTY.py
345 lines (272 loc) · 10.5 KB
/
txDevCH340TTY.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/python3
"""
Telex Device - Serial Communication over CH340-Chip (not FTDI, not Prolific, not CP213x)
Protocol:
https://wiki.telexforum.de/index.php?title=TW39_Verfahren_(Teil_2)
"""
__author__ = "Jochen Krapf"
__email__ = "jk@nerd2nerd.org"
__copyright__ = "Copyright 2018, JK"
__license__ = "GPL3"
__version__ = "0.0.2"
import serial
import time
import logging
l = logging.getLogger("piTelex." + __name__)
import txCode
import txBase
#def LOG(text:str, level:int=3):
# #log.LOG('\033[30;43m<'+text+'>\033[0m', level)
#######
class TelexCH340TTY(txBase.TelexBase):
def __init__(self, **params):
super().__init__()
self.id = 'chT'
self.params = params
portname = params.get('portname', '/dev/ttyUSB0')
baudrate = params.get('baudrate', 50)
bytesize = params.get('bytesize', 5)
stopbits = params.get('stopbits', serial.STOPBITS_ONE_POINT_FIVE)
coding = params.get('coding', 0)
loopback = params.get('loopback', None)
inverse_dtr = params.get('inverse_dtr', False)
self._local_echo = params.get('loc_echo', False)
self._rx_buffer = []
self._tx_buffer = []
self._counter_LTRS = 0
self._counter_FIGS = 0
self._counter_dial = 0
self._time_last_dial = 0
self._cts_stable = True # rxd=Low
self._cts_counter = 0
self._time_squelch = 0
self._time_tx_lock = 0
self._is_enabled = False
self._is_online = False
self._last_out_waiting = 0
self._set_mode(params['mode'])
if loopback is not None:
self._loopback = loopback
self._inverse_dtr = params.get('inverse_dtr', self._inverse_dtr)
self._inverse_rts = params.get('inverse_rts', self._inverse_rts)
# init serial
self._tty = serial.Serial(portname, write_timeout=0)
if baudrate not in self._tty.BAUDRATES and baudrate not in (45, 100):
raise Exception('Baudrate not supported')
if bytesize not in self._tty.BYTESIZES:
raise Exception('Databits not supported')
if stopbits not in self._tty.STOPBITS:
raise Exception('Stopbits not supported')
self._tty.baudrate = baudrate
self._tty.bytesize = bytesize
self._tty.stopbits = stopbits
self._baudrate = baudrate
self._inverse_dtr = inverse_dtr
# init codec
#character_duration = (bytesize + 1.0 + stopbits) / baudrate
character_duration = (bytesize + 3.0 ) / baudrate # CH340 sends always with 2 stop bits
self._mc = txCode.BaudotMurrayCode(self._loopback, coding=coding, character_duration=character_duration)
self._set_enable(False)
self._set_online(False)
# -----
def _set_mode(self, mode:str):
self._loopback = False
self._use_pulse_dial = False
self._use_squelch = False
self._use_cts = False
self._inverse_cts = False
#self._use_dtr = False
self._inverse_dtr = False
self._inverse_rts = False
self._use_dedicated_line = True
mode = mode.upper()
if mode.find('TW39') >= 0:
self._loopback = True
self._use_cts = True
self._use_pulse_dial = True
self._use_squelch = True
self._use_dedicated_line = False
if mode.find('TWM') >= 0:
self._loopback = True
self._use_cts = True
self._use_pulse_dial = False
self._use_squelch = True
self._use_dedicated_line = False
if mode.find('V.10') >= 0 or mode.find('V10') >= 0:
self._use_cts = True
self._inverse_cts = True
#self._inverse_dtr = True
self._use_dedicated_line = False
if mode.find('EDS') >= 0:
self._loopback = False
self._use_cts = True
self._inverse_dtr = True
self._use_squelch = True
self._use_dedicated_line = False
# -----
def exit(self):
self._tty.close()
# -----
def __del__(self):
super().__del__()
# =====
def read(self) -> str:
if self._tty.in_waiting:
a = ''
bb = self._tty.read(1)
if bb and (not self._use_squelch or time.monotonic() >= self._time_squelch):
if self._is_enabled or self._use_dedicated_line:
if self._local_echo:
self._tty.write(bb)
a = self._mc.decodeBM2A(bb)
if a:
self._check_special_sequences(a)
elif self._is_online and self._use_pulse_dial:
b = bb[0]
if b == 0: # break or idle mode
pass
elif (b & 0x13) == 0x10: # valid dial pulse - min 3 bits = 40ms, max 5 bits = 66ms
self._counter_dial += 1
self._time_last_dial = time.monotonic()
self._cts_counter = 0
if a:
self._rx_buffer.append(a)
#if self._local_echo:
# self._tx_buffer.append(a)
if self._rx_buffer:
ret = self._rx_buffer.pop(0)
return ret
# -----
def write(self, a:str, source:str):
if len(a) > 1 and a[0] == '\x1b':
self._check_commands(a[1:])
return
if a == '#':
a = '@' # ask teletype for hardware ID
if a:
if self._is_enabled or self._use_dedicated_line:
self._tx_buffer.append(a)
# =====
def idle(self):
if (not self._use_squelch) or time.monotonic() >= max(self._time_squelch, self._time_tx_lock):
if self._tx_buffer:
aa = []
a = None
while a != '@' and self._tx_buffer:
a = self._tx_buffer.pop(0)
aa.append(a)
if a == '@':
# WRU received: lock sending until after 21 character's
# time has passed. This may need to be raised due to
# the CH340's substantial write buffer.
self._time_tx_lock = time.monotonic() + 7.5*21/self._baudrate
aa = ''.join(aa)
self._tx_buffer = []
bb = self._mc.encodeA2BM(aa)
if bb:
self._rx_buffer.append('\x1b~' + str(self._tty.out_waiting + len(bb)))
# Force-update last out_waiting value to trigger idle2Hz update
self._last_out_waiting += len(bb)
self._tty.write(bb)
# -----
def idle20Hz(self):
time_act = time.monotonic()
if self._use_pulse_dial and self._counter_dial and (time_act - self._time_last_dial) > 0.2:
if self._counter_dial >= 10:
self._counter_dial = 0
a = str(self._counter_dial)
self._rx_buffer.append(a)
self._time_last_dial = time_act
self._counter_dial = 0
if self._use_cts:
cts = not self._tty.cts != self._inverse_cts # logical xor
if cts != self._cts_stable:
self._cts_counter += 1
if self._cts_counter == 10: # 0.5sec
self._cts_stable = cts
if not cts: # rxd=Low
self._rx_buffer.append('\x1bST')
pass
elif not self._is_enabled: # rxd=High
self._rx_buffer.append('\x1bAT')
pass
pass
else:
self._cts_counter = 0
# -----
def idle2Hz(self):
# send printer FIFO info
out_waiting = self._tty.out_waiting
if out_waiting != self._last_out_waiting:
self._rx_buffer.append('\x1b~' + str(out_waiting))
self._last_out_waiting = out_waiting
# -----
def _set_online(self, online:bool):
self._is_online = online
self._tty.rts = online != self._inverse_rts # RTS
# -----
def _set_enable(self, enable:bool):
if enable and not self._is_enabled:
# Confirm enable status for MCP
self._rx_buffer.append('\x1bAA')
self._is_enabled = enable
self._tty.dtr = enable != self._inverse_dtr # DTR -> True=Low=motor_on
if 0: # experimental
self._tty.break_condition = not enable
self._mc.reset()
if self._use_squelch:
self._set_time_squelch(0.5)
#self._tty.send_break(1.0)
# -----
def _set_time_squelch(self, t_diff):
t = time.monotonic() + t_diff
if self._time_squelch < t:
self._time_squelch = t
# -----
def _set_pulse_dial(self, enable:bool):
if not self._use_pulse_dial:
return
if enable:
self._tty.baudrate = 75 # fix baudrate to scan number switch
else:
self._tty.baudrate = self._baudrate
# =====
def _check_special_sequences(self, a:str):
if not self._use_cts:
if a == '<':
self._counter_LTRS += 1
if self._counter_LTRS == 5:
self._rx_buffer.append('\x1bST')
else:
self._counter_LTRS = 0
if a == '>':
self._counter_FIGS += 1
if self._counter_FIGS == 5:
self._rx_buffer.append('\x1bAT')
else:
self._counter_FIGS = 0
# -----
def _check_commands(self, a:str):
enable = None
if a in ('A', 'AA'):
self._set_pulse_dial(False)
self._set_online(True)
enable = True
if a in ('Z', 'ZZ'):
self._tx_buffer = [] # empty write buffer...
self._set_pulse_dial(False)
self._set_online(False)
enable = False #self._use_dedicated_line
if not enable and self._use_squelch:
self._set_time_squelch(1.5)
if a in ('WB',):
self._set_online(True)
if self._use_pulse_dial: # TW39
self._set_pulse_dial(True)
self._tty.write(b'\x1E') # send pulse with 25ms low to signal 'ready for dialing' ('Wahlbereitschaft')
enable = False
else: # dedicated line, TWM, V.10
enable = True
if enable is not None:
self._set_enable(enable)
#######