From c9f8996d2cdaa676e7e99d102d0e2cc67bce9f59 Mon Sep 17 00:00:00 2001 From: Chris Liechti Date: Fri, 12 Aug 2016 21:04:37 +0200 Subject: docs: mention context manager of Serial class --- documentation/pyserial_api.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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 -- cgit v1.2.1