-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
tools/pyboard.py: ProcessToSerial cross-platform fix #8435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,13 +153,72 @@ def inWaiting(self): | |
else: | ||
return n_waiting | ||
|
||
# A thread that constantly reads from subp stdout and puts data into a queue (FIFO list) | ||
# When tested, it reads double '\r' which messes up the further code, so if double '\r' detected, skip one | ||
def read_stream(stream, q): | ||
data_prec = b"" | ||
while True: | ||
data = stream.read(1) | ||
if (data_prec == data) and (data_prec == b'\r'): | ||
continue | ||
else: | ||
q.put(data) | ||
data_prec = data | ||
|
||
class ProcessToSerial: | ||
"Execute a process and emulate serial connection using its stdin/stdout." | ||
class ProcessToSerialThreading: | ||
"Execute a process and emulate serial connection using its stdin/stdout. Communication with stdout is done through the read_stream thread" | ||
|
||
def __init__(self, cmd): | ||
def __init__(self, cmd): | ||
import subprocess | ||
|
||
# On Windows, preexec_fn parameter should be replaced by start_new_session | ||
self.subp = subprocess.Popen( | ||
cmd, | ||
bufsize=0, | ||
shell=True, | ||
start_new_session=True, | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
) | ||
|
||
# On Windows, select can not be used, a thread is used instead | ||
# that constantly reads from subp.stdout and completes a queue | ||
import threading | ||
import queue | ||
self.q = queue.Queue() | ||
self.t = threading.Thread(target=read_stream, args=(self.subp.stdout, self.q)) | ||
self.t.daemon=True # Makes thread exit when main script exits | ||
self.t.start() | ||
|
||
def close(self): | ||
import signal | ||
os.kill(self.subp.pid, signal.SIGTERM) | ||
|
||
def read(self, size=1): | ||
data = b"" | ||
# On Windows, data will be read from the queue that is completed by the thread | ||
i = 0 | ||
while i != size: | ||
data += self.q.get() | ||
i += 1 | ||
return data | ||
|
||
def write(self, data): | ||
self.subp.stdin.write(data) | ||
return len(data) | ||
|
||
def inWaiting(self): | ||
if self.q.empty(): | ||
return 0 | ||
else: | ||
return 1 | ||
|
||
class ProcessToSerialPosix: | ||
"Execute a process and emulate serial connection using its stdin/stdout. Using select to check if any data available on stdout" | ||
|
||
def __init__(self, cmd): | ||
import subprocess | ||
|
||
self.subp = subprocess.Popen( | ||
cmd, | ||
bufsize=0, | ||
|
@@ -168,7 +227,7 @@ def __init__(self, cmd): | |
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
) | ||
|
||
# Initially was implemented with selectors, but that adds Python3 | ||
# dependency. However, there can be race conditions communicating | ||
# with a particular child process (like QEMU), and selectors may | ||
|
@@ -185,7 +244,6 @@ def __init__(self, cmd): | |
|
||
def close(self): | ||
import signal | ||
|
||
os.killpg(os.getpgid(self.subp.pid), signal.SIGTERM) | ||
|
||
def read(self, size=1): | ||
|
@@ -258,7 +316,12 @@ def __init__( | |
self.in_raw_repl = False | ||
self.use_raw_paste = True | ||
if device.startswith("exec:"): | ||
self.serial = ProcessToSerial(device[len("exec:") :]) | ||
# Class selector based on select module platform dependancy (available on Unix, but not on Windows) | ||
import select | ||
if hasattr(select, "poll"): | ||
self.serial = ProcessToSerialPosix(device[len("exec:") :]) | ||
else: | ||
self.serial = ProcessToSerialThreading(device[len("exec:") :]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excuse me, I don't have much experience with Python. If I keep the ProcessToSerial class, how and where I could declare ProcessToSerialPosix and ProcessToSerialThreading ? I appreciate very much your help and indications. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class ProcessToSerialPosix:
...
class ProcessToSerialThreading:
...
if hasattr(select, "poll"):
ProcessToSerial = ProcessToSerialPosix
else:
ProcessToSerial = ProcessToSerialThreading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you very much for your time and help ! I didn't know you can do that in Python. I made a commit with these changes. |
||
elif device.startswith("execpty:"): | ||
self.serial = ProcessPtyToTerminal(device[len("qemupty:") :]) | ||
elif device and device[0].isdigit() and device[-1].isdigit() and device.count(".") == 3: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you understand why it reads double \r?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried to investigate the reason, but unfortunately I am still not able to understand why it reads double \r