Python implementation of Femtican client
Project description
lib-femtican.py
Environment setup
Consult DevBook section 10-50.
If you have conda installed correctly, run conda env create in the root project folder.
How to build this as a package
Change the package version in the pyproject.toml file according to semantic versioning rules.
Inside a conda environment, run:
python -m build . --sdist
Importing the package
pip install [path]/femtican-[version].tar.gz
Importing the package in Thonny IDE
Tools -> Manage packages -> Install from local file -> locate femtican-[version].tar.gz
Usage
Communication happens over NATS. All messages use the FemticanV3 topic.
The default NATS port is 60214.
Listening to broadcasts
import time
from femtican import FemticanClient
client = FemticanClient()
# default port is 60214. If running on host connected to laser system router use port 4222. If you want to use a different IP or port, connect like this:
# e.g. client.connect("172.168.1.2", 4222)
client.connect("cmp.local")
def global_handler(device_id, register_id, value):
match device_id, register_id:
case 208, 49:
print(f"Filtered data {value} from Device: 208, Register: 49")
client.broadcast_handler = global_handler
time.sleep(2)
client.disconnect()
Listening to broadcast (using per-register handlers)
import time
from femtican import FemticanClient
client = FemticanClient()
client.connect("cmp.local") # default port: 60214
def aries_handler(device_id, register_id, value):
print(f"{device_id}-{register_id}----{value} -- aries")
def random_handler(device_id, register_id, value):
print(f"{device_id}-{register_id}----{value} -- random")
client.add_broadcast_handler(162, 50, aries_handler)
client.add_broadcast_handler(162, 48, random_handler)
# optional: trigger a few broadcasts (example)
for _ in range(10):
client.trigger_broadcast(162, 50)
time.sleep(0.05)
# wait for incoming broadcasts
time.sleep(2)
client.remove_broadcast_handler(162, 50)
client.remove_broadcast_handler(162, 48)
client.disconnect()
Triggering a broadcast on a device
from femtican import FemticanClient
client = FemticanClient()
client.connect("cmp.local")
client.trigger_broadcast(208, 49) # device_id, register_id
client.disconnect()
Setting a value to a register
from femtican import FemticanClient
client = FemticanClient()
client.connect("cmp.local")
client.set_register(208, 49, 0) # device_id, register_id, value. Value can be text (string), float or None type
client.set_register_index(208, 49, 1, 3.0) # device_id, register_id, index, value. Value can only be float type
client.disconnect()
Error handling
import time
from femtican import FemticanClient
client = FemticanClient()
client.connect("cmp.local")
def error_handler(error_message):
print(f"Error: {error_message}")
client.error_handler = error_handler
time.sleep(2)
client.disconnect()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
femtican-0.4.0.tar.gz
(13.2 kB
view details)
File details
Details for the file femtican-0.4.0.tar.gz.
File metadata
- Download URL: femtican-0.4.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a556db845253ac9e9925389932d25519fb71e2c98b6761d286c280484c28a798
|
|
| MD5 |
5fae3004e01d572f303eab7e0f399ba1
|
|
| BLAKE2b-256 |
cc9ccc4730797f6c1026001522a372ab82e8404c9a11d109a9f5cef8477b1de2
|