dhylands/upy-shell

rshell throws exception if started just after Pyboard plugged in

Opened this issue · 4 comments

This is arguably trivial, but if /dev/ttyACM0 has only just been instantiated when rshell is started the following occurs:

$ rshell
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 275, in open
    self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)
OSError: [Errno 16] Device or resource busy: '/dev/ttyACM0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/qnap2/python/rshell/rshell.py", line 1434, in <module>
    main()
  File "/mnt/qnap2/python/rshell/rshell.py", line 1355, in main
    pyb = pyboard.Pyboard(args.port, baudrate=args.baud)
  File "/mnt/qnap2/python/rshell/pyboard.py", line 45, in __init__
    self.serial = serial.Serial(serial_device, baudrate=baudrate, interCharTimeout=1)
  File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 278, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'

A further observation. This effect is PC/distro/timing dependent. On my laptop, if I start rshell and then plug in the pyboard, it initially reports that it is waiting for /dev/ttyACM0 and then throws the exception. On my desktop this scenario doesn't produce the fault which only occurs if I start rshell soon after plugging in the Pyboard.

@peterhinch If you're running linux, it would be interesting to see if usb-ser-mon.py behaves the same way. usb-ser-mon.py hooks in with udev to detect when serial ports come and go.
https://github.com/dhylands/usb-ser-mon It needs pyudev to be installed.

I started usb-ser-mon with the Pyboard connected (it had been connected for some time) and it detected it. I then unplugged the Pyboard and plugged it back in again. This was the outcome of the sequence described.

$ ./usb-ser-mon.py 
USB Serial device with vendor 'Micro_Python' serial '000000000011' connected @/dev/ttyACM0
Use Control-X to exit.
USB Serial device @/dev/ttyACM0 disconnected.

Waiting for USB Serial Device ...
USB Serial device with vendor 'Micro_Python' serial '000000000011' connected @/dev/ttyACM0
Use Control-X to exit.
Traceback (most recent call last):
                                    File "./usb-ser-mon.py", line 293, in main
                                                                                  debug=args.debug, echo=args.echo)
                                                                                                                     File "./usb-ser-mon.py", line 148, in usb_serial_mon
                                                       log(data, end='')
                                                                          File "./usb-ser-mon.py", line 28, in log
                                                                                                                      if str[-1] == '\r':
                   IndexError: string index out of range
                                                        [adminpete@axolotl]: ~/qnap/python/usb_ser_mon/usb-ser-mon
$ 

ok - I'll have to fix that - that looks like a logic error