app_v4_分离版 — DiNi 水准仪 & 手机通信 远程控制
两个独立 FastAPI 服务,分别管理 PC 端与水准仪(NiNi)、手机(蓝牙模块)的蓝牙连接。
目录结构
app_v4_分离版/
├── send.py # 共享客户端 — 统一命令行发送脚本
├── shuizhunyi/
│ └── main.py # 水准仪服务 — 连接 Trimble-1780 控制 DiNi
├── shouji/
│ └── main.py # 手机通信服务 — 模拟水准仪向手机APP发测量数据
└── README.md # 本文件
1. 安装依赖
pip install fastapi uvicorn pyserial
send.py 额外依赖 requests(通常已自带)。
2. 水准仪控制服务 (shuizhunyi/)
通过 Trimble-1780 蓝牙适配器与天宝 DiNi 水准仪通信,发送 DiNi 协议命令。
启动
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 调用示例
# 从上级目录执行
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 格式测量数据。
数据发送为直接发送(调用 API 时立即通过串口发出,不等手机轮询)。
启动
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 接口
| 方法 | 路径 | 说明 |
|------|------|------|
| `GET` | `/` | 服务状态 + COM 口列表 |
| `GET/POST` | `/test` | 测试:未连接→COM列表;已连接→手机连接状态+统计 |
| `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 disconnect --port 58100
/command 支持的命令
| 命令 | 格式 | 说明 |
|---|---|---|
send |
send <R> <HD> |
直接发送测量数据到手机 (R=标尺读数, HD=距离) |
disconnect |
disconnect |
断开蓝牙连接 |
| 原始 | hex:AABBCC |
发送原始 hex 字节 |
/measure-and-send — 一键测量+发送
# 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)
统一的命令行发送脚本,通过 --type / --port 参数调用不同服务。
参数
--type {test,connect,disconnect,command} 操作类型
--data DATA 操作数据 (COM口 / 命令 / 参数)
--host HOST 服务地址 (默认 127.0.0.1)
--port PORT 服务端口 (水准仪 58000 / 手机 58100)
示例
# 水准仪服务 (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
方式一:一键操作 (推荐)
# 终端1: 启动水准仪服务
cd shuizhunyi && python main.py --dini-port COM6
# 终端2: 启动手机通信服务
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 '{}'
方式二:分步操作
# 终端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 "send 0.89182 3.323" --port 58100 # 直接发到手机
6. 注意事项
- 串口独占: 每个 COM 口只能有一个程序打开,两个服务需使用不同的 COM 口
- 蓝牙配对: 使用前确保 Trimble-1780 / 蓝牙模块已在 Windows 中配对,生成虚拟 COM 口
- 波特率: DiNi 水准仪默认 9600,需与仪器内菜单设置一致
- 手机通信方向: 手机通信服务使用 incoming SPP 端口(PC 作为服务端),启动时标 ★ 推荐的即为 incoming 端口
- DiNi 设置命令格式: 参数名与值之间不加空格(如
!KSND0而非!KSND 0),服务端已自动处理
Description
Languages
Python
100%