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

Connect using a URL for maximum flexibility #7

Merged
merged 1 commit into from
Jul 17, 2024
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
16 changes: 10 additions & 6 deletions SerialLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,29 @@ def __init__(self, unicode='utf-8'):
self.buffer = BytesIO()

@keyword("Connect")
def connect_to_serial(self, device: str, baudrate: int) -> serial.Serial:
def connect_to_serial_url(self, url: str, baudrate: int) -> serial.Serial:
"""
Connects to a serial device.

``device`` - The device name or a device number.
``url`` - The device name or URL. see: https://pythonhosted.org/pyserial/url_handlers.html#urls

``baudrate`` - The baud rate to use for communication.

NOTE: baudrate is kept for backwords compatibility.
=== Example ===
| Connect | <device> | <baudrate>
| Connect | /dev/ttyUSB0 | 115200
| ConnectUrl | <device> | <baudrate>
| ConnectUrl | /dev/ttyUSB0 | 115200
| ConnectUrl | spy:///dev/ttyUSB0/file=dump-comms.txt | 115200

=== Returns ===
The connected serial device object.
"""
try:
self.device = serial.Serial(device, baudrate=baudrate)
self.device = serial.serial_for_url(url, do_not_open=True)
self.device.baudrate = baudrate
self.device.open()
except SerialException as exc:
raise PySerialError(f"Failed to connect {device}: {exc}") from exc
raise PySerialError(f"Failed to connect {url}: {exc}") from exc
return self.device

@keyword("Disconnect")
Expand Down