diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2009-08-10 00:55:46 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2009-08-10 00:55:46 +0000 |
commit | 4065dce14f8be509daf326b3977d39339e2d3734 (patch) | |
tree | a91fdf4570275501dacce941c85ee8b5d4b7761c | |
parent | c63b3c06e6aff014eaacf6e0dcf0aad4e790316f (diff) | |
download | pyserial-git-4065dce14f8be509daf326b3977d39339e2d3734.tar.gz |
add getSettingsDict and applySettingsDict
-rw-r--r-- | pyserial/documentation/pyserial_api.rst | 24 | ||||
-rw-r--r-- | pyserial/serial/serialutil.py | 17 |
2 files changed, 41 insertions, 0 deletions
diff --git a/pyserial/documentation/pyserial_api.rst b/pyserial/documentation/pyserial_api.rst index fbe0e9d..8f413f7 100644 --- a/pyserial/documentation/pyserial_api.rst +++ b/pyserial/documentation/pyserial_api.rst @@ -392,6 +392,30 @@ Native ports .. versionadded:: 2.5 + .. method:: getSettingsDict() + + :return: a dictionary with current port settings. + + Get a dictionary with port settings. This is useful to backup the + current settings so that a later point in time they can be restored + using :meth:`applySettingsDict`. + + Note that control lines (RTS/DTR) are part of the settings. + + .. versionadded:: 2.5 + + .. method:: applySettingsDict(d) + + :param d: a dictionary with port settings. + + Applies a dictionary that was created by :meth:`getSettingsDict`. Only + changes are applied and when a key is missing it means that the setting + stays unchanged. + + Note that control lines (RTS/DTR) are not changed. + + .. versionadded:: 2.5 + .. note:: diff --git a/pyserial/serial/serialutil.py b/pyserial/serial/serialutil.py index 2a3253c..db8f645 100644 --- a/pyserial/serial/serialutil.py +++ b/pyserial/serial/serialutil.py @@ -399,6 +399,23 @@ class SerialBase(object): interCharTimeout = property(getInterCharTimeout, setInterCharTimeout, doc="Inter-character timeout setting for read()") + # - - - - - - - - - - - - - - - - - - - - - - - - + + _SETTINGS = ('baudrate', 'bytesize', 'parity', 'stopbits', 'xonxoff', + 'dsrdtr', 'rtscts', 'timeout', 'writeTimeout', 'interCharTimeout') + + def getSettingsDict(self): + """Get current port settings as a dictionary. For use with + applySettingsDict""" + return dict([(key, getattr(self, '_'+key)) for key in self._SETTINGS]) + + def applySettingsDict(self, d): + """apply stored settings from a dictionary returned from + getSettingsDict. it's allowed to delete keys from the dictionary. these + values will simply left unchanged.""" + for key in self._SETTINGS: + if d[key] != getattr(self, '_'+key): # check against internal "_" value + setattr(self, key, d[key]) # set non "_" value to use properties write function # - - - - - - - - - - - - - - - - - - - - - - - - |