Skip to content

Commit 0455c21

Browse files
feat: initial open source release
0 parents  commit 0455c21

16 files changed

+1407
-0
lines changed

.github/semantic.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
titleOnly: true

.github/workflows/cd.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Release
2+
on:
3+
push:
4+
branches: main
5+
6+
jobs:
7+
publish:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
# https://docs.github.com/en/rest/overview/permissions-required-for-github-apps?apiVersion=2022-11-28#metadata
11+
id-token: write
12+
contents: write
13+
steps:
14+
- uses: actions/checkout@v2
15+
with:
16+
ref: main
17+
fetch-depth: 0
18+
19+
- name: Python Semantic Release
20+
id: release
21+
uses: python-semantic-release/[email protected]
22+
with:
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
root_options: "-vv"
25+
26+
- name: Publish package distributions to PyPI
27+
id: pypi-publish
28+
29+
# NOTE: DO NOT wrap the conditional in ${{ }} as it will always evaluate to true.
30+
# See https://github.com/actions/runner/issues/1173
31+
if: steps.release.outputs.released == 'true'
32+
uses: pypa/gh-action-pypi-publish@release/v1
33+
with:
34+
verbose: true
35+
36+
- name: Publish package distributions to GitHub Releases
37+
id: github-release
38+
39+
# NOTE: DO NOT wrap the conditional in ${{ }} as it will always evaluate to true.
40+
# See https://github.com/actions/runner/issues/1173
41+
if: steps.release.outputs.released == 'true'
42+
uses: python-semantic-release/upload-to-gh-release@main
43+
with:
44+
github_token: ${{ secrets.GITHUB_TOKEN }}
45+
tag: ${{ steps.release.outputs.tag }}
46+

.gitignore

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Tutor Intelligence
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# tcp-modbus-aio
2+
3+
[![PyPI version](https://badge.fury.io/py/tcp-modbus-aio.svg)](https://badge.fury.io/py/tcp-modbus-aio)
4+
5+
asyncio client library for tcp modbus devices. built on top of [umodbus](https://pypi.org/project/uModbus/) but extended for more *industrial robustness* and asyncio compat. the [umodbus documentation](https://umodbus.readthedocs.io/en/latest/) is recommended reading to have any hope of using this code.
6+
7+
narrowly constructed for the use cases of [Tutor Intelligence](http://tutorintelligence.com/), but feel free to post an issue or PR if relevant to you.
8+
9+
### usage
10+
11+
create a `TCPModbusClient`. once you have it, you can call the following methods on it:
12+
- `await conn.send_modbus_message(request_function, **kwargs)`: sends a `umodbus.functions.ModbusFunction` to the modbus device and returns the corresponding `ModbusFunction` reply.
13+
- `await conn.test_connection()`: sends a modbus message to the device to ensure it's still operational (currently hardcoded to read coil 0) and return boolean of whether it succeeded. is implemented as a cached awaitable to allow you to spam this call.
14+
- `await conn.clear_tcp_connection()`: kill the current TCP socket (a new one will automatically be created for the next request)
15+
- `await conn.log_watch(msg, memo_key="system_temperature", expiry_period_s=10, hz=1)`: spins up a background coroutine to log the result of that message for the next `expiry_period` seconds at `hz` frequency. `memo_key` is used to allow multiple calls to `log_watch` without having overlapping log watch loops.
16+
- `await conn.close()`: for cleaning up the connection (kills TCP conn and ping loop)

examples/example.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
3+
from umodbus.functions import ReadCoils
4+
5+
from tcp_modbus_aio.connection import TCPModbusClient
6+
7+
8+
async def example() -> None:
9+
example_message = ReadCoils()
10+
example_message.starting_address = 0
11+
example_message.quantity = 1
12+
13+
async with TCPModbusClient("192.168.250.204") as conn:
14+
response = await conn.send_modbus_message(example_message)
15+
16+
assert response is not None, "we expect a response from ReadCoils"
17+
print(response.data)
18+
19+
20+
if __name__ == "__main__":
21+
asyncio.run(example())

0 commit comments

Comments
 (0)