初始化

This commit is contained in:
lhx
2026-06-08 15:17:52 +08:00
commit a40feb04ee
5 changed files with 1717 additions and 0 deletions

206
README.md Normal file
View File

@@ -0,0 +1,206 @@
# app_v4_分离版 — DiNi 水准仪 & 手机通信 远程控制
两个独立 FastAPI 服务,分别管理 PC 端与水准仪(NiNi)、手机(蓝牙模块)的蓝牙连接。
## 目录结构
```
app_v4_分离版/
├── send.py # 共享客户端 — 统一命令行发送脚本
├── shuizhunyi/
│ └── main.py # 水准仪服务 — 连接 Trimble-1780 控制 DiNi
├── shouji/
│ └── main.py # 手机通信服务 — 模拟水准仪向手机APP发测量数据
└── README.md # 本文件
```
---
## 1. 安装依赖
```bash
pip install fastapi uvicorn pyserial
```
`send.py` 额外依赖 `requests`(通常已自带)。
---
## 2. 水准仪控制服务 (`shuizhunyi/`)
通过 Trimble-1780 蓝牙适配器与天宝 DiNi 水准仪通信,发送 DiNi 协议命令。
### 启动
```bash
cd shuizhunyi
python main.py # 默认 0.0.0.0:58000
python main.py --port 8080 # 指定 HTTP 端口
python main.py --dini-port COM6 # 启动时自动连接水准仪
python main.py --baudrate 19200 # 指定波特率
```
### API 接口
| 方法 | 路径 | 说明 |
|------|------|------|
| `GET` | `/` | 服务状态 + COM 口列表 |
| `GET/POST` | `/test` | 测试未连接→COM列表已连接→仪器标识 + 可选测量 |
| `POST` | `/connect` | `{"port":"COM6","baudrate":9600}` 连接水准仪 |
| `POST` | `/disconnect` | 断开连接 |
| `POST` | `/command` | `{"cmd":"..."}` 发送 DiNi 命令 |
### send.py 调用示例
```bash
# 从上级目录执行
send.py --type test --port 58000
send.py --type connect --data COM6 --port 58000
send.py --type command --data "?0000" --port 58000 # 读取仪器标识
send.py --type command --data "?0100" --port 58000 # 读取仪器编号
send.py --type command --data "?KSND" --port 58000 # 读取提示音状态
send.py --type command --data "!KnM5" --port 58000 # 设置最大测量次数=5
send.py --type command --data "FML" --port 58000 # 触发测量
send.py --type command --data "SEO" --port 58000 # 远程关机
send.py --type disconnect --port 58000
```
### 支持的命令
**读取参数**: `?0000` `?0100` `?KT30` `?Krk` `?KLx` `?KOf` `?KmL` `?KmR` `?KnM` `?KEKR` `?KREF` `?KRAD` `?KFIR` `?KSND` `?KAPO`
**设置参数**: `!KnM<N>` `!KREF<0|1>` `!KEKR<0|1>` `!KFIR<0|1>` `!KSND<0|1>` `!KAPO<0|1>` `!KRAD<0|1|2>` `!Krk<浮点>` `!KLx<浮点>` `!KOf<浮点>` `!KmL<浮点>` `!KmR<浮点>`
> **注意**: 设置命令参数名与值之间无需空格,直接拼接,如 `!KnM5`、`!KSND0`。
> 文档标明 DiNi 设置命令"无需输入 Δ(空格)"。
**功能命令**: `FML` 触发测量 | `SEO` 远程关机
---
## 3. 手机通信服务 (`shouji/`)
PC 端蓝牙 SPP 作为服务端,等待手机 APP 连接后,模拟 Trimble DiNi 03 水准仪向手机发送 M5 格式测量数据。
### 工作原理
```
手机APP ──蓝牙──▶ 蓝牙模块 ──SPP串口──▶ PC (本服务)
▲ │
│ ?0100 轮询 │ 测量数据 (M5)
└────────────────────────────────┘
```
- 手机通过蓝牙连接到 PC 的蓝牙模块
- 手机 APP 周期性发送 `?0100` 轮询水准仪
- 本服务响应协议握手/控制码,并在收到轮询时发送排队测量数据
- 手机似乎只接收不回复确认
### 启动
```bash
cd shouji
python main.py # 默认 0.0.0.0:58100
python main.py --port 8081 # 指定 HTTP 端口
python main.py --bt-port COM3 # 启动时自动连接蓝牙 COM 口
```
### API 接口
| 方法 | 路径 | 说明 |
|------|------|------|
| `GET` | `/` | 服务状态 + COM 口列表 |
| `GET/POST` | `/test` | 测试未连接→COM列表已连接→手机连接状态+统计 |
| `GET` | `/status` | 详细状态:手机连接、待发送数、收发字节数 |
| `POST` | `/connect` | `{"port":"COM3"}` 打开蓝牙 COM 口 |
| `POST` | `/disconnect` | 断开 |
| `POST` | `/command` | `{"cmd":"..."}` 发送命令 |
### send.py 调用示例
```bash
# 从上级目录执行
send.py --type test --port 58100
send.py --type connect --data COM3 --port 58100 # 连接蓝牙 COM 口
send.py --type command --data "send 0.89182 3.323" --port 58100 # 添加测量到队列
send.py --type command --data "send 1.50000 5.000" --port 58100 # 再添加一条
send.py --type command --data "force" --port 58100 # 强制发送 (不等轮询)
send.py --type command --data "clear" --port 58100 # 清空队列
send.py --type disconnect --port 58100
```
### /command 支持的命令
| 命令 | 格式 | 说明 |
|------|------|------|
| `send` | `send <R> <HD>` | 添加测量到发送队列 (R=标尺读数, HD=距离) |
| `force` | `force` | 强制立即发送队列中第一条 |
| `clear` | `clear` | 清空待发送队列 |
| `disconnect` | `disconnect` | 断开蓝牙连接 |
| 原始 | `hex:AABBCC` | 发送原始 hex 字节 |
---
## 4. 共享客户端 (`send.py`)
统一的命令行发送脚本,通过 `--type` / `--port` 参数调用不同服务。
### 参数
```
--type {test,connect,disconnect,command} 操作类型
--data DATA 操作数据 (COM口 / 命令 / 参数)
--host HOST 服务地址 (默认 127.0.0.1)
--port PORT 服务端口 (水准仪 58000 / 手机 58100)
```
### 示例
```bash
# 水准仪服务 (port 58000)
python send.py --type test --port 58000
python send.py --type connect --data COM6 --port 58000
python send.py --type command --data "?0000" --port 58000
python send.py --type command --data "FML" --port 58000
# 手机通信服务 (port 58100)
python send.py --type test --port 58100
python send.py --type connect --data COM3 --port 58100
python send.py --type command --data "send 0.89182 3.323" --port 58100
python send.py --type command --data "force" --port 58100
```
> **提示**: 两个 `send.py` 是同一个文件,通过 `--port` 参数区分调用哪个服务。
---
## 5. 典型工作流程
### 场景: 电脑读取水准仪数据 → 发送到手机 APP
```bash
# 终端1: 启动水准仪服务
cd shuizhunyi
python main.py --dini-port COM6 # 端口 58000
# 终端2: 启动手机通信服务
cd shouji
python main.py --bt-port COM3 # 端口 58100
# 终端3: 操作
python send.py --type command --data "FML" --port 58000 # 从水准仪获取测量
# → 假设返回 staff_reading=0.89182, distance=3.323
python send.py --type command --data "send 0.89182 3.323" --port 58100 # 排队发给手机
# 手机APP发送 ?0100 轮询时自动收到数据
```
---
## 6. 注意事项
1. **串口独占**: 每个 COM 口只能有一个程序打开,两个服务需使用不同的 COM 口
2. **蓝牙配对**: 使用前确保 Trimble-1780 / 蓝牙模块已在 Windows 中配对,生成虚拟 COM 口
3. **波特率**: DiNi 水准仪默认 9600需与仪器内菜单设置一致
4. **手机通信方向**: 手机通信服务使用 **incoming** SPP 端口PC 作为服务端),启动时标 ★ 推荐的即为 incoming 端口
5. **DiNi 设置命令格式**: 参数名与值之间不加空格(如 `!KSND0` 而非 `!KSND 0`),服务端已自动处理