You can use a simple Python program to mirror the IOs of your devices.
The easiest solution is this little Python program, which mirrors your IOs using a dictionary with piCtory names. If the input value of "I_1" of die DIO is True, than the program will write a "1" to "Output_15" which you can read with Modbus. If you write a value >0 to an Input of the ModbusSlave, than the associated Output of the DIO will be set to True.
This is a kind of 1:1 mirroring if you use the 34/32 mode for the DIOs in piCtory. If you use 19/17 mode, all 16 Bits of Input / Output will be available as one Word, which you can mirror to just one input / output of Modbus, but you have to split this bits with your ModbusMaster.
Code: Select all
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""Modbus IO-mirror."""
__author__ = "Sven Sager"
__copyright__ = "Copyright (C) 2018 Sven Sager"
__license__ = "GPLv3"
import revpimodio2
def cleanup():
u"""Clean IOs of RevPi."""
rpi.core.a1green.value = False
def cycle(ct):
u"""Cycle to mirror the IOs."""
# Flash A1 led just to see the program is running
rpi.core.a1green.value = ct.flag10c
# Mirror data
for input_name in dict_io_map:
rpi.io[dict_io_map[input_name]].value = int(rpi.io[input_name].value)
# Create RevPiModIO and pass cleanup function
rpi = revpimodio2.RevPiModIO(autorefresh=True)
rpi.handlesignalend(cleanup)
# Create IO mapping input: output
# The names are the names which you define on piCtory!
dict_io_map = {
# DIO 1 inputs to first 14 Outputs of ModbusMaster
"I_1_i09": "Output_1",
"I_2_i09": "Output_2",
"I_3_i09": "Output_3",
"I_4_i09": "Output_4",
"I_5_i09": "Output_5",
"I_6_i09": "Output_6",
"I_7_i09": "Output_7",
"I_8_i09": "Output_8",
"I_9_i09": "Output_9",
"I_10_i09": "Output_10",
"I_11_i09": "Output_11",
"I_12_i09": "Output_12",
"I_13_i09": "Output_13",
"I_14_i09": "Output_14",
# DIO 2 inputs to next 14 Outputs of ModbusMaster
"I_1": "Output_15",
"I_2": "Output_16",
"I_3": "Output_17",
"I_4": "Output_18",
"I_5": "Output_19",
"I_6": "Output_20",
"I_7": "Output_21",
"I_8": "Output_22",
"I_9": "Output_23",
"I_10": "Output_24",
"I_11": "Output_25",
"I_12": "Output_26",
"I_13": "Output_27",
"I_14": "Output_28",
# First 14 inputs of ModbusMaster to DIO 1
"Input_1": "O_1_i09",
"Input_2": "O_2_i09",
"Input_3": "O_3_i09",
"Input_4": "O_4_i09",
"Input_5": "O_5_i09",
"Input_6": "O_6_i09",
"Input_7": "O_7_i09",
"Input_8": "O_8_i09",
"Input_9": "O_9_i09",
"Input_10": "O_10_i09",
"Input_11": "O_11_i09",
"Input_12": "O_12_i09",
"Input_13": "O_13_i09",
"Input_14": "O_14_i09",
# Next 14 inputs of ModbusMaster to DIO 2
"Input_15": "O_1",
"Input_16": "O_2",
"Input_17": "O_3",
"Input_18": "O_4",
"Input_19": "O_5",
"Input_20": "O_6",
"Input_21": "O_7",
"Input_22": "O_8",
"Input_23": "O_9",
"Input_24": "O_10",
"Input_25": "O_11",
"Input_26": "O_12",
"Input_27": "O_13",
"Input_28": "O_14",
}
# Start mirroring
rpi.cycleloop(cycle)
To transfair, start and watch the program, you can install RevPiPyLoad. The daemon will execute it on Revolution Pi startup and let it run all the time:
https://revpimodio.org/en/revpipyplc-2/
Sven