44import logging
55import sys
66import time
7+ from io import TextIOWrapper
78
89from typing import List , Optional
910
@@ -44,7 +45,10 @@ def __init__(self, devPath: Optional[str]=None, debugOut=None, noProto: bool=Fal
4445
4546 logger .debug (f"Connecting to { self .devPath } " )
4647
47- self ._set_hupcl_with_termios ()
48+ if sys .platform != "win32" :
49+ with open (self .devPath , encoding = "utf8" ) as f :
50+ self ._set_hupcl_with_termios (f )
51+ time .sleep (0.1 )
4852
4953 self .stream = serial .Serial (
5054 self .devPath , 115200 , exclusive = True , timeout = 0.5 , write_timeout = 0
@@ -56,21 +60,17 @@ def __init__(self, devPath: Optional[str]=None, debugOut=None, noProto: bool=Fal
5660 self , debugOut = debugOut , noProto = noProto , connectNow = connectNow , noNodes = noNodes
5761 )
5862
59- def _set_hupcl_with_termios (self ):
63+ def _set_hupcl_with_termios (self , f : TextIOWrapper ):
6064 """first we need to set the HUPCL so the device will not reboot based on RTS and/or DTR
6165 see https://github.com/pyserial/pyserial/issues/124
6266 """
6367 if sys .platform == "win32" :
6468 return
6569
66- with open (self .devPath , encoding = "utf8" ) as f :
67- import termios # pylint: disable=C0415,E0401
68- attrs = termios .tcgetattr (f )
69- attrs [2 ] = attrs [2 ] & ~ termios .HUPCL
70- termios .tcsetattr (f , termios .TCSAFLUSH , attrs )
71- f .close ()
72-
73- time .sleep (0.1 )
70+ import termios # pylint: disable=C0415,E0401
71+ attrs = termios .tcgetattr (f )
72+ attrs [2 ] = attrs [2 ] & ~ termios .HUPCL
73+ termios .tcsetattr (f , termios .TCSAFLUSH , attrs )
7474
7575 def __repr__ (self ):
7676 rep = f"SerialInterface(devPath={ self .devPath !r} "
0 commit comments