# Data Collector Gateway 基于 FastAPI 的 Modbus TCP HTTP 网关,用于通过 HTTP 接口读取 Modbus TCP 设备。 ## 环境要求 - Python 3.12+ ## 安装 ```bash uv sync ``` ## 功能 - 原始 Modbus 读取,返回 Modbus Poll 风格的 `Tx/Rx` 通信报文 - 点位读取,并支持常见寄存器类型的数据转换 - 支持 Modbus 功能码 `1=线圈输入`、`2=离散量输入`、`3=保持寄存器`、`4=输入寄存器` ## 运行 ```bash uvicorn main:app --host 0.0.0.0 --port 8000 ``` ## 接口 基础路径:`/api/dc-gateway` | 方法 | 路径 | 说明 | |---|---|---| | `GET` | `/health` | 健康检查 | | `POST` | `/modbus/read` | 原始 Modbus 读取,返回 `communication` 报文 | | `POST` | `/modbus/read_points` | 读取点位并返回转换后的值 | 示例: ```bash 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](docs/modbus-http-api.md)。 ## 测试 编译检查: ```bash python -m compileall app main.py ``` 集成测试: ```bash 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` |