Skip to content

Commit

Permalink
phoenixd: change the way how plo USB connections are handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
maska989 committed Jul 20, 2023
1 parent 6c0ba39 commit 3a1f379
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions trunner/tools/phoenix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import signal
import threading
import time

from serial.tools import list_ports


from contextlib import contextmanager

import pexpect


from trunner.harness import ProcessError
from .common import add_output_to_exception

Expand All @@ -22,6 +27,23 @@ def wait_for_dev(port, timeout=0):
raise TimeoutError


def wait_for_vid_pid(port_hint: str, timeout=0):
asleep = 0
port = None

while port is None:
time.sleep(0.01)
asleep += 0.01

for p in list_ports.grep(port_hint):
port = p

if timeout and asleep >= timeout:
raise PhoenixdError(f"Couldn't find USB device with vid/pid: '{port_hint}'. Timeout.") from TimeoutError

return port.device


class PsuError(ProcessError):
name = "PSU"

Expand Down Expand Up @@ -70,8 +92,7 @@ class PhoenixdError(ProcessError):
class Phoenixd:
"""Handler for phoenixd process"""

def __init__(self, port="/dev/serial/by-id/usb-Phoenix_Systems_plo_CDC_ACM-if00", cwd=".", directory="."):
self.port = port
def __init__(self, cwd=".", directory="."):
self.proc = None
self.reader_thread = None
self.cwd = cwd
Expand Down Expand Up @@ -100,11 +121,7 @@ def _reader(self):
self.proc.expect(pexpect.EOF, timeout=None)

def _run(self):
try:
wait_for_dev(self.port, timeout=10)
except TimeoutError as exc:
raise PhoenixdError(f"couldn't find {self.port}") from exc

self.port = wait_for_vid_pid("16f9:0003", timeout=10)
# Use pexpect.spawn to run a process as PTY, so it will flush on a new line
self.proc = pexpect.spawn(
"phoenixd",
Expand Down

0 comments on commit 3a1f379

Please sign in to comment.