Skip to content

tools/pyterm: catch serial.Exception when serial port is busy#10481

Merged
miri64 merged 1 commit intoRIOT-OS:masterfrom
aabadie:pr/tools/pyterm_exception
Nov 27, 2018
Merged

tools/pyterm: catch serial.Exception when serial port is busy#10481
miri64 merged 1 commit intoRIOT-OS:masterfrom
aabadie:pr/tools/pyterm_exception

Conversation

@aabadie
Copy link
Contributor

@aabadie aabadie commented Nov 27, 2018

Contribution description

This PR is fixing an exception not being catched when the serial port is not ready and one tries to open the terminal using pyterm (e.g make term).

This happens when opening a terminal right after flashing a board that has just been plugged.

Testing procedure

  • Plug the board
  • Build an application, let's say default:
make BOARD=samr21-xpro -C examples/default
  • Unplug the board, then re-plug it
  • Flash the firmware and open a terminal:
make BOARD=samr21-xpro -C examples/default flash term

Without this PR, the exception is not catched and the traceback is printed, which is not nice:

[...]
  39824	    508	   6044	  46376	   b528	/home/aabadie/softs/src/riot/RIOT/examples/default/bin/samr21-xpro/default.elf
/home/aabadie/softs/src/riot/RIOT/dist/tools/edbg/edbg  -t atmel_cm0p -b -v -p -f /home/aabadie/softs/src/riot/RIOT/examples/default/bin/samr21-xpro/default.bin
Debugger: ATMEL EDBG CMSIS-DAP ATML2127031800004957 01.1A.00FB (S)
Target: SAM R21G18A (Rev C)
Programming................................................................................................................................................................. done.
Verification................................................................................................................................................................. done.
/home/aabadie/softs/src/riot/RIOT/dist/tools/pyterm/pyterm -p "/dev/ttyACM0" -b "115200"
2018-11-27 16:53:31,554 - INFO # Connect to serial port /dev/ttyACM0
Traceback (most recent call last):
  File "/home/aabadie/softs/src/riot/RIOT/dist/tools/pyterm/pyterm", line 815, in <module>
    myshell.cmdloop("Welcome to pyterm!\nType '/exit' to exit.")
  File "/usr/lib/python2.7/cmd.py", line 109, in cmdloop
    self.preloop()
  File "/home/aabadie/softs/src/riot/RIOT/dist/tools/pyterm/pyterm", line 245, in preloop
    self.serial_connect()
  File "/home/aabadie/softs/src/riot/RIOT/dist/tools/pyterm/pyterm", line 611, in serial_connect
    self.ser = serial.Serial(port=self.port, dsrdtr=0, rtscts=0)
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'
/home/aabadie/softs/src/riot/RIOT/examples/default/../../Makefile.include:529: recipe for target 'term' failed
make: *** [term] Error 1
make: Leaving directory '/home/aabadie/softs/src/riot/RIOT/examples/default'

With this PR the serial.Exception is catched and only the error message is displayed:

[...]
   text	   data	    bss	    dec	    hex	filename
  39852	    508	   6044	  46404	   b544	/home/aabadie/softs/src/riot/RIOT/examples/default/bin/samr21-xpro/default.elf
/home/aabadie/softs/src/riot/RIOT/dist/tools/edbg/edbg  -t atmel_cm0p -b -v -p -f /home/aabadie/softs/src/riot/RIOT/examples/default/bin/samr21-xpro/default.bin
Debugger: ATMEL EDBG CMSIS-DAP ATML2127031800004957 01.1A.00FB (S)
Target: SAM R21G18A (Rev C)
Programming................................................................................................................................................................. done.
Verification................................................................................................................................................................. done.
/home/aabadie/softs/src/riot/RIOT/dist/tools/pyterm/pyterm -p "/dev/ttyACM0" -b "115200"
2018-11-27 16:55:19,143 - INFO # Connect to serial port /dev/ttyACM0
2018-11-27 16:55:19,143 - ERROR # Cannot connect to serial port /dev/ttyACM0: could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'
/home/aabadie/softs/src/riot/RIOT/examples/default/../../Makefile.include:529: recipe for target 'term' failed
make: *** [term] Error 1
make: Leaving directory '/home/aabadie/softs/src/riot/RIOT/examples/default'

Issues/PRs references

None

@aabadie aabadie added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Area: tools Area: Supplementary tools labels Nov 27, 2018
@aabadie aabadie requested a review from cladmi November 27, 2018 15:56
@aabadie
Copy link
Contributor Author

aabadie commented Nov 27, 2018

I just pushed an update that catch serial.Exception in a separate block. Otherwise the displayed message was a bit redundant:
ERROR # Cannot connect to serial port /dev/ttyACM0: could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0' instead of ERROR # could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@miri64
Copy link
Member

miri64 commented Nov 27, 2018

(please squash)

This can happen when opening a terminal right after flashing a board that has just been plugged
@aabadie aabadie force-pushed the pr/tools/pyterm_exception branch from 8c05d43 to 98413e4 Compare November 27, 2018 16:30
@aabadie
Copy link
Contributor Author

aabadie commented Nov 27, 2018

(please squash)

Just did. Thanks for reviewing!

@miri64
Copy link
Member

miri64 commented Nov 27, 2018

I've got an idea during the review, I put into a follow-up: #10482

@miri64 miri64 merged commit 2a2ef9b into RIOT-OS:master Nov 27, 2018
@aabadie aabadie added this to the Release 2019.01 milestone Dec 2, 2018
@aabadie aabadie deleted the pr/tools/pyterm_exception branch December 21, 2018 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants