Files
ch34/CLAUDE.md
2026-04-21 12:57:40 +08:00

106 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## 项目概述
这是一个 Flutter 插件项目,为 WCH CH34X 系列 USB 转串口芯片提供 Flutter 接口支持。支持的芯片型号CH340/CH341/CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104/CH9143。
## 常用命令
```bash
# 安装依赖
flutter pub get
# 运行所有测试
flutter test
# 运行单个测试文件
flutter test test/ch34_test.dart
flutter test test/ch34_method_channel_test.dart
# 代码分析lint
flutter analyze
# 格式化代码
dart format .
# 构建示例 APK
cd example && flutter build apk
# 运行示例应用
cd example && flutter run
```
## 代码架构
### 目录结构
```
lib/
├── ch34.dart # 主入口,导出所有公共 API
├── ch34_method_channel.dart # 顶层导出(向后兼容)
├── ch34_platform_interface.dart # 顶层导出(向后兼容)
└── src/
├── ch34_manager.dart # 公共 API 管理器(静态方法门面)
├── ch34_platform_interface.dart # 抽象平台接口定义
├── ch34_method_channel.dart # MethodChannel 实现 + 异常类
└── types/
└── ch34_types.dart # 所有类型和枚举定义
```
### 核心类
- **`Ch34Manager`** - 公共 API 入口,所有方法为静态,委托给 `Ch34Platform.instance`
- **`Ch34Platform`** - 抽象平台接口,定义所有方法签名,继承自 `PlatformInterface`
- **`MethodChannelCh34`** - 平台接口的具体实现,通过 MethodChannel 和 EventChannel 与 Android 原生端通信
- **`Ch34Exception`** - 插件自定义异常类
### 通信通道
| 通道类型 | 名称 | 用途 |
|---------|------|------|
| MethodChannel | `ch34` | 双向方法调用 |
| EventChannel | `ch34/data` | 串口数据推送 |
| EventChannel | `ch34/modem` | Modem 状态变化 |
| EventChannel | `ch34/usb_state` | USB 插拔状态 |
### Android 原生端
```
android/src/main/java/com/example/ch34/
├── Ch34Plugin.java # Flutter 插件主类,注册 MethodChannel/EventChannel
├── Ch34DataStreamHandler.java # 数据 EventChannel 的 StreamHandler
├── Ch34ModemStreamHandler.java # Modem EventChannel 的 StreamHandler
├── Ch34UsbStateStreamHandler.java # USB 状态 EventChannel 的 StreamHandler
└── Ch34TypeConverter.java # Dart/Java 类型转换工具
```
### 数据流
```
Flutter App → Ch34Manager → Ch34Platform.instance → MethodChannelCh34 → MethodChannel → Android (Ch34Plugin)
```
### 类型系统 (`ch34_types.dart`)
- **枚举**: `DataBits`, `StopBits`, `Parity`, `GpioDirection`, `GpioValue`, `SerialErrorType`
- **数据类**: `GpioStatus`, `SerialParameter`, `ChipMasterFrequency`, `ModemStatus`, `UsbDeviceInfo`
- 所有数据类提供 `fromMap`/`toMap` 方法用于平台间序列化
## 测试策略
- `test/ch34_test.dart` - 使用 Mock 平台实例测试 `Ch34Manager` 接口
- `test/ch34_method_channel_test.dart` - 使用 BinaryMessenger 模拟测试 MethodChannel 调用
- 测试遵循 flutter plugin 的标准测试模式
## 示例应用
`example/` 目录包含完整的示例应用,展示设备扫描、打开、数据发送/接收的基本流程。
## 用户约束
- Flutter 项目中禁止使用 `Get.snackbar`,使用 `easyloading` 代替
- 所有方法需添加方法级注释
- 遵循高内聚低耦合设计原则