This release is a pre-release and may not be stable for production use.
python-cybro
Asynchronous Python client for Cybro / HIQ PLCs by Cybrotech.
Read and write PLC variables through the scgi server's HTTP interface (Cybro), or
talk to the controllers directly over A-bus/UDP without any server (CybroDirect).
Requirements
- Python 3.11 or newer
- For
Cybro: a running Cybrotech scgi server v3.2.6 or newer (tested with v3.2.6 and v3.3.1; earlier versions are not supported). You can install the server natively, or run it as a Docker container: - For
CybroDirect: the controllers in the same broadcast domain (UDP port 8442). In Docker, use host networking.
Installation
pip install cybro
Usage
import asyncio
from cybro import Cybro
async def main() -> None:
nad = 10000 # network address (NAD) of the PLC
prefix = f"c{nad}."
async with Cybro("192.168.1.100", port=4000, nad=nad) as cybro:
# The first update reads server and PLC information.
# It must run before variables can be added, read or written.
device = await cybro.update()
print("Server version:", device.server_info.server_version)
print("Controllers:", device.server_info.nad_list)
print("PLC status:", device.plc_info.plc_status)
# Register variables that every following update() should refresh.
cybro.add_var(f"{prefix}scan_time")
cybro.add_var(f"{prefix}sys.response_time")
device = await cybro.update()
for name in device.user_vars:
print(name, "->", device.vars[name].value)
# Read and write single variables.
print(await cybro.read_var(f"{prefix}lc00_qx00"))
await cybro.write_var(f"{prefix}lc00_qx00", "1")
asyncio.run(main())
A longer example lives in examples/control.py.
Connecting
Cybro(host_str, port=4000, nad=0, session=None)
host_strcan be a plain host name or IP address, or a URL with a path, e.g.http://example.com/scgi. The scheme is ignored; requests always use HTTP.nadis the network address of the PLC. It is needed for PLC information and foradd_var(). Withnad=0(the default), only server information is read. See Server only (nad=0).sessionlets you pass your ownaiohttp.ClientSession; you stay responsible for closing it. Otherwise one is created on the first request and closed when leavingasync with Cybro(...)or callingawait cybro.disconnect().
Reading data
await cybro.update(full_update=False, plc_nad=0, device_type=0) returns a Device:
| Attribute | Content |
|---|---|
device.server_info |
Server data: version, uptime, request counters, active NADs (nad_list) |
device.plc_info |
PLC data: IP/port, status, response time, ALC file, available variables |
device.vars |
All read variables by name (Var objects); only variables that have been read |
device.var_info |
Type and description of every PLC variable (VarInfo objects), values are not included |
device.user_vars |
Variables registered with add_var() |
- The first call (or
full_update=True) reads server and PLC information. Later calls only refresh the variables registered withadd_var(). - Set
device_type=1for HIQ controllers to also read their HIQ-specific variables. plc_nadsets the PLC address if none was given toCybro(). It only takes effect on the very firstupdate()call.
Server only (nad=0)
Without a NAD, update() reads only the server information. Use this to check that the
server is reachable, or to list the controllers it knows (device.server_info.nad_list).
In this mode:
device.plc_infoisNone.cybro.add_var(name)only accepts system variables (c<nad>.sys.*). Passallow_all=Trueto register other variables.read_var()andwrite_var()work with full variable names, e.g.c10000.scan_time.
To work with a PLC, create the Cybro object with its NAD.
Variables
cybro.add_var(name)registers a variable forupdate(). Only variables listed in the PLC's ALC file and system variables (c<nad>.sys.*) are accepted; passallow_all=Trueto add any name.cybro.remove_var(name)removes it again.await cybro.read_var(name)andawait cybro.write_var(name, value)read or write a single variable immediately.await cybro.read_var_int(name),read_var_float(name)andread_var_bool(name)return the value asint,floatorbool. They return"?"if the variable is unknown or its value cannot be converted.
Values are returned as strings. Each Var in device.vars has helpers to convert them:
value_int(), value_float() and value_bool().
Errors
All exceptions derive from CybroError, so catching it is enough:
| Exception | Raised when |
|---|---|
CybroConnectionError |
The scgi server cannot be reached |
CybroConnectionTimeoutError |
The scgi server does not answer in time |
CybroEmptyResponseError |
The scgi server returns an empty response |
CybroPlcNotFoundError |
The PLC information for the NAD is missing |
CybroError |
Any other error, e.g. add_var() or read_var() before update() |
Connection errors, timeouts and server errors (HTTP 5xx) are retried up to three times before an exception is raised. Client errors (HTTP 4xx) are raised immediately. Writes are retried too, so after a timeout a write may reach the PLC twice.
Direct mode (no scgi server)
CybroDirect has the same API as Cybro and returns the same Device, but talks
to the controller directly over A-bus (UDP port 8442). All controllers share one
AbusTransport (one UDP socket):
import asyncio
from cybro import CybroDirect
from cybro.abus import AbusTransport
from cybro.abus import scan
async def main() -> None:
transport = await AbusTransport.create() # binds UDP 8442
try:
for found in await scan(transport): # who is on the LAN?
print(found.nad, found.ip, found.status.plc.name)
cybro = CybroDirect(transport, nad=10000) # IP found by broadcast
device = await cybro.update()
cybro.add_var("c10000.scan_time")
device = await cybro.update()
print(device.vars["c10000.scan_time"].value)
await cybro.write_var("c10000.lc00_qx00", "1")
finally:
transport.close()
asyncio.run(main())
AbusTransport.create()binds UDP 8442 and raisesOSErrorif the port is in use (e.g. by an scgi server on the same host).AbusTransport.create(("0.0.0.0", 0))binds any free port: requests work, but frames the controllers send on their own are not received.CybroDirect(transport, nad, ip=None, password=None): withoutip, the controller is found by broadcast, and found again after a timeout. A staticipis always used.passwordis the A-bus password, if the controller has one.- Every request reads the program head and status first. After a program change, the allocation table (ALC) is downloaded again; otherwise it is kept in memory.
- Values are formatted like the scgi server does (e.g. a REAL
-1512.10107421875).c<nad>.sys.*values are created by the library;sys.server_versionisdirect <version>. - Differences to
Cybro: a controller that doesn't answer raisesCybroConnectionTimeoutError(there's no server to answer instead). An invalid write (unknown variable, value out of range) raisesCybroError. Requests are not retried with backoff; the transport resends each frame up to three times (200 ms timeout). scan()sends READ_STATUS to NAD 0; every controller answers with its NAD, IP and status.locate(transport, nad)returns the IP of one controller.- Tests:
cybro.testing.FakeAbusControlleris a fake controller on UDP with variables, an ALC, an optional password and socket/push frames.
The A-bus protocol code is ported from the Cybro Edge Toolkit (scgi_server) with the permission of Cybrotech.
Development
The project uses Poetry. A ready-to-use dev container is included for VS Code.
poetry install
poetry run pytest
poetry run pre-commit install # run Ruff, prettier and the tests before every commit
Contributing
Contributions are welcome! Please read the contribution guidelines first.
License
Metadata
Release files for cybro 1.0.0b1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cybro-1.0.0b1.tar.gz | 33.0 kB | Without provenance | Details for cybro-1.0.0b1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cybro-1.0.0b1-py3-none-any.whl | Python 3 | none | any | Details for cybro-1.0.0b1-py3-none-any.whl |
Total release size: 67.5 kB
Release files / cybro-1.0.0b1.tar.gz
| Download URL | cybro-1.0.0b1.tar.gz |
|---|---|
| Size | 33.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d5d54efc3f1c9c0d64e3b0210bc8465259321273e60fa766295e6b51a8739d65
|
|
BLAKE2b-256 checksum How to use checksums |
9b46423ac661992b05327b295b693ec5249c9613d8091f72faf2bbd770ae3224
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / cybro-1.0.0b1-py3-none-any.whl
| Download URL | cybro-1.0.0b1-py3-none-any.whl |
|---|---|
| Size | 34.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4120af4ab3a8461ce1fc3945fb559dc5c64fbd3da0ac118dcee2a3d3b2dc940a
|
|
BLAKE2b-256 checksum How to use checksums |
75217fcd41de676da34963468d73833459b74bc9eb112d32f0152c1c3bbbc33e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for cybro 1.0.0b1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.