Mange
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:

  1. Import serial communication libraries.
  2. Parse magna.json for relevant data points.
  3. Establish serial communication between Raspberry Pi and the Grundfos pump.
  4. 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()
 
 
 
Notes:
  1. Adjust Commands: You will need to update the commands (start_command, stop_command, etc.) based on the structure in your magna.json file.
  2. 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!
 
 
 
 
Reply 0 0
thiagoralves
I'm not familiar with Genibus, but this program seems fairly incomplete, as 99% of what ChatGPT generates. In theory it should be possible, and quite straightforward, to have a python bridge between Modbus and another protocol, given that you have a decent, usable Python library for this "another protocol". Using PyModbus one can quite easily read and write Modbus registers on OpenPLC. Then it is just a matter of reading/writing the other end using the other protocol syntax / library.
Reply 0 0
Mange
Hmm, I see that there is Oscat library to use easily with Open PLC, what about this reply then? Does it make sense? Would these blocks work if I put them in Open PLC-editor as outputs from it? :
 
Here’s a function block outline for sending a Genibus telegram to a slave using the Oscat library and OpenPLC. This example assumes you are familiar with handling data structures and serial communication using the Oscat library.

1. Initialization Block for Serial Communication

Configure the serial communication using Oscat function blocks:
VAR
    Serial_Port: SERIAL_PORT;
    Init_OK: BOOL := FALSE;
END_VAR

IF NOT Init_OK THEN
    Serial_Port.OPEN := TRUE;
    Serial_Port.BAUD := 9600;  // Set baud rate as per Genibus specifications
    Serial_Port.PORT_NAME := '/dev/ttyS0'; // Adjust based on your hardware
    Init_OK := Serial_Port.OPEN;
END_IF;
 

2. Build Telegram Function Block

Prepare a function block to create a Genibus telegram:
st
 
FUNCTION_BLOCK FB_CreateTelegram
VAR_INPUT
    DestinationAddress: BYTE;
    SourceAddress: BYTE;
    DataClass: BYTE;
    Operation: BYTE;
    Data: ARRAY [1..8] OF BYTE;  // Adjust size as needed
END_VAR
VAR_OUTPUT
    Telegram: ARRAY [1..20] OF BYTE; // Adjust size as needed
    TelegramSize: INT;
END_VAR
VAR
    Index: INT;
    CRC: WORD;
END_VAR

// Build the telegram
Telegram[1] := 16#27; // Start delimiter for Data Request
Telegram[2] := 7 + SIZEOF(Data); // Length
Telegram[3] := DestinationAddress;
Telegram[4] := SourceAddress;
Telegram[5] := DataClass;
Telegram[6] := Operation;
Telegram[7] := SIZEOF(Data);

FOR Index := 1 TO SIZEOF(Data) DO
    Telegram[7 + Index] := Data[Index];
END_FOR;

// Calculate CRC (Assume CRC calculation function exists)
CRC := FB_CalculateCRC(Telegram, 2, 7 + SIZEOF(Data)); // Calculate over appropriate range
Telegram[8 + SIZEOF(Data)] := HI_BYTE(CRC);
Telegram[9 + SIZEOF(Data)] := LO_BYTE(CRC);

TelegramSize := 9 + SIZEOF(Data);
 

3. Send Telegram Function Block

Send the created telegram over the serial port:
st
Kopiera kod
 
FUNCTION_BLOCK FB_SendTelegram
VAR_INPUT
    Telegram: ARRAY [1..20] OF BYTE; // Adjust size based on your telegram
    TelegramSize: INT;
END_VAR
VAR_OUTPUT
    Sent_OK: BOOL;
END_VAR
VAR
    Index: INT;
END_VAR

FOR Index := 1 TO TelegramSize DO
    Serial_Port.WRITE_BYTE := Telegram[Index];
    Serial_Port.SEND := TRUE;
END_FOR;

Sent_OK := TRUE;  // Indicate successful transmission
 

4. Example Usage

In your main program, create a function block to handle sending commands:
st
Kopiera kod
 
VAR
    FB_Telegram: FB_CreateTelegram;
    FB_Send: FB_SendTelegram;
    DataArray: ARRAY [1..1] OF BYTE := [16#01]; // Example data for start
END_VAR

// Create and send a "start" command
FB_Telegram(DestinationAddress := 16#20, SourceAddress := 16#01, DataClass := 16#03, Operation := 16#10, Data := DataArray);
FB_Send(Telegram := FB_Telegram.Telegram, TelegramSize := FB_Telegram.TelegramSize);
Notes:
  • Ensure you handle the CRC calculation correctly according to the Genibus specification.
  • Modify data fields like DataClass and Operation based on your specific commands.
  • Use Oscat library functions for any string or data manipulation where needed.
This setup allows you to build and send telegrams to a Genibus slave device. Adjust as necessary for specific command types and data! Let me know if you need more customization or specific examples for handling responses!
Reply 0 0
thiagoralves
No, that won't work... You cannot address the serial port from ST in OpenPLC.
Reply 0 0
Mange
No, that won't work... You cannot address the serial port from ST in OpenPLC.
OK, I tell him that..
Reply 0 0
Mange
He admitted mistake and came up with this:  
Here’s a practical setup where an external Python application handles serial communication with your Genibus device, while exchanging data with OpenPLC using Modbus registers:

1. Python Application (External Controller)

This application handles serial communication with your Genibus slave device and updates Modbus registers in OpenPLC.

Example Code:

import serial
import minimalmodbus
import time

# Setup serial communication for Genibus
ser = serial.Serial(
    port='/dev/serial0',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

# Setup Modbus communication to OpenPLC
modbus_client = minimalmodbus.Instrument('/dev/ttyUSB0', 1)  # Adjust device and slave address as needed
modbus_client.serial.baudrate = 9600
modbus_client.mode = minimalmodbus.MODE_RTU

def send_genibus_command(command):
    """Send a Genibus command and handle response."""
    ser.write(command)
    time.sleep(0.1)  # Adjust delay if necessary
    response = ser.read(ser.in_waiting or 1)
    return response

def update_modbus_registers():
    """Example function to interact with OpenPLC via Modbus."""
    # Example: Reading command from OpenPLC (register 0)
    command = modbus_client.read_register(0)
    if command == 1:  # 1 could represent 'start'
        send_genibus_command(b'\x27\x0A...')  # Replace with actual Genibus start telegram
        modbus_client.write_register(1, 1)  # Set status in OpenPLC (register 1)
    elif command == 2:  # 2 could represent 'stop'
        send_genibus_command(b'\x27\x0B...')  # Replace with actual Genibus stop telegram
        modbus_client.write_register(1, 0)  # Clear status in OpenPLC

# Main loop
while True:
    update_modbus_registers()
    time.sleep(1)
 
 

2. Explanation:

  • Serial Communication: Handles sending Genibus commands through RS485.
  • Modbus Communication: Acts as a bridge between OpenPLC and the Genibus device. OpenPLC can set Modbus registers to send commands (e.g., start/stop).
  • Register Mapping: Use Modbus registers to pass commands, read statuses, or report data between OpenPLC and the external Python application.

3. OpenPLC Side Configuration:

  • Create Modbus tags in your OpenPLC project to send commands (e.g., start/stop) and receive statuses.
This setup allows OpenPLC to control and monitor your Genibus device via Modbus while the external Python application handles the actual Genibus communication.
Is this correct? I can use 2 pins of the RaspberryPI for this Genibus communication, and 2 other for my ordinary Modbus signals to the other device perhaps.  
Or is it better to use this hat for that?:
 
 
 
 
 
 
 
 
Reply 0 0
Mange
I also got hold of this original documentation from Grundfos about the Genibus protocol.  
Reply 0 0
thiagoralves
This can continue forever ad eternum... It is clear to me that ChatGPT does not have a clue what Genibus is, so it keeps generating incomplete crap when asked about it (which is what it does when it does not know). Without proper programming knowledge it will be quite hard and exhausting for you to have this task done by just having a conversation with ChatGPT
Reply 0 0
fg71
Reply 1 0
Mange
Hmm.. I got the same feeling. I didnt give him the last protocol yet though. From a programmers viewpoint, all info needed is in there, or ?
But is the first python application also crap? I really would like to understand how to connect an application like that, on the Rasp, that can handle communication to/from a slave.
Is it rocket science, or are there some examples from real life that I could study perhaps?
I think that the std modbus RTU-module/block/whatever, is integrated in OpenPLC, but I guess that there are other protocols which are handled by python applications in the computer?
 
How about this advice: "Create Modbus tags in your OpenPLC project to send commands (e.g., start/stop) and receive statuses.", is it possible or only general BS?
Reply 0 0
Mange
I know, there is a lot on Github, I only dont know how to use it...
Reply 0 0
thiagoralves
I honestly don’t think ChatGPT will be able to do that for you. It will require some programming knowledge to put things together. It is not complex, but hasn’t been built yet, so someone must build it. From the OpenPLC side you don’t have to do anything. Modbus comes built in. From the Python side you must build a tool that will be a Modbus slave (you will have to add it to the OpenPLC runtime under Slave Devices so they can talk) and then forward the Modbus commands it receives to the Genibus device.
Reply 0 0
Mange
I honestly don’t think ChatGPT will be able to do that for you. It will require some programming knowledge to put things together. It is not complex, but hasn’t been built yet, so someone must build it. From the OpenPLC side you don’t have to do anything. Modbus comes built in. From the Python side you must build a tool that will be a Modbus slave (you will have to add it to the OpenPLC runtime under Slave Devices so they can talk) and then forward the Modbus commands it receives to the Genibus device.
Ok, I see. Will consider options.
(Please see my post on bugs, Thiago. Right now I cant start oplc-editor.)
Reply 0 0
Mange
+ a 12V adapter and will simply use one of the pins of the PI to drive it w PWM.
(For test and first use, I will simply use one 5V pin and connect it via a relay to get 50% speed of the pump which I think it quite near what I need in the end.)
 
If anyone manage to make a controlmodule for Genibus in the future, Il be interested in testing it. :-)
Reply 0 0
Reply