Skip to content

Commit

Permalink
Merge pull request #1 from lobis/do-not-open-on-initialization
Browse files Browse the repository at this point in the history
[feature] do not open serial ports on initialization
  • Loading branch information
lobis authored Feb 27, 2024
2 parents 6ff6a38 + c6315ad commit 52ffbbb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 92 deletions.
20 changes: 2 additions & 18 deletions src/vsr53/ThyrCommPackage.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from __future__ import annotations

from vsr53.AccessCodes import AccessCode as AC
from vsr53.Commands import Commands as CMD
from vsr53.DisplayModes import Units as Units
from vsr53.logger import log as log

# from DISPLAY import Orientation as Orientation


class ThyrCommPackage:
"""
Expand Down Expand Up @@ -136,16 +131,5 @@ def parse_answer(self, answer):
return answer

@staticmethod
def get_ascii_list(string):
return [ord(c) for c in (string)]


if __name__ == "__main__":
SENSOR_ADDRESS = 2
t_package = ThyrCommPackage(SENSOR_ADDRESS)
t_package.access_code = AC.WR_TX
t_package.cmd = CMD.Display_Unit
t_package.data = Units.MBAR
log.info(t_package.get_package())
log.info(t_package.get_string())
# print([hex(value) for value in t_package.get_package_ascii_list()])
def get_ascii_list(s: str):
return [ord(c) for c in s]
10 changes: 0 additions & 10 deletions src/vsr53/sys.py

This file was deleted.

86 changes: 26 additions & 60 deletions src/vsr53/vsr53.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
class VSR53(ABC):
@abstractmethod
def __init__(self):
self._port = None
self._serial = None
self._address = None

def open_communication(self):
opening_port_trials = 0
if self._port.isOpen():
self._port.flush()
if self._serial.isOpen():
self._serial.flush()
log.info("Port is Open!")
elif not self._port.isOpen() and opening_port_trials < 5:
elif not self._serial.isOpen() and opening_port_trials < 5:
log.info("Port closed, trying to open...")
self._port.open()
self._serial.open()
opening_port_trials += 1
self.open_communication()

Expand All @@ -37,8 +37,8 @@ def close_communication(self):
Closes communication with serial device
:return: None
"""
self._port.flush()
self._port.close()
self._serial.flush()
self._serial.close()
log.info("Closing communication with device")

def __enter__(self):
Expand Down Expand Up @@ -353,18 +353,18 @@ def _instruction_exchange(self, pack):
else:
log.error("BAD TRANSACTION")
fine_transaction = False
self._port.flush()
self._serial.flush()
pack.parse_answer(message)
if pack.access_code == AC.ERR_RX:
log.error(f"{ErrorMessages.MSG[pack.data]}")
return message

def _send_message(self, pack):
log.debug(f"TXin' this: {pack.get_string()}")
self._port.write(pack.get_package_ascii_list())
self._serial.write(pack.get_package_ascii_list())

def _receive_message(self):
message = self._port.readline()
message = self._serial.readline()
log.debug(f"RXin' this: {message}")
return message

Expand All @@ -381,60 +381,26 @@ def __init__(self, port: str, *, address: int = 1, baudrate: int = 9600):
:param address: Defined by the address switch mounted in the device from 1 to 16
:param baudrate: Baud rate for data transmission
"""
self._port = serial.rs485.RS485(
port,
baudrate=baudrate,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0.02,
)
self._serial = serial.rs485.RS485()
self._serial.port = port
self._serial.baudrate = baudrate
self._serial.parity = serial.PARITY_NONE
self._serial.stopbits = serial.STOPBITS_ONE
self._serial.bytesize = serial.EIGHTBITS
self._serial.timeout = 0.02
self._serial.rs485_mode = serial.rs485.RS485Settings()

self._port.rs485_mode = serial.rs485.RS485Settings()
self._address = address


class VSR53USB(VSR53):
def __init__(self, port: str, *, address: int = 1, baudrate: int = 9600):
self._port = serial.Serial(
port,
baudrate=baudrate,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0.02,
)
self._address = address
self._serial = serial.Serial()
self._serial.port = port
self._serial.baudrate = baudrate
self._serial.parity = serial.PARITY_NONE
self._serial.stopbits = serial.STOPBITS_ONE
self._serial.bytesize = serial.EIGHTBITS
self._serial.timeout = 0.02


if __name__ == "__main__":
import logging

from vsr53.sys import dev_tty

log.setLevel(logging.INFO)

sensor_address = 1
vacuum_sense = VSR53DL(dev_tty, address=sensor_address)
vacuum_sense.open_communication()

vacuum_sense.get_device_type()
vacuum_sense.get_product_name()
vacuum_sense.get_serial_number_device()
vacuum_sense.get_serial_number_head()
vacuum_sense.get_response_delay()
vacuum_sense.get_device_version()
vacuum_sense.get_firmware_version()
vacuum_sense.get_bootloader_version()
vacuum_sense.get_measurement_range()
vacuum_sense.get_measurement_value()
vacuum_sense.get_measurement_value_piezo()
vacuum_sense.get_measurement_value_pirani()
vacuum_sense.set_display_unit(Units.MBAR)
vacuum_sense.get_display_unit()
vacuum_sense.set_display_orientation(Orientation.NORMAL)
vacuum_sense.get_display_orientation()
vacuum_sense.get_relay_1_status()
vacuum_sense.get_relay_2_status()
vacuum_sense.get_operating_hours()
vacuum_sense.close_communication()
self._address = address
4 changes: 2 additions & 2 deletions test/test_vsr53dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from vsr53 import VSR53DL
from vsr53.DisplayModes import Units
from vsr53.sys import dev_tty


@pytest.fixture()
def vacuum_sensor():
port = "/dev/ttyUSB0"
sensor_address = 1
vacuum_sense = VSR53DL(dev_tty, address=sensor_address)
vacuum_sense = VSR53DL(port, address=sensor_address)
vacuum_sense.open_communication()
yield vacuum_sense
vacuum_sense.close_communication()
Expand Down
4 changes: 2 additions & 2 deletions test/test_vsr53usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import pytest

from vsr53 import VSR53USB
from vsr53.sys import dev_tty


@pytest.fixture()
def vacuum_sensor():
port = "/dev/ttyUSB0"
sensor_address = 1
vacuum_sense = VSR53USB(dev_tty, address=sensor_address)
vacuum_sense = VSR53USB(port, address=sensor_address)
vacuum_sense.open_communication()
yield vacuum_sense
vacuum_sense.close_communication()
Expand Down

0 comments on commit 52ffbbb

Please sign in to comment.