Debian Codegraph @

putserial

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # vim: set ts=4 sw=4 et :
4 #
5 # putserial - program to write data to a serial port
6 #
7 # Copyright 2020 Sony Corporation
8 #
9 # This program is provided under the Gnu General Public License (GPL)
10 # version 2 ONLY. This program is distributed WITHOUT ANY WARRANTY.
11 # See the LICENSE file, which should have accompanied this program,
12 # for the text of the license.
13 #
14 # Written 2020-03-02 by Tim Bird <[email protected]>
15 #
16 # To do:
17 # - finish program
18 # - remove command line options that don't make sense
19 # - decide usage:
20 # putserial -d <device> speed data [<file>..]
21 # if no files specified, put data read from stdin
22 # - does putserial read data and display it, while writing?
23 #
24
25 # Use the module docstring as the usage text for the program
26 """Putserial writes data to a serial port.
27
28 The main reason it is better than simple cat, is that it first
29 programs the line speed of the port, which avoids some errors.
30
31 Options:
32 -h, --help Print this message
33 -d, --device=<devpath> Set the device to read (default '/dev/ttyS0')
34 -b, --baudrate=<val> Set the baudrate (default 115200)
35 -B <val> Force the baudrate to the indicated value
36 (grabserial won't check if the baudrate is legal)
37 -w, --width=<val> Set the data bit width (default 8)
38 -p, --parity=<val> Set the parity (default N)
39 -s, --stopbits=<val> Set the stopbits (default 1)
40 -x, --xonxoff Enable software flow control (default off)
41 -r, --rtscts Enable RTS/CTS flow control (default off)
42 -i, --input=<file> Use the data from the indicated file as input,
43 rather than a command line argument
44
45 Ex: putserial -d /dev/ttyUSB0
46 """
47
48
49 import os
50 import sys
51 import getopt
52 import time
53 import datetime
54 import re
55
56 try:
57 import thread
58 except ImportError:
59 import _thread as thread
60
61 import serial
62
63 VERSION = (2, 0, 4)
64
65 verbose = 0 # pylint: disable=I0011,C0103
66 cmdinput = u"" # pylint: disable=I0011,C0103
67
68
69 def vprint(message):
70 """Print message if in verbose mode."""
71 if verbose:
72 print(message)
73
74 def eprint(message):
75 """Print message to standard error."""
76 sys.stderr.write(message+'\n')
77
78 def usage():
79 """Show grabserial usage help."""
80 print("Usage: grabserial -d <device> [options]\n")
81 print(__doc__)
82 sys.exit(0)
83
84
85 def device_exists(device):
86 """Check that the specified serial device exists."""
87 if os.path.islink(device):
88 device=os.path.realpath(device)
89 try:
90 from serial.tools import list_ports
91
92 for port in list_ports.comports():
93 if port[0] == device:
94 return True
95
96 return False
97 except serial.SerialException:
98 return os.path.exists(device)
99
100
101 def read_input():
102 """Read input from stdin in a thread separate from the grab routine."""
103 global cmdinput # pylint: disable=I0011,C0103,W0603
104
105 # NOTE: cmdinput is in unicode (to make handling similar between
106 # python2 and python3)
107
108 while 1:
109 if sys.version_info < (3, 0):
110 try:
111 # raw_input in python 2.x returns byte string
112 # decode to unicode
113 cmdinput = raw_input().decode(sys.stdin.encoding)
114 except EOFError:
115 # if we're piping input, we want to stop trying to read
116 # it when the pipe closes, or the file ends
117 break
118 else:
119 # raw_input is gone in python3
120 # https://www.python.org/dev/peps/pep-3111/
121 # input() returns string in unicode already
122 try:
123 cmdinput = input() # pylint: disable=I0011,W0141
124 except EOFError:
125 break
126
127 # OK - no more user input, just wait for program exit
128 while 1:
129 time.sleep(1)
130
131
132 # grab - main routine to grab a serial port and transfer data to it
133 # Also can take an optional file descriptor for where to send the data
134 # by default, data read from the serial port is sent to sys.stdout, but
135 # you can specify your own (already open) file descriptor, or None. This
136 # would only make sense if you specified another out_filename with
137 # "-o","myoutputfilename"
138 # Return value: True if we should 'restart' the program
139 def grab(arglist, outputfd=sys.stdout):
140 """Grab data from a serial port and produce formatted output.
141
142 Arguments:
143 arglist : the list of arguments to configure the serial
144 port and control output and processing
145 (see usage help).
146 outputfd (optional) : a file stream to which output should be sent.
147 Defaults to sys.stdout.
148
149 Returns True if the grab should be restarted. This may be the
150 case if the connection was broken due to a timeout or an error
151 on the serial port, and continuous recording is requested.
152 """
153 global verbose # pylint: disable=I0011,C0103,W0603
154 global cmdinput # pylint: disable=I0011,C0103,W0603
155
156 # parse the command line options
157 try:
158 opts, args = getopt.getopt(
159 arglist,
160 "hli:d:b:B:w:p:s:xrfc:taTF:m:e:o:AQvVq:nSC", [
161 "help",
162 "launchtime",
163 "inlinepat=",
164 "instantpat=",
165 "device=",
166 "baudrate=",
167 "width=",
168 "parity=",
169 "stopbits=",
170 "xonxoff",
171 "rtscts",
172 "force-reset",
173 "command=",
174 "time",
175 "again",
176 "systime",
177 "timeformat=",
178 "match=",
179 "endtime=",
180 "output=",
181 "append",
182 "quiet",
183 "verbose",
184 "version",
185 "quitpat=",
186 "nodelta",
187 "skip",
188 "crtonewline",
189 "command-mode",
190 ])
191 except getopt.GetoptError as err:
192 # print help info and exit
193 eprint("Error parsing command line options:")
194 eprint(str(err))
195 eprint("Use 'grabserial -h' to get usage help")
196 sys.exit(2)
197
198 sd = serial.Serial()
199 sd.port = ""
200 sd.baudrate = 115200
201 sd.bytesize = serial.EIGHTBITS
202 sd.parity = serial.PARITY_NONE
203 sd.stopbits = serial.STOPBITS_ONE
204 sd.xonxoff = False
205 sd.rtscts = False
206 sd.dsrdtr = False
207 # specify a read timeout of 1 second
208 sd.timeout = 1
209 force = False
210 show_time = 0
211 show_systime = 0
212 basepat = ""
213 inlinepat = ''
214 quitpat = ''
215 basetime = 0
216 inlinetime = None
217 endtime = 0
218 out_filename = None
219 out = None
220 out_permissions = "wb"
221 append = False
222 command = ""
223 command_mode = False
224 skip_device_check = 0
225 cr_to_nl = 0
226 restart = False
227 quiet = False
228 systime_format = "%H:%M:%S.%f"
229 use_delta = True
230 out_filenamehasdate = 0
231
232 for opt, arg in opts:
233 if opt in ["-h", "--help"]:
234 usage()
235 if opt in ["-d", "--device"]:
236 device = arg
237 if not skip_device_check and not device_exists(device):
238 eprint("""Error: serial device '%s' does not exist, aborting.
239 If you think this port really exists, then try using the -S option
240 to skip the serial device check. (put it before the -d argument)
241
242 Use 'grabserial -h' for usage help."""
243 % device)
244 sd.close()
245 sys.exit(2)
246 sd.port = device
247 if opt in ["-b", "--baudrate"]:
248 baud = int(arg)
249 if baud not in sd.BAUDRATES:
250 eprint("Error: invalid baud rate '%d' specified" % baud)
251 eprint("Valid baud rates are: %s" % str(sd.BAUDRATES))
252 eprint("You can force the baud rate using the -B option")
253 sd.close()
254 sys.exit(3)
255 sd.baudrate = baud
256 if opt == "-B":
257 sd.baudrate = int(arg)
258 if opt in ["-p", "--parity"]:
259 par = arg.upper()
260 if par not in sd.PARITIES:
261 eprint("Error: invalid parity '%s' specified" % par)
262 eprint("Valid parities are: %s" % str(sd.PARITIES))
263 sd.close()
264 sys.exit(3)
265 sd.parity = par
266 if opt in ["-w", "--width"]:
267 width = int(arg)
268 if width not in sd.BYTESIZES:
269 eprint("Error: invalid data bit width '%d' specified" % width)
270 eprint("Valid data bit widths are: %s" % str(sd.BYTESIZES))
271 sd.close()
272 sys.exit(3)
273 sd.bytesize = width
274 if opt in ["-s", "--stopbits"]:
275 stop = int(arg)
276 if stop not in sd.STOPBITS:
277 eprint("Error: invalid stopbits '%d' specified" % stop)
278 eprint("Valid stopbits are: %s" % str(sd.STOPBITS))
279 sd.close()
280 sys.exit(3)
281 sd.stopbits = stop
282 if opt in ["-c", "--command"]:
283 command = arg
284 if opt in ["-C", "--command-mode"]:
285 command_mode = True
286 if opt in ["-x", "--xonxoff"]:
287 sd.xonxoff = True
288 if opt in ["-r", "--rtscts"]:
289 sd.rtscts = True
290 if opt in ["-f", "--force-set"]:
291 force = True
292 if opt in ["-t", "--time"]:
293 show_time = 1
294 show_systime = 0
295 if opt in ["-a", "--again"]:
296 restart = True
297 if opt in ["-T", "--systime"]:
298 show_time = 0
299 show_systime = 1
300 if opt in ["-F", "--timeformat"]:
301 systime_format = arg
302 if opt in ["-m", "--match"]:
303 basepat = arg
304 if opt in ["-i", "--inlinepat", "--instantpat"]:
305 # --instantpat is supported for backwards compatibility
306 inlinepat = arg
307 if opt in ["-q", "--quitpat"]:
308 quitpat = arg
309 if opt in ["-l", "--launchtime"]:
310 vprint('Setting basetime to time of program launch')
311 basetime = time.time()
312 if opt in ["-e", "--endtime"]:
313 endstr = arg
314 try:
315 endtime = time.time()+float(endstr)
316 except ValueError:
317 eprint("Error: invalid endtime %s specified" % arg)
318 sd.close()
319 sys.exit(3)
320 if opt in ["-o", "--output"]:
321 out_filename = arg
322 if out_filename == "%":
323 out_filename = "%Y-%m-%dT%H:%M:%S"
324 if "%d" in out_filename:
325 out_pattern = out_filename
326 out_filenamehasdate = 1
327 if "%" in out_filename:
328 out_filename = datetime.datetime.now().strftime(out_filename)
329 if opt in ["-A", "--append"]:
330 out_permissions = "a+b"
331 append = True
332 if opt in ["-Q", "--quiet"]:
333 quiet = True
334 if opt in ["-v", "--verbose"]:
335 verbose = 1
336 if opt in ["-V", "--version"]:
337 print("grabserial version %d.%d.%d" % VERSION)
338 sd.close()
339 sys.exit(0)
340 if opt in ["-S", "--skip"]:
341 skip_device_check = 1
342 if opt in ["-n", "--nodelta"]:
343 use_delta = False
344 if opt in ["--crtonewline"]:
345 cr_to_nl = 1
346
347 if args:
348 eprint("Error: unrecognized argument '%s'" % args[0])
349 eprint("Use 'grabserial -h' to get usage help")
350 eprint("")
351 sys.exit(2)
352
353 if command_mode:
354 if not command:
355 eprint("Error: Must specify a command in command-mode")
356 sd.close()
357 sys.exit(3)
358 if not quitpat:
359 eprint("Error: Must specify a quit pattern in command-mode")
360 sd.close()
361 sys.exit(3)
362 cmd_index = 0
363 cmd_done = False
364 quit_index = 0
365 quit_done = False
366 vprint("Executing command '%s', and terminating on '%s'" %
367 (command, quitpat))
368
369 # if verbose, show what our settings are
370 if sd.port:
371 vprint("Opening serial port %s" % sd.port)
372 vprint("%d:%d%s%s:xonxoff=%d:rtscts=%d" %
373 (sd.baudrate, sd.bytesize, sd.parity, sd.stopbits,
374 sd.xonxoff, sd.rtscts))
375 else:
376 eprint("Error: Missing serial port to read from.")
377 eprint("Use 'grabserial -h' to get usage help")
378 sys.exit(2)
379
380 if endtime and not restart:
381 vprint("Program set to end in %s seconds" % endstr)
382 if endtime and restart:
383 vprint("Program set to restart after %s seconds." % endstr)
384 if show_time:
385 vprint("Printing timing information for each line")
386 if show_systime:
387 vprint("Printing absolute timing information for each line")
388 if basepat:
389 vprint("Using pattern '%s' to set base time" % basepat)
390 if inlinepat:
391 vprint("Using inline pattern '%s' to report time of at end of run"
392 % inlinepat)
393 if quitpat and not restart:
394 vprint("Using pattern '%s' to exit program" % quitpat)
395 if quitpat and restart:
396 vprint("Using pattern '%s' to restart program" % quitpat)
397 if skip_device_check:
398 vprint("Skipping check of serial device")
399 if out_filename:
400 try:
401 # open in binary mode, to pass through data as unmodified
402 # as possible
403 out = open(out_filename, out_permissions)
404 if out_filenamehasdate:
405 out_opendate = datetime.date.today()
406 except IOError:
407 print("Can't open output file '%s'" % out_filename)
408 sys.exit(1)
409 if append:
410 vprint("Appending data to '%s'" % out_filename)
411 else:
412 vprint("Saving data to '%s'" % out_filename)
413 if quiet:
414 vprint("Keeping quiet on stdout")
415
416 prev1 = 0
417 linetime = 0
418 newline = 1
419 curline = ""
420 xline = b""
421 vprint("Use Control-C to stop...")
422
423 try:
424 # pyserial does not reconfigure the device if the settings
425 # don't change from the previous ones. This causes issues
426 # with (at least) some USB serial converters
427 # Allow user to force device reconfiguration
428 if force:
429 toggle = sd.xonxoff
430 sd.xonxoff = not toggle
431 sd.open()
432 sd.close()
433 sd.xonxoff = toggle
434
435 sd.open()
436 sd.flushInput()
437 sd.flushOutput()
438
439 if command:
440 command += u"\n"
441 sd.write(command.encode("utf8"))
442 sd.flush()
443 except serial.serialutil.SerialException:
444 # This is the exception which is raised when you unplug the USB UART.
445 # Applies to both python 2 and 3 on Linux and Windows.
446 stop_reason = "grabserial stopped due to a SerialException"
447
448 # capture stdin to send to serial port
449 try:
450 thread.start_new_thread(read_input, ())
451 except thread.error:
452 print("Error starting thread for read input\n")
453
454 stop_reason = "putserial stopped for an unknown reason"
455 # read from the serial port until something stops the program
456 while 1:
457 try:
458 if cmdinput:
459 sd.write((cmdinput + u"\n").encode("utf8"))
460 cmdinput = u""
461
462 # read for up to 1 second
463 # NOTE: x should be a byte string in both python 2 and 3
464 x = sd.read(1)
465
466 # see if we're supposed to stop yet
467 if endtime and time.time() > endtime:
468 stop_reason = "grabserial stopped due to time expiration"
469 break
470
471 # if we didn't read anything, loop
472 if len(x) == 0:
473 continue
474
475 # convert carriage returns to newlines.
476 if x == b"\r":
477 if cr_to_nl:
478 x = b"\n"
479 else:
480 continue
481
482 # set basetime to when first char is received
483 if not basetime:
484 basetime = time.time()
485
486 # if outputting data to a file with a date in its name and the
487 # date has changed, then close it and open a new file.
488 if (out_filename
489 and out_filenamehasdate
490 and newline
491 and datetime.date.today() > out_opendate
492 and not endtime):
493 vprint("Closing output file: '%s'\n" % out_filename)
494 out.close()
495 out_filename = datetime.datetime.now().strftime(out_pattern)
496 vprint("Opening new output file: '%s'\n" % out_filename)
497 try:
498 out = open(out_filename, out_permissions)
499 out_opendate = datetime.date.today()
500 except IOError:
501 print("Can't open output file '%s'" % out_filename)
502 sys.exit(1)
503
504 if show_time and newline:
505 linetime = time.time()
506 elapsed = linetime-basetime
507 delta = elapsed-prev1
508 msg = "[%4.6f %2.6f] " % (elapsed, delta)
509 if not quiet:
510 if outputfd:
511 outputfd.write(msg)
512 if out:
513 try:
514 out.write(msg.encode(sys.stdout.encoding))
515 except UnicodeEncodeError:
516 try:
517 out.write(msg.encode("utf8"))
518 except UnicodeEncodeError:
519 out.write(msg)
520
521 prev1 = elapsed
522 newline = 0
523
524 if show_systime and newline:
525 linetime = time.time()
526 linetimestr = datetime.datetime.now().strftime(systime_format)
527 elapsed = linetime-basetime
528 if use_delta:
529 delta = elapsed-prev1
530 msg = "[%s %2.6f] " % (linetimestr, delta)
531 else:
532 msg = "[%s] " % (linetimestr)
533 if not quiet:
534 outputfd.write(msg)
535 if out:
536 try:
537 out.write(msg.encode(sys.stdout.encoding))
538 except UnicodeEncodeError:
539 try:
540 out.write(msg.encode("utf8"))
541 except UnicodeEncodeError:
542 out.write(msg)
543
544 prev1 = elapsed
545 newline = 0
546
547 out_char = x.decode("utf8", "ignore")
548 # You sometimes get a decoding error if the serial port gives
549 # you garbage data. This can happen, for instance, when
550 # the uart changes line speed during bootup.
551 #
552 # NOTE: I chose 'ignore' for decoding errors
553 # because I believe the most common use case is
554 # a user watching stdout from grabserial in a terminal
555 # window. You don't want to emit weird characters
556 # in that case. However, this will end up losing
557 # characters that can't be decoded. Another option
558 # is 'replace', with its own set of issues.
559 #
560 # Note that the exact data from the serial port is
561 # preserved in an output file (specified with the -o
562 # parameter), so you can use that to diagnose weird
563 # uart problems, if needed.
564
565 # curline is in unicode
566 curline += out_char
567 xline += x
568
569 # this is tricky! Enjoy.
570 if command_mode:
571 # check for data to suppress
572 if not cmd_done and cmd_index == len(curline)-1:
573 if curline[cmd_index] == command[cmd_index]:
574 cmd_index += 1
575 out_char = None
576 if cmd_index >= len(command):
577 cmd_done = True
578 else:
579 # mis-match, output partial match, if any
580 # FIXTHIS - only look at first line returned by port
581 # (maybe set cmd_done when first \n is detected??)
582 if cmd_index:
583 if not quiet:
584 outputfd.write(curline)
585 if out:
586 out.write(xline)
587 # we just wrote it out, no need to do it later
588 out_char = None
589 cmd_index = 0
590
591 if not quit_done and quit_index == len(curline)-1:
592 if curline[quit_index] == quitpat[quit_index]:
593 quit_index += 1
594 out_char = None
595 if quit_index >= len(quitpat):
596 quit_done = True
597 else:
598 # mis-match
599 if quit_index:
600 if not quiet:
601 outputfd.write(curline)
602 if out:
603 out.write(xline)
604 out_char = None
605 quit_index = 0
606
607 # FIXTHIS - should I buffer the output here??
608 if not quiet and out_char:
609 # x is a bytestr
610 outputfd.write(out_char)
611 if out and out_char:
612 # save bytestring data exactly as received from serial port
613 # (ie there is no 'decode' here)
614 out.write(x)
615
616 # watch for patterns
617 if inlinepat and not inlinetime and \
618 re.search(inlinepat, curline):
619 # inlinepat is in curline:
620 inlinetime = time.time()
621
622 # Exit the loop if quitpat matches
623 if quitpat and re.search(quitpat, curline):
624 stop_reason = "grabserial stopped because quit pattern '" + \
625 quitpat + "' was found"
626 break
627
628 if x == b"\n":
629 newline = 1
630 if basepat and re.match(basepat, curline):
631 basetime = linetime
632 elapsed = 0
633 prev1 = 0
634 curline = ""
635 xline = b""
636 sys.stdout.flush()
637 if out:
638 out.flush()
639
640 except serial.serialutil.SerialException:
641 # This is the exception which is raised when you unplug the USB
642 # UART. Applies to both python 2 and 3 on Linux and Windows.
643
644 stop_reason = "grabserial stopped due to a SerialException"
645
646 # We might get a Ctrl+C while we are sleeping, so catch that
647 try:
648 # Wait a second so we don't use excessive CPU to spin in a loop
649 # when the serial device is disconnected.
650 time.sleep(1)
651 except KeyboardInterrupt:
652 stop_reason = "grabserial stopped due to keyboard interrupt"
653
654 # An actual error, don't restart.
655 restart = False
656 break
657 except EnvironmentError:
658 stop_reason = "grabserial stopped due to some external error"
659
660 # An actual error. We don't want to restart the program in this
661 # case, so this function will return false.
662 restart = False
663 break
664 except KeyboardInterrupt:
665 stop_reason = "grabserial stopped due to keyboard interrupt"
666
667 # An actual error, don't restart.
668 restart = False
669 break
670
671 sd.close()
672 if inlinetime:
673 inlinetime_str = '%4.6f' % (inlinetime-basetime)
674 msg = u'\nThe inlinepat: "%s" was matched at %s\n' % \
675 (inlinepat, inlinetime_str)
676 if not quiet:
677 outputfd.write(msg)
678 outputfd.flush()
679 if out:
680 try:
681 out.write(msg.encode(sys.stdout.encoding))
682 except UnicodeEncodeError:
683 try:
684 out.write(msg.encode("utf8"))
685 except UnicodeEncodeError:
686 out.write(msg)
687 out.flush()
688
689 if out:
690 out.close()
691
692 vprint(stop_reason)
693
694 return restart
695
696
697 if __name__ == "__main__":
698 while True:
699 restart_requested = grab(sys.argv[1:])
700
701 if restart_requested:
702 vprint(
703 "Restarting %s\n" %
704 datetime.datetime.now().strftime("%H:%M:%S.%f"))
705 else:
706 break
707
708 # emacs custom variables for using tabs
709 # indent-tabs-mode: nil
710 # tab-width: 4