diff options
author | Chris Liechti <cliechti@gmx.net> | 2016-08-12 21:04:37 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2016-08-12 21:04:37 +0200 |
commit | c9f8996d2cdaa676e7e99d102d0e2cc67bce9f59 (patch) | |
tree | 5803d62616cbfee8e93a282f21eec1ec87fced6a | |
parent | 1770e50b63b400d264c8dd40bf1d4e70860b3012 (diff) | |
download | pyserial-git-c9f8996d2cdaa676e7e99d102d0e2cc67bce9f59.tar.gz |
docs: mention context manager of Serial class
-rw-r--r-- | documentation/pyserial_api.rst | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/documentation/pyserial_api.rst b/documentation/pyserial_api.rst index a3417c2..f7216e4 100644 --- a/documentation/pyserial_api.rst +++ b/documentation/pyserial_api.rst @@ -479,6 +479,34 @@ Native ports .. versionadded:: 2.5 .. versionchanged:: 3.0 renamed from ``applySettingsDict`` + + This class can be used as context manager. The serial port is closed when + the context is left. + + .. method:: __enter__() + + :returns: Serial instance + + Returns the instance that was used in the ``with`` statement. + + Example: + + >>> with serial.serial_for_url(port) as s: + ... s.write(b'hello') + + Here no port argument is given, so it is not opened automatically: + + >>> with serial.Serial() as s: + ... s.port = ... + ... s.open() + ... s.write(b'hello') + + + .. method:: __exit__(exc_type, exc_val, exc_tb) + + Closes serial port. + + Platform specific methods. .. warning:: Programs using the following methods and attributes are not |