fg71
OpenPLC Runtime - Python on Linux (PSM) is a very powerful. Lets say you like to communicate with Raspberry Pi
that is running OpenPLC Runtime with Siemens S7 communication. To make this happen we need to download the source code of snap7 and compile it for Raspberry Pi OS. Its not as simple as pip install to make that happen. We need to compile the source code and copy the library to /usr/local/lib/ libsnap7.so
 
Now we have binary that we could use for Raspberry Pi OS.
 
Inside OpenPLC_v3 folder we do
-> .venv/bin/pip install python-snap7
 
Before we could use this communication with OpenPLC Runtime we need to edit the common.py from the snap7
-> nano .venv/lib/<your_python_version>/site-packages/snap7/ common.py
 
And look for this line
if not lib_location:
        lib_location = _find_in_package() or find_library("snap7") or _find_locally("snap7") or '/usr/local/lib/libsnap7.so'
 
And add '/usr/local/lib/libsnap7.so' to the end of the line like the above.
 
Now we could setup the blink plc program to send write message to PLC that use S7 communication.
 
Here is a example with Python on Linux (PSM), if you couldn't do it in C why not use Python.
 
import psm
import time
from snap7 import logo

client= logo.Logo()
status = client.connect("192.168.0.2", 0x03, 0x02)
if status:
    print("Client is connected with PLC")

#global variables
counter = 0
var_state = False

def hardware_init():
    #Insert your hardware initialization code in here
    psm.start()

def update_inputs():
    #place here your code to update inputs
    global counter
    global var_state
    # psm.set_var("IX0.0", var_state)
    counter += 1
    if (counter == 10):
        counter = 0
        var_state = not var_state
    
def update_outputs():
    #place here your code to work on outputs
    a = psm.get_var("QX0.0")
    if a == True:
        client.write("V0.0", True)
    else:
        client.write("V0.0", False)


if __name__ == "__main__":
    hardware_init()
    while (not psm.should_quit()):
        update_inputs()
        update_outputs()
        time.sleep(0.1) #You can adjust the psm cycle time here
    psm.stop()
 
Reply 0 0
Reply