File tree Expand file tree Collapse file tree 1 file changed +44
-1
lines changed
Expand file tree Collapse file tree 1 file changed +44
-1
lines changed Original file line number Diff line number Diff line change 2222
2323import logging
2424from types import TracebackType
25- from typing import Literal
25+ from typing import Literal , Generator
26+ from contextlib import contextmanager
2627
2728from serial import (
2829 Serial ,
@@ -412,3 +413,45 @@ def exchange(self, cmd: str) -> str:
412413 """
413414 self .send (cmd )
414415 return self .receive ()
416+
417+ @contextmanager
418+ def timeout_override (self , timeout : int ) -> Generator [None , None , None ]:
419+ """
420+ Context manager that temporarily overrides connection parameters.
421+
422+ Parameters
423+ ----------
424+ timeout : int
425+ Temporary timeout in seconds.
426+
427+ Returns
428+ -------
429+ Generator
430+ Context manager generator object.
431+
432+ Example
433+ -------
434+
435+ >>> from serial import Serial
436+ >>> from geocompy.communication import SerialConnection
437+ >>>
438+ >>> port = Serial("COM1", timeout=5)
439+ >>> with SerialConnection(port) as com:
440+ ... # normal operation
441+ ...
442+ ... # potentially long operation
443+ ... with com.timeout_override(20):
444+ ... answer = com.exchange("message")
445+ ...
446+ ... # continue normal operation
447+ ...
448+ """
449+ saved_timeout = self ._port .timeout
450+
451+ try :
452+ if timeout is not None :
453+ self ._port .timeout = timeout
454+ yield
455+ finally :
456+ if timeout is not None :
457+ self ._port .timeout = saved_timeout
You can’t perform that action at this time.
0 commit comments