a
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
# 设备连接与注册
|
||||
|
||||
<cite>
|
||||
**本文档引用的文件**
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp)
|
||||
- [DevManager.h](file://h/DevManager.h)
|
||||
- [SComPort.cpp](file://cpp/Tools/SComPort.cpp)
|
||||
- [SComPort.h](file://h/SComPort.h)
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp)
|
||||
- [Device.h](file://h/Device.h)
|
||||
- [DetcGD10Dev.cpp](file://cpp/Operator/DetcGD10Dev.cpp)
|
||||
- [DetcGD10Dev.h](file://h/DetcGD10Dev.h)
|
||||
- [GD10OperCmd.cpp](file://cpp/Tools/GD10OperCmd.cpp)
|
||||
- [GD10OperCmd.h](file://h/GD10OperCmd.h)
|
||||
- [Global.cpp](file://cpp/Main/Global.cpp)
|
||||
</cite>
|
||||
|
||||
## 目录
|
||||
1. [引言](#引言)
|
||||
2. [设备连接与枚举](#设备连接与枚举)
|
||||
3. [设备注册与数据初始化](#设备注册与数据初始化)
|
||||
4. [CDevice对象与链表管理](#cdevice对象与链表管理)
|
||||
5. [连接状态与通知机制](#连接状态与通知机制)
|
||||
6. [常见问题与解决方案](#常见问题与解决方案)
|
||||
7. [结论](#结论)
|
||||
|
||||
## 引言
|
||||
本文档详细阐述了GeomativeStudio软件中设备连接与注册功能的实现机制。核心组件`CDevManager`负责管理所有设备的生命周期,通过`SComPort`类与GD10设备建立串口通信,实现设备的自动检测、握手、注册和数据同步。文档将深入分析从物理连接检测到设备对象在内存中创建并注册的完整流程。
|
||||
|
||||
## 设备连接与枚举
|
||||
|
||||
设备连接的起点是检测物理设备的存在。系统通过`CDetcGD10Dev`类来检测连接的GD10设备。
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[开始检测设备] --> B[枚举所有逻辑驱动器]
|
||||
B --> C{驱动器类型为可移动磁盘?}
|
||||
C --> |是| D[获取驱动器卷标]
|
||||
D --> E{卷标为"GD10"或"GD20"?}
|
||||
E --> |是| F[记录设备地址]
|
||||
E --> |否| G[检查是否存在\\SD\\equipment目录]
|
||||
G --> |存在| F
|
||||
G --> |不存在| H[继续下一个驱动器]
|
||||
C --> |否| H
|
||||
H --> I{遍历完所有驱动器?}
|
||||
I --> |否| B
|
||||
I --> |是| J[返回设备地址]
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [DetcGD10Dev.cpp](file://cpp/Operator/DetcGD10Dev.cpp#L100-L173)
|
||||
|
||||
**Section sources**
|
||||
- [DetcGD10Dev.cpp](file://cpp/Operator/DetcGD10Dev.cpp#L61-L68)
|
||||
- [DetcGD10Dev.h](file://h/DetcGD10Dev.h#L22)
|
||||
|
||||
### 串口通信建立
|
||||
一旦检测到设备,`CDevManager`会通过`SComPort`类建立串口通信。`SComPort`的`OpenComm`方法负责初始化串口。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant CDevManager as CDevManager
|
||||
participant SComPort as SComPort
|
||||
participant OS as 操作系统
|
||||
CDevManager->>SComPort : OpenComm("COM3")
|
||||
SComPort->>OS : CreateFile("\\\\.\\COM3")
|
||||
OS-->>SComPort : 返回设备句柄
|
||||
SComPort->>SComPort : SetupComm(设置缓冲区大小)
|
||||
SComPort->>SComPort : GetCommState(获取当前配置)
|
||||
SComPort->>SComPort : 配置DCB结构体
|
||||
SComPort->>SComPort : SetCommState(应用配置)
|
||||
SComPort->>SComPort : SetCommMask(设置事件掩码)
|
||||
SComPort-->>CDevManager : 返回TRUE(成功)
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [SComPort.cpp](file://cpp/Tools/SComPort.cpp#L127-L222)
|
||||
- [SComPort.h](file://h/SComPort.h#L34)
|
||||
|
||||
**Section sources**
|
||||
- [SComPort.cpp](file://cpp/Tools/SComPort.cpp#L127-L222)
|
||||
|
||||
## 设备注册与数据初始化
|
||||
|
||||
当设备通过串口成功连接后,需要进行注册,将设备的物理信息同步到本地数据库。
|
||||
|
||||
### 设备信息读取
|
||||
`CDevice`类的`GetDevInfo`方法负责从GD10设备读取其硬件和固件信息。
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[调用GetDevInfo] --> B[发送"device_ex_info()"命令]
|
||||
B --> C[等待设备响应]
|
||||
C --> D{收到响应?}
|
||||
D --> |是| E[解析响应数据]
|
||||
E --> F[提取硬件版本(HWV)]
|
||||
E --> G[提取固件版本(SWV)]
|
||||
E --> H[提取生产日期(Mdate)]
|
||||
E --> I[提取批次号(Mbatch)]
|
||||
F --> J[更新CDevice对象属性]
|
||||
G --> J
|
||||
H --> J
|
||||
I --> J
|
||||
J --> K[将信息写入本地数据库]
|
||||
K --> L[返回成功]
|
||||
D --> |否| M[返回失败]
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp#L1008-L1053)
|
||||
|
||||
**Section sources**
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp#L1008-L1113)
|
||||
|
||||
### 设备注册流程
|
||||
设备信息读取成功后,`CDevManager`会调用`AddDevice`方法将设备正式注册到系统中。
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class CDevManager {
|
||||
+AddDevice(CDevice* pDev)
|
||||
+InitialDevLinkList()
|
||||
+GetDeviceByID(DWORD dwDevID)
|
||||
}
|
||||
class CDevice {
|
||||
+m_szDevSN
|
||||
+m_szModelNO
|
||||
+m_szHWV
|
||||
+m_szSWV
|
||||
+m_uState
|
||||
+m_dwID
|
||||
}
|
||||
class _ConnectionPtr {
|
||||
+BeginTrans()
|
||||
+CommitTrans()
|
||||
+RollbackTrans()
|
||||
}
|
||||
CDevManager --> CDevice : 持有
|
||||
CDevManager --> _ConnectionPtr : 使用
|
||||
CDevManager --> CDevice : 注册
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L203-L280)
|
||||
- [DevManager.h](file://h/DevManager.h#L30)
|
||||
|
||||
**Section sources**
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L203-L280)
|
||||
|
||||
## CDevice对象与链表管理
|
||||
|
||||
`CDevManager`使用一个链表`m_devLinkList`来管理所有已注册的设备对象。
|
||||
|
||||
### 对象创建与注册
|
||||
当一个新设备被发现时,`CDevManager`会创建一个`CDevice`对象,并将其添加到链表中。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant CDevManager as CDevManager
|
||||
participant CDevice as CDevice
|
||||
participant LinkList as CLinkList
|
||||
CDevManager->>CDevice : new CDevice()
|
||||
CDevice-->>CDevManager : 返回指针
|
||||
CDevManager->>CDevManager : m_handleProcessor.GenerateHandle()
|
||||
CDevManager->>LinkList : m_devLinkList.Add(句柄, CDevice指针)
|
||||
LinkList-->>CDevManager : 添加成功
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L325-L343)
|
||||
- [DevManager.h](file://h/DevManager.h#L63)
|
||||
|
||||
**Section sources**
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L325-L343)
|
||||
|
||||
### 链表管理机制
|
||||
`CLinkList`类提供了对设备对象的增删查改操作,`CDevManager`通过设备ID或句柄来访问特定的`CDevice`对象。
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class CLinkList {
|
||||
+Add(DWORD handle, CDevice* pDev)
|
||||
+Delete(DWORD handle)
|
||||
+Delete(CDevice* pDev)
|
||||
+Get(DWORD handle) CDevice*
|
||||
+Find(int index) CDevice*
|
||||
+Length() int
|
||||
+DeleteAll()
|
||||
}
|
||||
class CDevice {
|
||||
+m_dwID
|
||||
+m_szDevSN
|
||||
+m_uState
|
||||
}
|
||||
CLinkList "1" --> "*" CDevice : 包含
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [DevManager.h](file://h/DevManager.h#L63)
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L48-L68)
|
||||
|
||||
**Section sources**
|
||||
- [DevManager.h](file://h/DevManager.h#L63)
|
||||
|
||||
## 连接状态与通知机制
|
||||
|
||||
系统通过`CDevice`对象的`m_uState`属性来跟踪设备的连接状态,并通过UI层进行通知。
|
||||
|
||||
### 状态变更流程
|
||||
设备的状态会随着连接过程而改变,从`PZ_STATE_NEW`到`PZ_STATE_ONLINE`。
|
||||
|
||||
```mermaid
|
||||
stateDiagram-v2
|
||||
[*] --> PZ_STATE_OFFLINE
|
||||
PZ_STATE_OFFLINE --> PZ_STATE_NEW : 设备被发现
|
||||
PZ_STATE_NEW --> PZ_STATE_ONLINE : 注册成功
|
||||
PZ_STATE_NEW --> PZ_STATE_OFFLINE : 注册失败
|
||||
PZ_STATE_ONLINE --> PZ_STATE_OFFLINE : 连接断开
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Device.h](file://h/Device.h#L89)
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp#L75-L77)
|
||||
|
||||
**Section sources**
|
||||
- [Device.h](file://h/Device.h#L89)
|
||||
|
||||
### UI层响应
|
||||
当设备状态发生变化时,UI层会更新设备列表视图,显示最新的设备信息。
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant CDevice as CDevice
|
||||
participant CDevManager as CDevManager
|
||||
participant UI as UI界面
|
||||
CDevice->>CDevice : SetState(PZ_STATE_ONLINE)
|
||||
CDevice->>CDevManager : 通知状态变更
|
||||
CDevManager->>UI : 发送WM_DEVICE_STATE_CHANGED消息
|
||||
UI->>UI : 更新设备列表
|
||||
UI->>UI : 刷新设备详细信息
|
||||
```
|
||||
|
||||
**Diagram sources**
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp#L75-L77)
|
||||
- [DevManager.cpp](file://cpp/Managers/DevManager.cpp#L489-L507)
|
||||
|
||||
**Section sources**
|
||||
- [Device.cpp](file://cpp/ProblemZone/Device.cpp#L75-L77)
|
||||
|
||||
## 常见问题与解决方案
|
||||
|
||||
在设备连接过程中可能会遇到各种问题,系统提供了相应的处理机制。
|
||||
|
||||
### 连接超时
|
||||
如果在规定时间内未能收到设备的响应,则判定为连接超时。
|
||||
|
||||
**解决方案**:
|
||||
1. 检查设备电源是否开启。
|
||||
2. 检查USB线缆是否连接牢固。
|
||||
3. 尝试更换USB端口或线缆。
|
||||
4. 重启设备和软件。
|
||||
|
||||
### 端口占用
|
||||
如果目标串口已被其他程序占用,则无法打开。
|
||||
|
||||
**解决方案**:
|
||||
1. 关闭可能占用该串口的其他程序(如串口调试助手)。
|
||||
2. 在设备管理器中检查端口状态。
|
||||
3. 重启计算机以释放所有端口。
|
||||
|
||||
### 协议不匹配
|
||||
如果设备固件版本过低,可能不支持当前软件的通信协议。
|
||||
|
||||
**解决方案**:
|
||||
1. 检查设备固件版本。
|
||||
2. 使用`IAP-GD10.bat`工具对设备进行固件升级。
|
||||
3. 确保软件版本与设备固件版本兼容。
|
||||
|
||||
## 结论
|
||||
GeomativeStudio的设备连接与注册功能通过`CDevManager`、`SComPort`和`CDevice`等核心类协同工作,实现了从物理设备检测、串口通信建立、设备信息读取、数据库注册到UI状态更新的完整流程。该设计具有良好的模块化和可维护性,能够稳定地管理GD10设备的连接生命周期。
|
||||
Reference in New Issue
Block a user