Skip to content
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

trunner: phoenixd usb lookup #228

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions trunner/tools/phoenix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
import signal
import threading
import time
from contextlib import contextmanager

import pexpect

from trunner.harness import ProcessError
from serial.tools import list_ports
from contextlib import contextmanager
from .common import add_output_to_exception


def wait_for_dev(port, timeout=0):
def wait_for_vid_pid(vid: int, pid: int, timeout=0):
"""wait for connected usb serial device with required vendor & product id"""

asleep = 0
found_ports = []

# naive wait for dev
while not os.path.exists(port):
while not found_ports:
time.sleep(0.01)
asleep += 0.01

maska989 marked this conversation as resolved.
Show resolved Hide resolved
found_ports = [port for port in list_ports.comports() if port.pid == pid and port.vid == vid]

if len(found_ports) > 1:
raise Exception(
"More than one plo port was found! Maybe more than one device is connected? Hint used to find port:"
f"{vid:04x}:{pid:04x}"
)

if timeout and asleep >= timeout:
raise TimeoutError
raise TimeoutError(f"Couldn't find plo USB device with vid/pid: '{vid:04x}:{pid:04x}'")

return found_ports[0].device


class PsuError(ProcessError):
Expand Down Expand Up @@ -70,8 +83,9 @@ 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, vid=0x16F9, pid=0x0003, cwd=".", directory="."):
self.vid = vid
self.pid = pid
self.proc = None
self.reader_thread = None
self.cwd = cwd
Expand Down Expand Up @@ -101,9 +115,9 @@ def _reader(self):

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(self.vid, self.pid, timeout=10)
except (TimeoutError, Exception) as exc:
raise PhoenixdError(str(exc)) from exc

# Use pexpect.spawn to run a process as PTY, so it will flush on a new line
self.proc = pexpect.spawn(
Expand Down
Loading