fg71
Today I find a module in Python that binding S7Net and other PLC.
 
Python module: LECPython
 
At first it was not so simple to understand I'm not C# programmer but I do know Python. So I was able to get the ModebusTcp to work. The parameters is maybe not so easy to understand but there is a document to explain it. This is not a replacement for Snap7 or other Modbus modules for Python. Maybe other have not try it before so that why I share it here.
 
The program I use as Modbus server is SnapMB from Davide Nardella
 
Here is a screenshot.
[203128%2F9028995%2Fmceclip0]
 
from LECPython import LECPython

lecp = LECPython()
# res = lecp.SiemensPPIOverTcpConnection("192.168.0.2", 2000, 1, "CDAB", True, 2000)
# res = lecp.SiemensS7NetConnection("S200", "192.168.0.2", 2000, 0, 0, "CDAB", True, 2000)
res = lecp.ModbusTcpNetConnection("192.168.0.177", 502, 1, "CDAB", True, 2000)
print(res)

if res["IsSuccess"]:
    plc = res["Content"]
    print("Connection success")
else:
    print("Connection error")
    
r = lecp.ReadNodeValues(res["Content"], "0", "bool", 8)
print(r)
w = lecp.WriteNodeValues(res["Content"], "1", "bool", [True])
 
Reply 1 0
fg71
Few hours yesterday and some today debugging Python I was able to solve the task to use S7 Protocol with my LOGO. I have heard that LOGO is based on S7-200 but maybe the correct answer would be S7-200SMART. Maybe someone here knows more. To test the code below you need LOGO or if you change how communication is done with other Siemens PLC.
 
Here is the code that read/write data-type bool from Network block and Memory flag.
from LECPython import LECPython
from time import sleep

# import inspect

lecp = LECPython()
# res = lecp.SiemensPPIOverTcpConnection("192.168.0.2", 102, 1, "CDAB", True, 2000)
res = lecp.SiemensS7NetConnection("S200SMART", "192.168.0.2", 102, 0, 0, "CDAB", True, 2000)
# res = lecp.ModbusTcpNetConnection("192.168.0.177", 502, 1, "CDAB", True, 2000)
# res = lecp.SiemensFetchWriteNetConnection("192.168.0.2", 102, "CDAB", True, 2000)

if res["IsSuccess"]:
    plc = res["Content"]
    print("Connection success")
else:
    print("Connection error")

## debugging
# frame = inspect.currentframe()
# print(inspect.getargvalues(frame))

# Read memory flag M1
r = lecp.ReadNodeValues(plc, "V1104.0", "bool", 1)
print("M1: ", r["Content"])

sleep(1)

# Write memory flag M1
lecp.WriteNodeValues(plc, "V1104.0", "bool", [True])
r = lecp.ReadNodeValues(plc, "V1104.0", "bool", 1)
print("M1: ", r["Content"])

sleep(1)

lecp.WriteNodeValues(plc, "V1104.0", "bool", [False])
r = lecp.ReadNodeValues(plc, "V1104.0", "bool", 1)
print("M1: ", r["Content"])

sleep(1)

r = lecp.ReadNodeValues(plc, "V0.0", "bool", 1)
print("NI1: ", r["Content"])

sleep(1)

lecp.WriteNodeValues(plc, "V0.0", "bool", [True])
r = lecp.ReadNodeValues(plc, "V0.0", "bool", 1)
print("NI1: ", r["Content"])

sleep(1)

lecp.WriteNodeValues(plc, "V0.0", "bool", [False])
r = lecp.ReadNodeValues(plc, "V0.0", "bool", 1)
print("NI1: ", r["Content"])
 
Reply 2 0
thiagoralves
This LECPython library seems to be very powerful! Nice finding
Reply 1 0
fg71
I had a mixed feeling about the documentation of if. The good news is Python have many different way to extract information from Class, methods....
 
ex.
print(lecp.connection.__dir__())
Reply 1 0
xeden3
First of all, thank you so much for your attention and use of LECPython. I hope LECPython can develop healthily and steadily.
 
Background: As developers of industrial control systems and MES systems, we frequently use platforms like Python and Node.js in our daily work. However, these platforms have limited support for PLCs. Therefore, based on the LECPServer middleware project, we developed the LECPython module to facilitate communication with PLCs using Python. This aims to help more Python developers quickly enter the field of PLC and industrial control development.
 
Feedback and Contributions: The project has an open-source repository on GitHub, where the Python middleware part of the code is available. If you have any feedback or suggestions, please feel free to reach out to us through GitHub.
 
Future Plans: Currently, LECPServer and LECPython support the vast majority of PLC platforms on the market, fulfilling more than 95% of our requirements in actual work scenarios. Moving forward, we may periodically add support for other PLC brands such as GE, Yaskawa, Yamatake, and Vigor. Additionally, we are considering enabling serial communication support for PLCs, although this is not currently provided due to the predominant use of network devices in industrial control scenarios involving Python.
 
 --- 😉😉😉
Reply 1 0
fg71
I always use Python to do prototype coding but also to do visual simulation. I'm also a user of Node.js specially Node-Red. I do not know exactly how I find LECPython but I was not afraid to try it. And sometimes a simple example give other users the understanding how things are working. To share knowledge smal or big give people the power to do the same. In other words only by sharing greater things could happen.
 
Thanks for develop LECPython
 
In my country Sweden Siemens are big, and I believe ABB is to. The market for micro/nano PLC is great specially in Europe. Anyone could learn to code and build something to automate at home. Industrial Automation and the brands make it very difficult to learn when the hardware and software license is very expensive.
The core technology of MCU is not different from a cheap board vs high-end board. Its only how the program code is made into memory register of the hardware.
 
I have always said I like to learn how OPCUA is working, maybe it time to learn it with Python or Node-Red.
 
Thanks for the replay and open letter for the future of LECPython.
Reply 1 0
fg71
I tested your LECPServer and it did work for S200SMART with Siemens LOGO, but didn't find anywhere I could save my settings.
I do know for the most times address in registers for Siemens LOGO but many times I need to look in my cheat sheet.
 
The value entry do update when the state is changing in PLC.
 
I used the FREE version.
Reply 1 0
xeden3
I always use Python to do prototype coding but also to do visual simulation. I'm also a user of Node.js specially Node-Red. I do not know exactly how I find LECPython but I was not afraid to try it. And sometimes a simple example give other users the understanding how things are working. To share knowledge smal or big give people the power to do the same. In other words only by sharing greater things could happen.
 
Thanks for develop LECPython
 
In my country Sweden Siemens are big, and I believe ABB is to. The market for micro/nano PLC is great specially in Europe. Anyone could learn to code and build something to automate at home. Industrial Automation and the brands make it very difficult to learn when the hardware and software license is very expensive.
The core technology of MCU is not different from a cheap board vs high-end board. Its only how the program code is made into memory register of the hardware.
 
I have always said I like to learn how OPCUA is working, maybe it time to learn it with Python or Node-Red.
 
Thanks for the replay and open letter for the future of LECPython.
Yes, I completely agree with you. I've used Node-RED for some test projects before, but its performance was really poor. I tried to implement the entire running logic of a device using Node-RED, but for slightly more complex scheduling and calculation logic, it was basically unusable.
 
I've tried it on both PC and Raspberry Pi. Now I'm trying OpenPLC, hoping it can meet my requirements.

Attached is a screenshot of the Node-RED code from my previous project. The logic is actually quite simple, mainly involving loops, conditions, and timers.
 
微信截图_20241107222125.png
Reply 1 0
xeden3
I tested your LECPServer and it did work for S200SMART with Siemens LOGO, but didn't find anywhere I could save my settings.
I do know for the most times address in registers for Siemens LOGO but many times I need to look in my cheat sheet.
 
The value entry do update when the state is changing in PLC.
 
I used the FREE version.
In the LECPServer folder, there is a configuration file named plc.conf. The configuration is saved in JSON format and can usually be used directly by copying it.
 
微信截图_20241107222334.png

微信截图_20241107222542.png
Reply 1 0
fg71
I saw the plc.conf, I meant the entry for register address, lets say I like to watch 20 registers and every time I start the application I need to do it again. At least that is what I understand. It would be simple to make csv file with correct parameters for registers for PLC. And use that to import...
Or export to csv to import later... or using json or other format.
Reply 1 0
fg71
I do not think Node-Red is made for performance, its a event-driven application and it use JavaScript. In that context there maybe are some clever way to make Node-Red to perform better. Like ex. get/set multiply values.... The same approach you could do with Modbus protocol.
Reply 1 0
fg71
I have update your LECPython to the latest it looks like S200 is working now with Siemens LOGO
It doesn't matter if I use decimal or hex value ex. 0x02 or 2.... for TSAP
 
from LECPython import LECPython
from time import sleep

lecp = LECPython()
res = lecp.SiemensS7NetConnection("S200", "192.168.0.2", 102, 0x02, 0x03, "CDAB", True, 2000)

if res["IsSuccess"]:
    plc = res["Content"]
    print("Connection success")
else:
    print("Connection error")

# Read output Q1
r = lecp.ReadNodeValues(plc, "V1064.0", "bool", 1)
print("Q1:", r["Content"])
 
 
Reply 1 0
Reply