Nincs leírás

Lu Xianghui b64e342ae5 Update Dockerfile 4 napja
app a2473eb789 Add Dockerfile 4 napja
docs e14ebb5507 First commit 5 napja
intergration a2473eb789 Add Dockerfile 4 napja
.dockerignore a2473eb789 Add Dockerfile 4 napja
.gitignore e14ebb5507 First commit 5 napja
.python-version e14ebb5507 First commit 5 napja
Dockerfile b64e342ae5 Update Dockerfile 4 napja
README.md e14ebb5507 First commit 5 napja
main.py e14ebb5507 First commit 5 napja
pyproject.toml e14ebb5507 First commit 5 napja
uv.lock e14ebb5507 First commit 5 napja

README.md

Data Collector Gateway

基于 FastAPI 的 Modbus TCP HTTP 网关,用于通过 HTTP 接口读取 Modbus TCP 设备。

环境要求

  • Python 3.12+

安装

uv sync

功能

  • 原始 Modbus 读取,返回 Modbus Poll 风格的 Tx/Rx 通信报文
  • 点位读取,并支持常见寄存器类型的数据转换
  • 支持 Modbus 功能码 1=线圈输入2=离散量输入3=保持寄存器4=输入寄存器

运行

uvicorn main:app --host 0.0.0.0 --port 8000

接口

基础路径:/api/dc-gateway

方法 路径 说明
GET /health 健康检查
POST /modbus/read 原始 Modbus 读取,返回 communication 报文
POST /modbus/read_points 读取点位并返回转换后的值

示例:

curl -X POST http://127.0.0.1:8000/api/dc-gateway/modbus/read \
  -H "Content-Type: application/json" \
  -d '{
    "device_type": "ModbusTCP",
    "ip": "192.168.75.240",
    "port": 505,
    "word_byte_order": "ABCD",
    "address_base": 0,
    "slave_id": 1,
    "read": {
      "function_code": 3,
      "address": 0,
      "quantity": 4
    }
  }'

完整请求和响应约定见 docs/modbus-http-api.md

测试

编译检查:

python -m compileall app main.py

集成测试:

python -m unittest discover -s intergration/modbus -p "test_*.py"

环境变量:

名称 默认值
DATA_COLLECTOR_BASE_URL http://127.0.0.1:8000
MODBUS_DEVICE_IP 192.168.75.240
MODBUS_TCP_PORT 505
MODBUS_SLAVE_ID 1
REQUEST_TIMEOUT 20