Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad0f34e

Browse files
committed
mpremote: Fix disconnect handling on Windows and Linux.
- Handle SerialException on Windows when device disconnects- Print clean 'device disconnected' message instead of stack trace- Fix terminal formatting issues on Linux after disconnect- Return disconnected state after console cleanup to avoid terminal issuesThis ensures proper disconnect messages on both platforms withoutshowing confusing error traces to users.Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent45e4deb commitad0f34e

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

‎tools/mpremote/mpremote/console.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,19 @@ def inWaiting(self):
9696
return1ifself.ctrl_cormsvcrt.kbhit()else0
9797

9898
defwaitchar(self,pyb_serial):
99-
whilenot (self.inWaiting()orpyb_serial.inWaiting()):
100-
time.sleep(0.01)
99+
importserial
100+
101+
whileTrue:
102+
try:
103+
ifself.inWaiting()orpyb_serial.inWaiting():
104+
break
105+
time.sleep(0.01)
106+
exceptserial.SerialExceptionase:
107+
if"ClearCommError failed"instr(e):
108+
# Device disconnected on Windows
109+
raiseserial.SerialException("Device disconnected")
110+
else:
111+
raise
101112

102113
defreadchar(self):
103114
ifself.ctrl_c:

‎tools/mpremote/mpremote/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,11 @@ def main():
574574
# If no commands were "actions" then implicitly finish with the REPL
575575
# using default args.
576576
ifstate.run_repl_on_completion():
577-
do_repl(state,argparse_repl().parse_args([]))
577+
disconnected=do_repl(state,argparse_repl().parse_args([]))
578+
579+
# Handle disconnection message
580+
ifdisconnected:
581+
print("\ndevice disconnected")
578582

579583
return0
580584
exceptCommandErrorase:

‎tools/mpremote/mpremote/repl.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
from .transportimportTransportError
44

5+
importserial
6+
57

68
defdo_repl_main_loop(
79
state,console_in,console_out_write,*,escape_non_printable,code_to_inject,file_to_inject
810
):
11+
disconnected=False
912
whileTrue:
10-
console_in.waitchar(state.transport.serial)
11-
c=console_in.readchar()
13+
try:
14+
console_in.waitchar(state.transport.serial)
15+
c=console_in.readchar()
16+
exceptserial.SerialExceptionase:
17+
if"Device disconnected"instr(e)or"ClearCommError failed"instr(e):
18+
disconnected=True
19+
break
20+
else:
21+
raise
1222
ifc:
1323
ifcin (b"\x1d",b"\x18"):# ctrl-] or ctrl-x, quit
1424
break
@@ -35,8 +45,15 @@ def do_repl_main_loop(
3545
n=state.transport.serial.inWaiting()
3646
exceptOSErroraser:
3747
ifer.args[0]==5:# IO error, device disappeared
38-
print("devicedisconnected")
48+
disconnected=True
3949
break
50+
exceptExceptionaser:
51+
# Handle Windows serial exception
52+
ifisinstance(er,serial.SerialException)and"ClearCommError failed"instr(er):
53+
disconnected=True
54+
break
55+
else:
56+
raise
4057

4158
ifn>0:
4259
dev_data_in=state.transport.serial.read(n)
@@ -53,6 +70,8 @@ def do_repl_main_loop(
5370
console_data_out=dev_data_in
5471
console_out_write(console_data_out)
5572

73+
returndisconnected
74+
5675

5776
defdo_repl(state,args):
5877
state.ensure_friendly_repl()
@@ -85,8 +104,9 @@ def console_out_write(b):
85104
capture_file.write(b)
86105
capture_file.flush()
87106

107+
disconnected=False
88108
try:
89-
do_repl_main_loop(
109+
disconnected=do_repl_main_loop(
90110
state,
91111
console,
92112
console_out_write,
@@ -98,3 +118,5 @@ def console_out_write(b):
98118
console.exit()
99119
ifcapture_fileisnotNone:
100120
capture_file.close()
121+
122+
returndisconnected

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp