Lightweight python library for interaction with R4D8B08 and R4D3B16
from rs485_relay_board import RelayBoard
SLAVE_ADDR = 1
board = RelayBoard("/dev/ttyUSB0", SLAVE_ADDR)
board.open_all()
class RelayBoard()
Class for interaction with relay boards over modbus.
By default, the relay is in state "closed" / LOW.
"Closed" state means that COM is connected to NC and not to NO.
"Open" state means that COM is connected to NO and not to NC and is indicated by LED.
For this library state True
means "open" state.
e.g. Open = HIGH to relay, Closed = LOW to relay
def __init__(port: str,
slaveaddress: int,
channels: int = 16,
baudrate: int = 9600,
read_timeout: float = 0.2,
close_port_after_each_call: bool = False,
debug: bool = False)
Setup relay board.
Arguments:
port
: The serial port name, for example/dev/ttyUSB0
(Linux),/dev/tty.usbserial
(OS X) orCOM4
(Windows).slaveaddress
: Slave address in the range 0 to 247 (use decimal numbers, not hex).channels
: Number of channels of the relay board.baudrate
: Baudrate to use.read_timeout
: Read timeout after each command.close_port_after_each_call
: If the serial port should be closed after each call to the board.debug
: Set this toTrue
to print the communication details
def check_channel_number(channel: int)
Check if channel number is between 1 and self.channels
.
Arguments:
channel
: Number of channel.
Raises:
ValueError
:
def check_seconds(seconds: int)
Check if seconds is between 0 and 255.
Arguments:
seconds
: Whole number of seconds
def open_relay(channel: int)
Open relay.
Arguments:
channel
: Number of channel.
def close_relay(channel: int)
Close relay
Arguments:
channel
: Number of channel.
def set_relay(channel: int, state: bool)
Set state of given relay.
True means "Open" / High to relay state.
Arguments:
channel
: Number of channel.state
: Bool state
def toggle(channel: int)
Toggle (Self-locking) relay
Arguments:
channel
: Number of channel.
def latch(channel: int)
Latch (Inter-locking) relay
Arguments:
channel
: Number of channel.
def momentary(channel: int)
Momentary (Non-locking).
Close relay for 1s.
Arguments:
channel
: Number of channel.
def delay(channel: int, seconds: int)
Delay
Open relay for s.
Works even if relay is currently opened.
Arguments:
channel
: Number of channel.seconds
: Number of seconds to close the relay for
def open_all()
Open all relays.
def close_all()
Close all relays.
def read_relay(channel: int) -> bool
Read state of a relay.
True means "Open" / High to relay state.
Arguments:
channel
: Number of channel.
Returns:
Bool
def read_relays(start_channel: int, length: int) -> Tuple[bool]
Read state of n relays.
Arguments:
start_channel
: Number of channel to start reading from.length
: Number of relays to read.
Returns:
Tuple of booleans of length n(length).
def read_all_relays() -> Tuple[bool]
Read state of all relays.
Returns:
Tuple of booleans of length self.channels
.
MIT