Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 1 | #! python |
| 2 | # |
| 3 | # Python Serial Port Extension for Win32, Linux, BSD, Jython |
| 4 | # see __init__.py |
| 5 | # |
| 6 | # This module implements a special URL handler that wraps an other port, |
Chris Liechti | 6c8887c | 2015-08-18 23:38:05 +0200 | [diff] [blame] | 7 | # print the traffic for debugging purposes. With this, it is possible |
| 8 | # to debug the serial port traffic on every application that uses |
| 9 | # serial_for_url. |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 10 | # |
| 11 | # (C) 2015 Chris Liechti <cliechti@gmx.net> |
| 12 | # |
| 13 | # SPDX-License-Identifier: BSD-3-Clause |
| 14 | # |
| 15 | # URL format: spy://port[?option[=value][&option[=value]]] |
| 16 | # options: |
| 17 | # - dev=X a file or device to write to |
| 18 | # - color use escape code to colorize output |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 19 | # - raw forward raw bytes instead of hexdump |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 20 | # |
| 21 | # example: |
| 22 | # redirect output to an other terminal window on Posix (Linux): |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 23 | # python -m serial.tools.miniterm spy:///dev/ttyUSB0?dev=/dev/pts/14\&color |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 24 | |
| 25 | import sys |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 26 | import time |
| 27 | |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 28 | import serial |
| 29 | |
| 30 | try: |
| 31 | import urlparse |
| 32 | except ImportError: |
| 33 | import urllib.parse as urlparse |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 34 | |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 35 | |
| 36 | def sixteen(data): |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 37 | """\ |
| 38 | yield tuples of hex and ASCII display in multiples of 16. Includes a |
| 39 | space after 8 bytes and (None, None) after 16 bytes and at the end. |
| 40 | """ |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 41 | n = 0 |
| 42 | for b in serial.iterbytes(data): |
| 43 | yield ('{:02X} '.format(ord(b)), b if b' ' <= b < b'\x7f' else b'.') |
| 44 | n += 1 |
| 45 | if n == 8: |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 46 | yield (' ', ' ') |
Chris Liechti | 12a439f | 2015-08-20 23:01:53 +0200 | [diff] [blame^] | 47 | elif n >= 16: |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 48 | yield (None, None) |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 49 | n = 0 |
Chris Liechti | 12a439f | 2015-08-20 23:01:53 +0200 | [diff] [blame^] | 50 | if n > 0: |
| 51 | while n < 16: |
| 52 | n += 1 |
| 53 | if n == 8: |
| 54 | yield (' ', ' ') |
| 55 | yield (' ', ' ') |
| 56 | yield (None, None) |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def hexdump(data): |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 60 | """yield lines with hexdump of data""" |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 61 | values = [] |
| 62 | ascii = [] |
| 63 | for h, a in sixteen(data): |
| 64 | if h is None: |
| 65 | yield ' '.join([ |
| 66 | ''.join(values), |
| 67 | ''.join(ascii)]) |
| 68 | del values[:] |
| 69 | del ascii[:] |
| 70 | else: |
| 71 | values.append(h) |
| 72 | ascii.append(a) |
| 73 | |
| 74 | |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 75 | class FormatRaw(object): |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 76 | """forward rx and tx data to output""" |
| 77 | |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 78 | def __init__(self, output, color): |
| 79 | self.output = output |
| 80 | self.color = color |
| 81 | self.rx_color = '\x1b[32m' |
| 82 | self.tx_color = '\x1b[31m' |
| 83 | |
| 84 | def rx(self, data): |
| 85 | if self.color: |
| 86 | self.output.write(self.rx_color) |
| 87 | self.output.write(data) |
| 88 | self.output.flush() |
| 89 | |
| 90 | def tx(self, data): |
| 91 | if self.color: |
| 92 | self.output.write(self.tx_color) |
| 93 | self.output.write(data) |
| 94 | self.output.flush() |
| 95 | |
| 96 | def control(self, name, value): |
| 97 | pass |
| 98 | |
| 99 | |
| 100 | class FormatHexdump(object): |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 101 | """\ |
| 102 | Create a hex dump of RX ad TX data, show when control lines are read or |
| 103 | written. |
| 104 | |
| 105 | output example:: |
| 106 | |
| 107 | 000000.000 FLSH flushInput |
| 108 | 000002.469 RTS inactive |
| 109 | 000002.773 RTS active |
Chris Liechti | 12a439f | 2015-08-20 23:01:53 +0200 | [diff] [blame^] | 110 | 000003.001 TX 48 45 4C 4C 4F HELLO |
| 111 | 000003.102 RX 48 45 4C 4C 4F HELLO |
| 112 | |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 113 | """ |
| 114 | |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 115 | def __init__(self, output, color): |
| 116 | self.start_time = time.time() |
| 117 | self.output = output |
| 118 | self.color = color |
| 119 | self.rx_color = '\x1b[32m' |
| 120 | self.tx_color = '\x1b[31m' |
| 121 | self.control_color = '\x1b[37m' |
| 122 | |
| 123 | def write_line(self, timestamp, label, value): |
| 124 | self.output.write('{:010.3f} {:4} {}\n'.format(timestamp, label, value)) |
| 125 | self.output.flush() |
| 126 | |
| 127 | def rx(self, data): |
| 128 | if self.color: |
| 129 | self.output.write(self.rx_color) |
| 130 | for row in hexdump(data): |
| 131 | self.write_line(time.time() - self.start_time, 'RX', row) |
| 132 | |
| 133 | def tx(self, data): |
| 134 | if self.color: |
| 135 | self.output.write(self.tx_color) |
| 136 | for row in hexdump(data): |
| 137 | self.write_line(time.time() - self.start_time, 'TX', row) |
| 138 | |
| 139 | def control(self, name, value): |
| 140 | if self.color: |
| 141 | self.output.write(self.control_color) |
| 142 | self.write_line(time.time() - self.start_time, name, value) |
| 143 | |
| 144 | |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 145 | class Serial(serial.Serial): |
| 146 | """Just inherit the native Serial port implementation and patch the port property.""" |
| 147 | |
| 148 | def __init__(self, *args, **kwargs): |
| 149 | super(Serial, self).__init__(*args, **kwargs) |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 150 | self.formatter = None |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 151 | |
| 152 | @serial.Serial.port.setter |
| 153 | def port(self, value): |
| 154 | if value is not None: |
| 155 | serial.Serial.port.__set__(self, self.fromURL(value)) |
| 156 | |
| 157 | def fromURL(self, url): |
| 158 | """extract host and port from an URL string""" |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 159 | parts = urlparse.urlsplit(url) |
Chris Liechti | 6c8887c | 2015-08-18 23:38:05 +0200 | [diff] [blame] | 160 | if parts.scheme != 'spy': |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 161 | raise serial.SerialException('expected a string in the form "spy://port[?option[=value][&option[=value]]]": not starting with spy:// (%r)' % (parts.scheme,)) |
| 162 | # process options now, directly altering self |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 163 | formatter = FormatHexdump |
| 164 | color = False |
| 165 | output = sys.stderr |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 166 | for option, values in urlparse.parse_qs(parts.query, True).items(): |
| 167 | if option == 'dev': |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 168 | output = open(values[0], 'w') |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 169 | elif option == 'color': |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 170 | color = True |
| 171 | elif option == 'raw': |
| 172 | formatter = FormatRaw |
| 173 | self.formatter = formatter(output, color) |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 174 | return ''.join([parts.netloc, parts.path]) |
| 175 | |
| 176 | def write(self, tx): |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 177 | self.formatter.tx(tx) |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 178 | return super(Serial, self).write(tx) |
| 179 | |
| 180 | def read(self, size=1): |
| 181 | rx = super(Serial, self).read(size) |
| 182 | if rx: |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 183 | self.formatter.rx(rx) |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 184 | return rx |
| 185 | |
| 186 | |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 187 | def flush(self): |
| 188 | self.formatter.control('FLSH', 'flush') |
| 189 | super(Serial, self).flush() |
| 190 | |
| 191 | def flushInput(self): |
| 192 | self.formatter.control('FLSH', 'flushInput') |
Chris Liechti | 12a439f | 2015-08-20 23:01:53 +0200 | [diff] [blame^] | 193 | super(Serial, self).flushInput() |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 194 | |
| 195 | def flushOutput(self): |
| 196 | self.formatter.control('FLSH', 'flushOutput') |
| 197 | super(Serial, self).flushOutput() |
| 198 | |
| 199 | def sendBreak(self, duration=0.25): |
Chris Liechti | 9dcb423 | 2015-08-20 01:15:37 +0200 | [diff] [blame] | 200 | self.formatter.control('BRK', 'sendBreak {}'.format(duration)) |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 201 | super(Serial, self).sendBreak(duration) |
| 202 | |
| 203 | def setBreak(self, level=1): |
| 204 | self.formatter.control('BRK', 'active' if level else 'inactive') |
| 205 | super(Serial, self).setBreak(level) |
| 206 | |
| 207 | def setRTS(self, level=1): |
| 208 | self.formatter.control('RTS', 'active' if level else 'inactive') |
| 209 | super(Serial, self).setRTS(level) |
| 210 | |
| 211 | def setDTR(self, level=1): |
| 212 | self.formatter.control('DTR', 'active' if level else 'inactive') |
| 213 | super(Serial, self).setDTR(level) |
| 214 | |
| 215 | def getCTS(self): |
| 216 | level = super(Serial, self).getCTS() |
| 217 | self.formatter.control('CTS', 'active' if level else 'inactive') |
| 218 | return level |
| 219 | |
| 220 | def getDSR(self): |
| 221 | level = super(Serial, self).getDSR() |
| 222 | self.formatter.control('DSR', 'active' if level else 'inactive') |
| 223 | return level |
| 224 | |
| 225 | def getRI(self): |
| 226 | level = super(Serial, self).getRI() |
| 227 | self.formatter.control('RI', 'active' if level else 'inactive') |
| 228 | return level |
| 229 | |
| 230 | def getCD(self): |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 231 | level = super(Serial, self).getCD() |
Chris Liechti | 12a439f | 2015-08-20 23:01:53 +0200 | [diff] [blame^] | 232 | self.formatter.control('CD', 'active' if level else 'inactive') |
Chris Liechti | 730e9f2 | 2015-08-19 03:07:05 +0200 | [diff] [blame] | 233 | return level |
| 234 | |
Chris Liechti | ece60cf | 2015-08-18 02:39:03 +0200 | [diff] [blame] | 235 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 236 | if __name__ == '__main__': |
| 237 | s = Serial(None) |
| 238 | s.port = 'spy:///dev/ttyS0' |
| 239 | print(s) |
| 240 | |