Skip to content

fdobrovolny/RS485-relay-board

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RS485 relay board

Lightweight python library for interaction with R4D8B08 and R4D3B16

Example

from rs485_relay_board import RelayBoard


SLAVE_ADDR = 1
board = RelayBoard("/dev/ttyUSB0", SLAVE_ADDR)

board.open_all()

RelayBoard API

RelayBoard

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

__init__

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) or COM4 (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 to True to print the communication details

check_channel_number

def check_channel_number(channel: int)

Check if channel number is between 1 and self.channels.

Arguments:

  • channel: Number of channel.

Raises:

  • ValueError:

check_seconds

def check_seconds(seconds: int)

Check if seconds is between 0 and 255.

Arguments:

  • seconds: Whole number of seconds

open_relay

def open_relay(channel: int)

Open relay.

Arguments:

  • channel: Number of channel.

close_relay

def close_relay(channel: int)

Close relay

Arguments:

  • channel: Number of channel.

set_relay

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

toggle

def toggle(channel: int)

Toggle (Self-locking) relay

Arguments:

  • channel: Number of channel.

latch

def latch(channel: int)

Latch (Inter-locking) relay

Arguments:

  • channel: Number of channel.

momentary

def momentary(channel: int)

Momentary (Non-locking).

Close relay for 1s.

Arguments:

  • channel: Number of channel.

delay

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

open_all

def open_all()

Open all relays.

close_all

def close_all()

Close all relays.

read_relay

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

read_relays

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).

read_all_relays

def read_all_relays() -> Tuple[bool]

Read state of all relays.

Returns:

Tuple of booleans of length self.channels.

License

MIT