#153 Preserve serial port config


It would be nice if there was a way to save the serial port configuration when opening it, and restoring it when closing it. My situation is that I have a python app running on a Gumstix using the serial console. It is intended to communicate with the second serial port, but for testing, I let it use the serial console port. When I terminate the program, I'm left with my serial port still in raw mode, and I have to blindly type "stty sane" to recover, and then reload my key assignments. It would be nice if I didn't have to do this.

One way to implement this would be to have the open() method return an object (perhaps a dictionary) that contains all the settings, and provide another method for applying all the settings, which the user could call before closing. If that's exposing too much to the user, the settings could be stored internally, along with an attribute flag that tells whether the settings should be restored on close.

What should be saved/restored should at least be everything changed on open. If it used a dictionary to support everything "stty", "setserial" and "bind" supports under Linux (and whatever smaller set of attributes Windows supports), then it would make pyserial useful as a general purpose serial port configurator.


  • Chris Liechti

    Chris Liechti - 2013-10-17
    • assigned_to: Chris Liechti
    • Group: v1.0 (example) --> v2.6
  • Chris Liechti

    Chris Liechti - 2013-10-17

    I understand the problem, but I'm not sure if that is really that useful.

    Generally, only one program at a time should use the serial port, otherwise strange things may happen. I also recommend that every program that uses the serial port also configures it. I see there is a class of programs that do not configure the port, but i guess it is a really rare use case to run one of those on the same port as a pySerial based application.

    In your specific case, wouln't it also be an option to have a mode that sends the data via sys.stdin/stdout instead of the serial port for testing purposes?

  • Chris Liechti

    Chris Liechti - 2014-08-04
    • status: open --> closed
  • Chris Liechti

    Chris Liechti - 2014-08-04

    Saving the complete serial port settings before anything is changed would be a larger change to pySerial.

    After the port is open there are getSettingsDict() / applySettingsDict() to save and restore settings (e.g. if the program temporarily wants to change some settings), but i see that this is of no use for the problem in the description.

    For this problem it should be possible to make a workaround when starting the python script: run "stty --save" before it and then stty again with the provided settings. A small wrapper shell script could handle this. e.g. this one, all arguments are forwarded to python:

    PROPS=$(stty --save)
    python "$@"
    stty $PROPS

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks