How xeden3/lecpython works
1. What this repository is
- LECPython is a Python package that acts as a bridge to a C#/.NET PLC communication library.
- The repo itself is very small: a Python module entrypoint (__init__.py), a library wrapper (LECPythonLib.py), and package metadata/docs. The actual PLC communication engine is implied to live in a .NET assembly that this Python package loads.
2. Top-level packaging boundary
- The Python package is the public surface area.
- from LECPython import LECPython is the intended entrypoint for consumers.
- LECPythonLib.py likely contains the wrapper logic that imports/hosts the .NET component and exposes Python-friendly methods.
3. Runtime dependency model
- The package depends on pythonnet to interoperate with the .NET runtime.
- It requires .NET 8, and the README says the package checks for it on first use and can install it automatically if missing.
- This makes the first-call path materially different from the normal read/write path: there is a runtime bootstrap step before PLC I/O can happen.
4. Startup and bootstrap flow
- On first invocation, the package performs environment validation: detect .NET 8, then install it online if absent.
- After the runtime is available, the Python layer can load the C# component through pythonnet.
- This bootstrap behavior is part of the product architecture, not just an installation detail, because it controls whether the communication stack can initialize at all.
5. Connection lifecycle
- The README describes a lazy connection model:
- DeviceProfinetConnection creates a connector object but does not immediately connect to the PLC.
- The actual network connection occurs on the first read/write through DeviceProfinetCommunication.
- The connector object is stateful and persists long enough to handle reconnection automatically until it is cleared or explicitly closed.
6. Data I/O boundary
- The primary operational boundary is PLC read/write access.
- Reads and writes are done against a connection handle, an address like D100, a data type such as float, and a quantity/value payload.
- The public API shown in the README includes ReadNodeValues, WriteNodeValues, and ConnectClose.
7. PLC protocol layer
- The .NET backend is positioned as a multi-protocol PLC communications layer.
- Supported families include Modbus, Mitsubishi, Siemens, Omron, Rockwell, Keyence, Delta, Beckhoff, Panasonic, Inovance, Fuji, EverSensing, and Schneider.
- Architecturally, this suggests the C# layer contains protocol-specific adapters behind a common API, while Python remains protocol-agnostic.
8. Connection configuration and data formatting
- The connector exposes formatting controls such as data_format and is_string_reverse_byte_word.
- These settings affect byte/word order and value interpretation, which means the Python layer is not just transport glue; it also passes through PLC-specific data layout semantics to the .NET backend.
9. State and cleanup
- The connection object is intended to be reused across multiple operations.
- The lifecycle ends with an explicit close call, which indicates a managed connection resource in the .NET layer.
- The architecture therefore has three state boundaries: package/runtime initialization, connection object lifetime, and active PLC session lifetime.
10. External dependencies and environment
- There is no separate service, database, or web server in this repo.
- The important external systems are:
- the local Python runtime,
- pythonnet,
- the .NET 8 runtime,
- and the target PLC over the network.
- The repo’s behavior changes depending on OS/runtime availability because of the auto-install path.
11. Documentation and distribution
- The repository is packaged for pip installation and includes MIT licensing.
- The README points to external API documentation hosted on a separate site, implying the repo is primarily a thin distribution wrapper around a larger maintained API surface.
12. How to diagram it
- A correct architecture diagram should show:
- Python application code on the left,
- the LECPython wrapper package in the middle,
- pythonnet plus .NET 8 as the interop/runtime layer,
- the C# PLC communication engine behind that,
- and PLC devices on the right.
- A separate bootstrap path should be shown for first-run .NET detection and installation, distinct from normal read/write traffic.
Main entry points: LECPythonLib.py Wrapper API · __init__.py Package entry · README.md Docs · LICENSE License
Components 11
Python package
Public Python surface and distribution files
- Package entry · Public import surface for consumers
__init__.py - Wrapper API · Python-friendly facade over the managed PLC library
LECPythonLib.py - Docs · Usage notes, runtime expectations, and API overview
README.md - License · Distribution license
LICENSE
Interop/runtime
PythonNet and .NET bootstrap path
- Runtime bootstrap · Detects and installs required .NET runtime on first use
- pythonnet · Python-to-.NET interop layer
- .NET 8 · Required managed runtime for the backend assembly
PLC backend
Managed communication engine and target devices
- PLC engine · Managed multi-protocol PLC communication library
- Protocol adapters · Backend support for vendor-specific PLC families
- PLC devices · Network-connected industrial controllers
Other components
- Python app · Code that imports and uses LECPython
Connections 12
- Python app → Package entry: import · Consumers import the package entrypoint
- Package entry → Wrapper API: exposes · Entry module re-exports the main API
- Wrapper API → Runtime bootstrap: first use · Validates runtime availability before PLC I/O
- Runtime bootstrap → .NET 8: ensure · Detects or installs the required .NET runtime
- Wrapper API → pythonnet: uses · Loads the managed assembly through PythonNet
- pythonnet → .NET 8: hosts · Interop depends on the .NET runtime
- pythonnet → PLC engine: loads · Bridges Python calls into the C# communication library
- PLC engine → Protocol adapters: routes · Common API dispatches to protocol-specific adapters
- Protocol adapters → PLC devices: communicates · Sends read/write traffic to PLC hardware
- Wrapper API → PLC devices: read/write · Public operations open sessions and perform PLC I/O
- Wrapper API → Docs: documented by · README describes lazy connect, data formatting, and close flow
- Wrapper API → License: distributed with · Package is published under MIT license