增加测量并发送手机接口

This commit is contained in:
lhx
2026-06-09 11:09:43 +08:00
parent c25257b0e3
commit 2ad0488b26
2 changed files with 132 additions and 32 deletions

View File

@@ -82,19 +82,7 @@ send.py --type disconnect --port 58000
PC 端蓝牙 SPP 作为服务端,等待手机 APP 连接后,模拟 Trimble DiNi 03 水准仪向手机发送 M5 格式测量数据。
### 工作原理
```
手机APP ──蓝牙──▶ 蓝牙模块 ──SPP串口──▶ PC (本服务)
▲ │
│ ?0100 轮询 │ 测量数据 (M5)
└────────────────────────────────┘
```
- 手机通过蓝牙连接到 PC 的蓝牙模块
- 手机 APP 周期性发送 `?0100` 轮询水准仪
- 本服务响应协议握手/控制码,并在收到轮询时发送排队测量数据
- 手机似乎只接收不回复确认
数据发送为**直接发送**(调用 API 时立即通过串口发出,不等手机轮询)。
### 启动
@@ -103,7 +91,7 @@ cd shouji
python main.py # 默认 0.0.0.0:58100
python main.py --port 8081 # 指定 HTTP 端口
python main.py --bt-port COM3 # 启动时自动连接蓝牙 COM 口
```
python main.py --level-url http://192.168.1.100:58000 # 指定水准仪服务地址
### API 接口
@@ -111,21 +99,19 @@ python main.py --bt-port COM3 # 启动时自动连接蓝牙 COM
|------|------|------|
| `GET` | `/` | 服务状态 + COM 口列表 |
| `GET/POST` | `/test` | 测试未连接→COM列表已连接→手机连接状态+统计 |
| `GET` | `/status` | 详细状态:手机连接、待发送数、收发字节数 |
| `GET` | `/status` | 详细状态:手机连接、收发字节数 |
| `POST` | `/connect` | `{"port":"COM3"}` 打开蓝牙 COM 口 |
| `POST` | `/disconnect` | 断开 |
| `POST` | `/command` | `{"cmd":"..."}` 发送命令 |
| `POST` | `/measure-and-send` | 一键操作:触发水准仪测量→获取数据→发到手机 |
### 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 connect --data COM3 --port 58100 # 连接蓝牙 COM 口
send.py --type command --data "send 0.89182 3.323" --port 58100 # 直接发送测量数据
send.py --type disconnect --port 58100
```
@@ -133,12 +119,26 @@ send.py --type disconnect --port 58100
| 命令 | 格式 | 说明 |
|------|------|------|
| `send` | `send <R> <HD>` | 添加测量到发送队列 (R=标尺读数, HD=距离) |
| `force` | `force` | 强制立即发送队列中第一条 |
| `clear` | `clear` | 清空待发送队列 |
| `send` | `send <R> <HD>` | 直接发送测量数据到手机 (R=标尺读数, HD=距离) |
| `disconnect` | `disconnect` | 断开蓝牙连接 |
| 原始 | `hex:AABBCC` | 发送原始 hex 字节 |
### /measure-and-send — 一键测量+发送
```bash
# curl 调用 (send.py 暂不支持此接口,直接用 curl)
curl -X POST http://127.0.0.1:58100/measure-and-send \
-H "Content-Type: application/json" \
-d '{}'
# 指定水准仪服务地址
curl -X POST http://127.0.0.1:58100/measure-and-send \
-H "Content-Type: application/json" \
-d '{"level_url":"http://192.168.1.100:58000"}'
```
工作流:先向水准仪服务发 `FML` 触发测量 → 解析返回的 `staff_reading` + `distance` → 直接通过蓝牙发到手机。
---
## 4. 共享客户端 (`send.py`)
@@ -178,21 +178,34 @@ python send.py --type command --data "force"
### 场景: 电脑读取水准仪数据 → 发送到手机 APP
**方式一:一键操作 (推荐)**
```bash
# 终端1: 启动水准仪服务
cd shuizhunyi
python main.py --dini-port COM6 # 端口 58000
cd shuizhunyi && python main.py --dini-port COM6
# 终端2: 启动手机通信服务
cd shouji
python main.py --bt-port COM3 # 端口 58100
cd shouji && python main.py --bt-port COM3
# 一键: 触发测量 + 发送到手机
curl -X POST http://127.0.0.1:58100/measure-and-send \
-H "Content-Type: application/json" -d '{}'
```
**方式二:分步操作**
```bash
# 终端1: 启动水准仪服务
cd shuizhunyi && python main.py --dini-port COM6
# 终端2: 启动手机通信服务
cd shouji && python main.py --bt-port COM3
# 终端3: 操作
python send.py --type command --data "FML" --port 58000 # 水准仪获取测量
# → 假设返回 staff_reading=0.89182, distance=3.323
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 轮询时自动收到数据
python send.py --type command --data "send 0.89182 3.323" --port 58100 # 直接发到手机
```
---