1
This commit is contained in:
68
lib/ch34.dart
Normal file
68
lib/ch34.dart
Normal file
@@ -0,0 +1,68 @@
|
||||
/// CH34 Flutter 插件。
|
||||
///
|
||||
/// 用于与 WCH CH34X 系列 USB 转串口芯片进行通信。
|
||||
///
|
||||
/// 支持的芯片:CH340/CH341/CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104/CH9143
|
||||
///
|
||||
/// 使用方法:
|
||||
/// ```dart
|
||||
/// import 'package:ch34/ch34.dart';
|
||||
///
|
||||
/// // 初始化插件(必须在首次调用 Ch34Manager 前调用)
|
||||
/// Ch34.ensureInitialized();
|
||||
///
|
||||
/// // 枚举设备
|
||||
/// final devices = await Ch34Manager.enumDevice();
|
||||
///
|
||||
/// // 打开设备
|
||||
/// await Ch34Manager.openDevice(devices.first.deviceName);
|
||||
///
|
||||
/// // 设置参数
|
||||
/// await Ch34Manager.setSerialParameter(
|
||||
/// devices.first.deviceName,
|
||||
/// 0,
|
||||
/// const SerialParameter(baud: 9600),
|
||||
/// );
|
||||
///
|
||||
/// // 发送数据
|
||||
/// await Ch34Manager.writeData(
|
||||
/// devices.first.deviceName,
|
||||
/// 0,
|
||||
/// Uint8List.fromList([0x01, 0x02]),
|
||||
/// );
|
||||
///
|
||||
/// // 注册数据回调
|
||||
/// Ch34Manager.registerDataCallback(
|
||||
/// devices.first.deviceName,
|
||||
/// 0,
|
||||
/// (data) { print('Received: $data'); },
|
||||
/// );
|
||||
/// ```
|
||||
library ch34;
|
||||
|
||||
export 'src/ch34_method_channel.dart' show Ch34Exception;
|
||||
export 'src/ch34_platform_interface.dart' show Ch34Platform;
|
||||
export 'src/types/ch34_types.dart';
|
||||
export 'src/ch34_manager.dart';
|
||||
|
||||
import 'src/ch34_method_channel.dart';
|
||||
|
||||
/// CH34 插件入口类。
|
||||
///
|
||||
/// 提供插件初始化方法,确保平台实例已注册。
|
||||
class Ch34 {
|
||||
Ch34._();
|
||||
|
||||
static bool _initialized = false;
|
||||
|
||||
/// 初始化 CH34 插件。
|
||||
///
|
||||
/// 必须在首次调用 [Ch34Manager] 方法前调用此方法。
|
||||
/// 重复调用不会产生副作用。
|
||||
static void ensureInitialized() {
|
||||
if (!_initialized) {
|
||||
MethodChannelCh34.registerDefault();
|
||||
_initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user