@@ -583,8 +583,9 @@ class SerialIntercept:
583
583
584
584
585
585
class PyboardExtended (pyboard .Pyboard ):
586
- def __init__ (self , * args , ** kwargs ):
587
- super ().__init__ (* args , ** kwargs )
586
+ def __init__ (self , dev , * args , ** kwargs ):
587
+ super ().__init__ (dev , * args , ** kwargs )
588
+ self .device_name = dev
588
589
self .mounted = False
589
590
590
591
def enter_raw_repl_without_soft_reset (self ):
@@ -652,22 +653,6 @@ class PyboardExtended(pyboard.Pyboard):
652
653
self .mounted = False
653
654
654
655
655
- def find_serial_device (dev ):
656
- ports = serial .tools .list_ports .comports ()
657
- if dev is None :
658
- for dev in device_shortcuts .values ():
659
- if any (p .device == dev for p in ports ):
660
- return dev
661
- print ("no device found" )
662
- sys .exit (1 )
663
- else :
664
- dev = device_shortcuts .get (dev , dev )
665
- if any (p .device == dev for p in ports ):
666
- return dev
667
- print (f"{ dev } could not be accessed" )
668
- sys .exit (1 )
669
-
670
-
671
656
class ConsolePosix :
672
657
def __init__ (self ):
673
658
self .infd = sys .stdin .fileno ()
@@ -814,6 +799,33 @@ else:
814
799
VT_ENABLED = False
815
800
816
801
802
+ def do_connect (args ):
803
+ try :
804
+ if args and (args [0 ] == "connect" or args [0 ] in device_shortcuts ):
805
+ if args [0 ] == "connect" :
806
+ args .pop (0 )
807
+ dev = args .pop (0 )
808
+ dev = device_shortcuts .get (dev , dev )
809
+ return PyboardExtended (dev , baudrate = 115200 )
810
+ else :
811
+ # auto-detect and auto-connect
812
+ ports = serial .tools .list_ports .comports ()
813
+ for dev in device_shortcuts .values ():
814
+ if any (p .device == dev for p in ports ):
815
+ try :
816
+ return PyboardExtended (dev , baudrate = 115200 )
817
+ except pyboard .PyboardError as er :
818
+ if not er .args [0 ].startswith ("failed to access" ):
819
+ raise er
820
+ raise pyboard .PyboardError ("no device found" )
821
+ except pyboard .PyboardError as er :
822
+ msg = er .args [0 ]
823
+ if msg .startswith ("failed to access" ):
824
+ msg += " (it may be in use by another program)"
825
+ print (msg )
826
+ sys .exit (1 )
827
+
828
+
817
829
def do_filesystem (pyb , args ):
818
830
def _list_recursive (files , path ):
819
831
if os .path .isdir (path ):
@@ -892,7 +904,7 @@ def do_repl_main_loop(pyb, console_in, console_out_write, file_to_inject):
892
904
console_out_write (b"[%02x]" % ord (c ))
893
905
894
906
895
- def do_repl (pyb , dev , args ):
907
+ def do_repl (pyb , args ):
896
908
if len (args ) and args [0 ] == "--capture" :
897
909
args .pop (0 )
898
910
capture_file = args .pop (0 )
@@ -901,7 +913,7 @@ def do_repl(pyb, dev, args):
901
913
902
914
file_to_inject = args .pop (0 ) if len (args ) else None
903
915
904
- print ("Connected to MicroPython at %s" % dev )
916
+ print ("Connected to MicroPython at %s" % pyb . device_name )
905
917
print ("Use Ctrl-] to exit this shell" )
906
918
if capture_file is not None :
907
919
print ('Capturing session to file "%s"' % capture_file )
@@ -946,24 +958,7 @@ def execbuffer(pyb, buf, follow):
946
958
def main ():
947
959
args = sys .argv [1 :]
948
960
949
- if args and args [0 ] in device_shortcuts :
950
- dev = find_serial_device (args .pop (0 ))
951
- elif args and args [0 ] == "connect" :
952
- args .pop (0 )
953
- dev = find_serial_device (args .pop (0 ))
954
- else :
955
- # auto-detect and auto-connect
956
- dev = find_serial_device (None )
957
-
958
- try :
959
- pyb = PyboardExtended (dev , baudrate = 115200 )
960
- except pyboard .PyboardError as er :
961
- msg = er .args [0 ]
962
- if msg .startswith ("failed to access" ):
963
- msg += " (it may be in use by another program)"
964
- print (msg )
965
- sys .exit (1 )
966
-
961
+ pyb = do_connect (args )
967
962
in_raw_repl = False
968
963
did_action = False
969
964
@@ -1018,7 +1013,7 @@ def main():
1018
1013
elif cmd == "fs" :
1019
1014
do_filesystem (pyb , args )
1020
1015
elif cmd == "repl" :
1021
- do_repl (pyb , dev , args )
1016
+ do_repl (pyb , args )
1022
1017
elif cmd == "run" :
1023
1018
follow = True
1024
1019
if args [0 ] == "--no-follow" :
@@ -1039,7 +1034,7 @@ def main():
1039
1034
if in_raw_repl :
1040
1035
pyb .exit_raw_repl ()
1041
1036
in_raw_repl = False
1042
- do_repl (pyb , dev , args )
1037
+ do_repl (pyb , args )
1043
1038
finally :
1044
1039
if pyb is not None :
1045
1040
if pyb .mounted :
0 commit comments