Ok, So I successfully managed to make a little ladder program and uploaded it to my RaspberryPI4b.
It runs fine, but now I want to be able to send some basic commands to one inverter (heatpump kompressor), which I will make a separate thread about, and (one the same modbus(?), also to/from a circulation pump for brine. Its a Grundfos, equipped with the Genibus interface. Seems to be very alike modbus, A/B + grund, but I understood now that its not really that simple.
I got some help from CHatGPT and a company here who makes interfaces for the industry to be able to connect to all sorts of various protocolls used in automation. However, they, as well as Grundfos, wants to sell a HW that I dont like to put money in for this little project. I therefore discussed this with ChatGPT, and finally made it write a code proposal to be able to communicate direct from a Rasp to a Genibus-unit.
Here is what I got, as a complete noob on programming, would this be possible to use and put in a block in OpenPLC?
Or how can it be done?
ChatGPT (we all know we will ask in the end;-) :
The JSON file you found on GitHub,
magna.json, from the repository by christoph2, appears to contain data structure definitions related to the Genibus protocol for Grundfos MAGNA pumps. It includes parameters for readable/writable points, status codes, and operational modes for communication via Genibus. This could serve as a useful reference or template for building your own control application. You can explore this and similar resources on GitHub to better understand Genibus interactions with Grundfos pumps
here.
To integrate Genibus communication in your OpenPLC running on a Raspberry Pi, you can use the magna.json structure as a reference to create a custom Modbus-to-Genibus interface in Python. This interface can communicate via the serial port and send commands (start/stop, speed setting) and handle feedback (status/error messages) using the Genibus protocol.
Here is a basic example outline to adapt Genibus interaction to OpenPLC logic:
Python Program for OpenPLC Integration:
- Import serial communication libraries.
- Parse
magna.json for relevant data points. - Establish serial communication between Raspberry Pi and the Grundfos pump.
- Define functions to start/stop the pump, set speed, and handle error/status messages based on Genibus protocol using the reference structure.
You may need to modify and integrate this script to communicate with your OpenPLC logic via inputs/outputs. Would you like a detailed example?
Here is a Python script example to integrate Genibus communication with an OpenPLC system running on a Raspberry Pi. It uses the structure outlined in the magna.json reference file to communicate via serial with a Grundfos pump:
Example Python Program:
import serial
import json
import time
# Load Genibus mapping from a JSON file (based on your provided magna.json reference)
with open("magna.json", "r") as file:
genibus_data = json.load(file)
# Configure serial communication
ser = serial.Serial(
port='/dev/serial0', # Adjust for your serial port
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
def calculate_checksum(data):
"""Calculate the checksum for Genibus communication."""
return sum(data) & 0xFF
def send_genibus_command(command):
"""Send a command via Genibus protocol."""
checksum = calculate_checksum(command)
message = bytearray(command + [checksum])
ser.write(message)
time.sleep(0.1)
response = ser.read(ser.in_waiting or 1)
return response
def start_pump():
"""Example to start the pump using Genibus."""
# Replace with actual data from the JSON reference
start_command = genibus_data["start_command"] # Adjust field name as needed
response = send_genibus_command(start_command)
print("Start Pump Response:", response)
def stop_pump():
"""Example to stop the pump using Genibus."""
# Replace with actual data from the JSON reference
stop_command = genibus_data["stop_command"] # Adjust field name as needed
response = send_genibus_command(stop_command)
print("Stop Pump Response:", response)
def set_speed(speed_percent):
"""Set pump speed (0-100%)."""
if not 0 <= speed_percent <= 100:
raise ValueError("Speed must be between 0-100%")
# Convert speed to a Genibus command (example conversion)
speed_command = genibus_data["set_speed_command"] # Example placeholder
speed_value = int(speed_percent * 255 / 100)
command = speed_command + [speed_value]
response = send_genibus_command(command)
print("Set Speed Response:", response)
def read_status():
"""Read status or error messages."""
status_command = genibus_data["status_command"] # Example placeholder
response = send_genibus_command(status_command)
print("Status Response:", response)
# Example usage
try:
start_pump()
time.sleep(5)
set_speed(75)
time.sleep(10)
stop_pump()
read_status()
finally:
ser.close()
- Adjust Commands: You will need to update the commands (
start_command, stop_command, etc.) based on the structure in your magna.json file. - Integration with OpenPLC: Communicate between this script and OpenPLC using file-based I/O, MQTT, or Modbus registers, depending on your setup.
Let me know if you need more details or adjustments for integration!