RevPiModIO Classes

Main classes for managing Revolution Pi hardware.

RevPiModIO

class revpimodio2.modio.RevPiModIO(autorefresh=False, monitoring=False, syncoutputs=True, procimg=None, configrsc=None, simulator=False, debug=True, replace_io_file=None, shared_procimg=False)[source]

Bases: object

Class for managing the piCtory configuration.

This class takes over the entire configuration from piCtory and loads the devices and IOs. It takes over exclusive management of the process image and ensures that the data is synchronized. If only individual devices are to be controlled, use RevPiModIOSelected() and pass a list with device positions or device names during instantiation.

Main class for managing all devices and IOs from the piCtory configuration.

This class manages the complete piCtory configuration and loads all devices and IOs. It handles exclusive management of the process image and ensures data synchronization.

Constructor Parameters:

Parameters:
  • autorefresh (bool) – Automatically sync process image (recommended: True)

  • monitoring (bool) – Read-only mode for supervision (no writes)

  • syncoutputs (bool) – Load current output values on initialization

  • debug (bool) – Enable detailed error messages and logging

Key Attributes:

Access to all configured inputs/outputs

Access to RevPi Core values (LEDs, status)

Access to specific devices by name

Update frequency in milliseconds

Threading event for clean shutdown

Count of read/write failures

Exception threshold (0 = disabled)

Example:

import revpimodio2

# Initialize with auto-refresh
rpi = revpimodio2.RevPiModIO(autorefresh=True)

# Access IOs
if rpi.io.button.value:
    rpi.io.led.value = True

# Clean shutdown
rpi.exit()
__init__(autorefresh=False, monitoring=False, syncoutputs=True, procimg=None, configrsc=None, simulator=False, debug=True, replace_io_file=None, shared_procimg=False)[source]

Instantiates the basic functions.

Parameters:
  • autorefresh – If True, add all devices to autorefresh

  • monitoring – Inputs and outputs are read, never written

  • syncoutputs – Read currently set outputs from process image

  • procimg – Alternative path to process image

  • configrsc – Alternative path to piCtory configuration file

  • simulator – Loads the module as simulator and swaps IOs

  • debug – Output all warnings including cycle problems

  • replace_io_file – Load replace IO configuration from file

  • shared_procimg – Share process image with other processes, this could be insecure for automation

core
app
device
io
summary
exitsignal
__del__()[source]

Destroys all classes to clean up.

__enter__()[source]

Context manager entry (deprecated).

Deprecated since version Use: with revpi.io: or with revpi.device.my_device: instead.

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit (deprecated).

Writes process image and performs cleanup.

autorefresh_all() None[source]

Sets all devices to autorefresh function.

cleanup() None[source]

Terminates autorefresh and all threads.

cycleloop(func, cycletime=50, blocking=True)[source]

Starts the cycle loop.

The current program thread is “trapped” here until .exit() is called. After each update of the process image, it executes the passed function “func” and processes it. During execution of the function, the process image is not further updated. The inputs retain their current value until the end. Set outputs are written to the process image after the function run completes.

The cycle loop is left when the called function returns a value not equal to None (e.g. return True), or by calling .exit().

NOTE: The refresh time and the runtime of the function must not exceed the set autorefresh time or passed cycletime!

The cycletime parameter sets the desired cycle time of the passed function. The default value is 50 milliseconds, in which the process image is read, the passed function is executed, and the process image is written.

Parameters:
  • func – Function to be executed

  • cycletime – Cycle time in milliseconds - default 50 ms

  • blocking – If False, the program does NOT block here

Returns:

None or the return value of the cycle function

exit(full=True) None[source]

Terminates mainloop() and optionally autorefresh.

If the program is in mainloop(), calling exit() returns control to the main program.

The full parameter defaults to True and removes all devices from autorefresh. The thread for process image synchronization is then stopped and the program can be terminated cleanly.

Parameters:

full – Also removes all devices from autorefresh

export_replaced_ios(filename='replace_ios.conf') None[source]

Exports replaced IOs of this instance.

Exports all replaced IOs that were created with .replace_io(…). The file can be used, for example, for RevPiPyLoad to transfer data in the new formats via MQTT or to view with RevPiPyControl.

@param filename Filename for export file

get_jconfigrsc() dict[source]

Loads the piCtory configuration and creates a <class ‘dict’>.

Returns:

<class ‘dict’> of the piCtory configuration

handlesignalend(cleanupfunc=None) None[source]

Manage signal handler for program termination.

When this function is called, RevPiModIO takes over the SignalHandler for SIGINT and SIGTERM. These are received when the operating system or the user wants to cleanly terminate the control program.

The optional function “cleanupfunc” is executed last after the last reading of the inputs. Outputs set there are written one last time after the function completes. This is intended for cleanup work, such as switching off the LEDs on the RevPi-Core.

After receiving one of the signals once and terminating the RevPiModIO threads / functions, the SignalHandler are released again.

Parameters:

cleanupfunc – Function to be executed after termination

mainloop(blocking=True) None[source]

Starts the mainloop with event monitoring.

The current program thread is “trapped” here until RevPiDevicelist.exit() is called (unless blocking=False). It runs through the event monitoring and checks for changes of registered IOs with an event. If a change is detected, the program executes the associated functions in sequence.

If the parameter “blocking” is specified as False, this activates event monitoring and does NOT block the program at the point of call. Well suited for GUI programming when events from the RevPi are needed but the program should continue to execute.

Parameters:

blocking – If False, the program does NOT block here

readprocimg(device=None) bool[source]

Read all inputs of all/one device from the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

resetioerrors() None[source]

Resets current IOError counter to 0.

setdefaultvalues(device=None) None[source]

All output buffers are set to the piCtory default values.

Parameters:

device – Only apply to single device

syncoutputs(device=None) bool[source]

Read all currently set outputs in the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

writeprocimg(device=None) bool[source]

Write all outputs of all devices to the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

property debug: bool

Returns the status of the debug flag.

Returns:

Status of the debug flag

property configrsc: str

Getter function.

Returns:

Path of the used piCtory configuration

property cycletime: int

Returns the refresh rate in ms of the process image synchronization.

Returns:

Milliseconds

property ioerrors: int

Getter function.

Returns:

Current number of counted errors

property length: int

Getter function.

Returns:

Length in bytes of the devices

property maxioerrors: int

Getter function.

Returns:

Number of allowed errors

property monitoring: bool

Getter function.

Returns:

True if started as monitoring

property procimg: str

Getter function.

Returns:

Path of the used process image

property replace_io_file: str

Returns the path to the used replace IO file.

Returns:

Path to the replace IO file

property simulator: bool

Getter function.

Returns:

True if started as simulator

Loop Execution Methods

Data Synchronization Methods

Utility Methods

RevPiModIOSelected

class revpimodio2.modio.RevPiModIOSelected(deviceselection, autorefresh=False, monitoring=False, syncoutputs=True, procimg=None, configrsc=None, simulator=False, debug=True, replace_io_file=None, shared_procimg=False)[source]

Bases: RevPiModIO

Class for managing individual devices from piCtory.

This class only takes over specified devices from the piCtory configuration and maps them including IOs. It takes over exclusive management of the address range in the process image where the specified devices are located and ensures that the data is synchronized.

Manage only specific devices from the piCtory configuration.

Use this class when you only need to control specific devices instead of loading the entire configuration.

Example:

# Manage only specific devices
rpi = revpimodio2.RevPiModIOSelected("DIO_Module_1", "AIO_Module_1")
__init__(deviceselection, autorefresh=False, monitoring=False, syncoutputs=True, procimg=None, configrsc=None, simulator=False, debug=True, replace_io_file=None, shared_procimg=False)[source]

Instantiates the basic functions only for specified devices.

The deviceselection parameter can be a single device position / single device name or a list with multiple positions / names.

Parameters:

deviceselection – Position number or device name

Ref:

RevPiModIO.__init__(...)()

__del__()

Destroys all classes to clean up.

__enter__()

Context manager entry (deprecated).

Deprecated since version Use: with revpi.io: or with revpi.device.my_device: instead.

__exit__(exc_type, exc_val, exc_tb)

Context manager exit (deprecated).

Writes process image and performs cleanup.

app
autorefresh_all() None

Sets all devices to autorefresh function.

cleanup() None

Terminates autorefresh and all threads.

property configrsc: str

Getter function.

Returns:

Path of the used piCtory configuration

core
cycleloop(func, cycletime=50, blocking=True)

Starts the cycle loop.

The current program thread is “trapped” here until .exit() is called. After each update of the process image, it executes the passed function “func” and processes it. During execution of the function, the process image is not further updated. The inputs retain their current value until the end. Set outputs are written to the process image after the function run completes.

The cycle loop is left when the called function returns a value not equal to None (e.g. return True), or by calling .exit().

NOTE: The refresh time and the runtime of the function must not exceed the set autorefresh time or passed cycletime!

The cycletime parameter sets the desired cycle time of the passed function. The default value is 50 milliseconds, in which the process image is read, the passed function is executed, and the process image is written.

Parameters:
  • func – Function to be executed

  • cycletime – Cycle time in milliseconds - default 50 ms

  • blocking – If False, the program does NOT block here

Returns:

None or the return value of the cycle function

property cycletime: int

Returns the refresh rate in ms of the process image synchronization.

Returns:

Milliseconds

property debug: bool

Returns the status of the debug flag.

Returns:

Status of the debug flag

device
exit(full=True) None

Terminates mainloop() and optionally autorefresh.

If the program is in mainloop(), calling exit() returns control to the main program.

The full parameter defaults to True and removes all devices from autorefresh. The thread for process image synchronization is then stopped and the program can be terminated cleanly.

Parameters:

full – Also removes all devices from autorefresh

exitsignal
export_replaced_ios(filename='replace_ios.conf') None

Exports replaced IOs of this instance.

Exports all replaced IOs that were created with .replace_io(…). The file can be used, for example, for RevPiPyLoad to transfer data in the new formats via MQTT or to view with RevPiPyControl.

@param filename Filename for export file

get_jconfigrsc() dict

Loads the piCtory configuration and creates a <class ‘dict’>.

Returns:

<class ‘dict’> of the piCtory configuration

handlesignalend(cleanupfunc=None) None

Manage signal handler for program termination.

When this function is called, RevPiModIO takes over the SignalHandler for SIGINT and SIGTERM. These are received when the operating system or the user wants to cleanly terminate the control program.

The optional function “cleanupfunc” is executed last after the last reading of the inputs. Outputs set there are written one last time after the function completes. This is intended for cleanup work, such as switching off the LEDs on the RevPi-Core.

After receiving one of the signals once and terminating the RevPiModIO threads / functions, the SignalHandler are released again.

Parameters:

cleanupfunc – Function to be executed after termination

io
property ioerrors: int

Getter function.

Returns:

Current number of counted errors

property length: int

Getter function.

Returns:

Length in bytes of the devices

mainloop(blocking=True) None

Starts the mainloop with event monitoring.

The current program thread is “trapped” here until RevPiDevicelist.exit() is called (unless blocking=False). It runs through the event monitoring and checks for changes of registered IOs with an event. If a change is detected, the program executes the associated functions in sequence.

If the parameter “blocking” is specified as False, this activates event monitoring and does NOT block the program at the point of call. Well suited for GUI programming when events from the RevPi are needed but the program should continue to execute.

Parameters:

blocking – If False, the program does NOT block here

property maxioerrors: int

Getter function.

Returns:

Number of allowed errors

property monitoring: bool

Getter function.

Returns:

True if started as monitoring

property procimg: str

Getter function.

Returns:

Path of the used process image

readprocimg(device=None) bool

Read all inputs of all/one device from the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

property replace_io_file: str

Returns the path to the used replace IO file.

Returns:

Path to the replace IO file

resetioerrors() None

Resets current IOError counter to 0.

setdefaultvalues(device=None) None

All output buffers are set to the piCtory default values.

Parameters:

device – Only apply to single device

property simulator: bool

Getter function.

Returns:

True if started as simulator

summary
syncoutputs(device=None) bool

Read all currently set outputs in the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

writeprocimg(device=None) bool

Write all outputs of all devices to the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

RevPiModIODriver

class revpimodio2.modio.RevPiModIODriver(virtdev, autorefresh=False, syncoutputs=True, procimg=None, configrsc=None, debug=True, replace_io_file=None, shared_procimg=False)[source]

Bases: RevPiModIOSelected

Class to create custom drivers for virtual devices.

With this class, only specified virtual devices are managed with RevPiModIO. During instantiation, inputs and outputs are automatically swapped to allow writing of inputs. The data can then be retrieved from the devices via logiCAD.

Write data to virtual device inputs for driver development.

Example:

# Create driver for virtual device
driver = revpimodio2.RevPiModIODriver("VirtualDevice")
__del__()

Destroys all classes to clean up.

__enter__()

Context manager entry (deprecated).

Deprecated since version Use: with revpi.io: or with revpi.device.my_device: instead.

__exit__(exc_type, exc_val, exc_tb)

Context manager exit (deprecated).

Writes process image and performs cleanup.

__init__(virtdev, autorefresh=False, syncoutputs=True, procimg=None, configrsc=None, debug=True, replace_io_file=None, shared_procimg=False)[source]

Instantiates the basic functions.

Parameters ‘monitoring’ and ‘simulator’ are not available here as they are set automatically.

Parameters:

virtdev – Virtual device or multiple as <class ‘list’>

Ref:

RevPiModIO.__init__()

app
autorefresh_all() None

Sets all devices to autorefresh function.

cleanup() None

Terminates autorefresh and all threads.

property configrsc: str

Getter function.

Returns:

Path of the used piCtory configuration

core
cycleloop(func, cycletime=50, blocking=True)

Starts the cycle loop.

The current program thread is “trapped” here until .exit() is called. After each update of the process image, it executes the passed function “func” and processes it. During execution of the function, the process image is not further updated. The inputs retain their current value until the end. Set outputs are written to the process image after the function run completes.

The cycle loop is left when the called function returns a value not equal to None (e.g. return True), or by calling .exit().

NOTE: The refresh time and the runtime of the function must not exceed the set autorefresh time or passed cycletime!

The cycletime parameter sets the desired cycle time of the passed function. The default value is 50 milliseconds, in which the process image is read, the passed function is executed, and the process image is written.

Parameters:
  • func – Function to be executed

  • cycletime – Cycle time in milliseconds - default 50 ms

  • blocking – If False, the program does NOT block here

Returns:

None or the return value of the cycle function

property cycletime: int

Returns the refresh rate in ms of the process image synchronization.

Returns:

Milliseconds

property debug: bool

Returns the status of the debug flag.

Returns:

Status of the debug flag

device
exit(full=True) None

Terminates mainloop() and optionally autorefresh.

If the program is in mainloop(), calling exit() returns control to the main program.

The full parameter defaults to True and removes all devices from autorefresh. The thread for process image synchronization is then stopped and the program can be terminated cleanly.

Parameters:

full – Also removes all devices from autorefresh

exitsignal
export_replaced_ios(filename='replace_ios.conf') None

Exports replaced IOs of this instance.

Exports all replaced IOs that were created with .replace_io(…). The file can be used, for example, for RevPiPyLoad to transfer data in the new formats via MQTT or to view with RevPiPyControl.

@param filename Filename for export file

get_jconfigrsc() dict

Loads the piCtory configuration and creates a <class ‘dict’>.

Returns:

<class ‘dict’> of the piCtory configuration

handlesignalend(cleanupfunc=None) None

Manage signal handler for program termination.

When this function is called, RevPiModIO takes over the SignalHandler for SIGINT and SIGTERM. These are received when the operating system or the user wants to cleanly terminate the control program.

The optional function “cleanupfunc” is executed last after the last reading of the inputs. Outputs set there are written one last time after the function completes. This is intended for cleanup work, such as switching off the LEDs on the RevPi-Core.

After receiving one of the signals once and terminating the RevPiModIO threads / functions, the SignalHandler are released again.

Parameters:

cleanupfunc – Function to be executed after termination

io
property ioerrors: int

Getter function.

Returns:

Current number of counted errors

property length: int

Getter function.

Returns:

Length in bytes of the devices

mainloop(blocking=True) None

Starts the mainloop with event monitoring.

The current program thread is “trapped” here until RevPiDevicelist.exit() is called (unless blocking=False). It runs through the event monitoring and checks for changes of registered IOs with an event. If a change is detected, the program executes the associated functions in sequence.

If the parameter “blocking” is specified as False, this activates event monitoring and does NOT block the program at the point of call. Well suited for GUI programming when events from the RevPi are needed but the program should continue to execute.

Parameters:

blocking – If False, the program does NOT block here

property maxioerrors: int

Getter function.

Returns:

Number of allowed errors

property monitoring: bool

Getter function.

Returns:

True if started as monitoring

property procimg: str

Getter function.

Returns:

Path of the used process image

readprocimg(device=None) bool

Read all inputs of all/one device from the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

property replace_io_file: str

Returns the path to the used replace IO file.

Returns:

Path to the replace IO file

resetioerrors() None

Resets current IOError counter to 0.

setdefaultvalues(device=None) None

All output buffers are set to the piCtory default values.

Parameters:

device – Only apply to single device

property simulator: bool

Getter function.

Returns:

True if started as simulator

summary
syncoutputs(device=None) bool

Read all currently set outputs in the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

writeprocimg(device=None) bool

Write all outputs of all devices to the process image.

Devices with active autorefresh are excluded!

Parameters:

device – Only apply to single device

Returns:

True if work on all devices was successful

DevSelect

class revpimodio2.modio.DevSelect(device_type='', search_key: str = None, search_values=())[source]

Bases: object

Customized search filter for RevPiModIOSelected.

__init__(device_type='', search_key: str = None, search_values=())[source]

Create a customized search filter for RevPiModIOSelected search.

If you leave search_key set to None or empty string, the default, the given search_values will automatically select the search_key. This depends on the data type in the tuple. A string value searches for device name, an integer value searches for device position.

Parameters:
  • device_type – Set a filter for just this device types

  • search_key – Set a property of device to search values or auto

  • search_values – Search for this values

type
other_device_key
values