Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/internal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ here.
Serial handler
---------------

.. automodule:: pslab.serial_handler
.. automodule:: pslab.connection
:members:
:undoc-members:
:show-inheritance:
Expand Down
35 changes: 26 additions & 9 deletions pslab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pslab.instrument.logic_analyzer import LogicAnalyzer
from pslab.instrument.oscilloscope import Oscilloscope
from pslab.instrument.waveform_generator import WaveformGenerator, PWMGenerator
from pslab.serial_handler import SerialHandler
from pslab.connection import SerialHandler, autoconnect


def logic_analyzer(
Expand Down Expand Up @@ -222,6 +222,20 @@ def pwm(handler: SerialHandler, args: argparse.Namespace):
)


def _get_connected_handler(args: argparse.Namespace) -> SerialHandler:
"""Get a connected SerialHandler based on arguments."""
try:
if args.port:
handler = SerialHandler(port=args.port)
handler.connect()
else:
handler = autoconnect()
return handler
except Exception as e:
print(f"Error: Could not connect to PSLab device. Details: {e}")
sys.exit(1)


def main(args: argparse.Namespace):
"""Perform the given function on PSLab.

Expand All @@ -234,18 +248,21 @@ def main(args: argparse.Namespace):
install(args)
return

handler = SerialHandler(port=args.port)

if args.function == "flash":
flash(pslab.ScienceLab(args.port), args.hexfile)
return

if args.function == "collect":
collect(handler, args)
elif args.function == "wave":
wave(handler, args)
elif args.function == "pwm":
pwm(handler, args)
handler = _get_connected_handler(args)

try:
if args.function == "collect":
collect(handler, args)
elif args.function == "wave":
wave(handler, args)
elif args.function == "pwm":
pwm(handler, args)
finally:
handler.disconnect()


def get_parser() -> Tuple[argparse.ArgumentParser, argparse._SubParsersAction]:
Expand Down
2 changes: 1 addition & 1 deletion pslab/external/gas_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Callable, Union

from pslab import Multimeter
from pslab.serial_handler import SerialHandler
from pslab.connection import SerialHandler


class MQ135:
Expand Down
2 changes: 1 addition & 1 deletion pslab/external/hcsr04.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pslab.instrument.logic_analyzer import LogicAnalyzer
from pslab.instrument.waveform_generator import PWMGenerator
from pslab.serial_handler import SerialHandler
from pslab.connection import SerialHandler


class HCSR04:
Expand Down
2 changes: 1 addition & 1 deletion pslab/external/tcd1304.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from pslab import PWMGenerator
from pslab.instrument.waveform_generator import _get_wavelength
from pslab.protocol import MAX_SAMPLES
from pslab.serial_handler import SerialHandler
from pslab.connection import SerialHandler


class TCD1304:
Expand Down