- 添加Ch934xMode和GpioDirection枚举类定义 - 实现GPIO方向控制相关方法(setGpioDir/queryGpioDirFromCache) - 添加GPIO使能控制(enableGpio)和数量/组数查询(getGpiocount/getGpiogroup) - 实现串口参数配置(setSerialParameter)包括波特率/数据位/停止位/校验位/流控 - 添加Break信号控制(setBreak)功能 - 实现当前串口模式查询(getCurrentMode) - 添加设备连接状态查询(isConnected)和已打开设备列表获取(getConnectedDevices) - 更新文档说明GPIO方向控制和串口参数配置用法 - 在示例应用中添加相关功能按钮和操作逻辑
172 lines
6.5 KiB
Dart
172 lines
6.5 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
|
|
|
import 'ch934x_serial_method_channel.dart';
|
|
import 'src/models/models.dart';
|
|
|
|
/// CH934X 插件的平台无关抽象接口。
|
|
///
|
|
/// Dart 侧应面向此接口编程,具体实现由 Android 平台
|
|
/// (MethodChannel) 提供;`set mockMethodCallHandler` 的
|
|
/// 单元测试可以替换该实现以验证上层逻辑。
|
|
abstract class Ch934xSerialPlatform extends PlatformInterface {
|
|
/// 构造 [Ch934xSerialPlatform]。
|
|
Ch934xSerialPlatform() : super(token: _token);
|
|
|
|
static final Object _token = Object();
|
|
|
|
static Ch934xSerialPlatform _instance = MethodChannelCh934xSerial();
|
|
|
|
/// 平台无关实现当前持有的具体后端。
|
|
static Ch934xSerialPlatform get instance => _instance;
|
|
|
|
/// 替换具体实现,常用于单元测试。
|
|
static set instance(Ch934xSerialPlatform instance) {
|
|
PlatformInterface.verifyToken(instance, _token);
|
|
_instance = instance;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 设备查找
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 获取所有已连接的 CH934X 设备信息(对应 4.1.4 `getCH934XDeviceList`)。
|
|
Future<List<Ch934xDeviceInfo>> getDeviceList();
|
|
|
|
/// 获取指定设备的 CH934X 序列号(对应 4.1.1 `CH934XSerialNum`)。
|
|
Future<String?> getSerialNumber(int deviceId);
|
|
|
|
/// 获取指定设备类型(对应 4.1.2 `CH934XDeviceType`)。
|
|
Future<int> getDeviceType(int deviceId);
|
|
|
|
/// 获取指定设备的串口列表(对应 4.1.3 `getCH934XSerialPortList`)。
|
|
Future<List<Ch934xSerialPortInfo>> getSerialPortList(
|
|
int deviceId, {
|
|
required int interfaceNumber,
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 设备打开 / 关闭
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 初始化并打开指定串口(对应 5.1.1 `UsbSerial.init`)。
|
|
Future<bool> openPort(Ch934xPortTarget target);
|
|
|
|
/// 关闭当前线程/会话最近一次打开的串口(对应 5.2.1 `UsbSerial.close`)。
|
|
Future<bool> closePort();
|
|
|
|
/// 切换当前活跃串口。
|
|
///
|
|
/// 同一 UsbDevice 在调用 [openPort] 之后,所有串口都已连
|
|
/// 接;此方法仅切换后续读写/GPIO/Modem 操作的目标串口,
|
|
/// 不重复打开设备。返回是否切换成功。
|
|
Future<bool> setActivePort(int serialPortIndex);
|
|
|
|
/// 主动向系统申请指定设备的 USB 使用权限。
|
|
///
|
|
/// 在用户未授权时弹出系统对话框,等待用户选择后返回结果。
|
|
/// [openPort] 内部已自动处理权限申请,业务方通常无需手动
|
|
/// 调用;仅在希望提前引导用户授权时使用。
|
|
Future<bool> requestUsbPermission(int deviceId);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 串口读写
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 从串口读取数据(对应 6.1.1 `UsbSerial.read`)。
|
|
/// [serialPortIndex] 可选,指定读取哪个端口的缓冲区;不传则读上次 setActivePort 设置的端口。
|
|
Future<Uint8List> read(int length, {int? serialPortIndex});
|
|
|
|
/// 向串口写入数据(对应 6.2.1 `UsbSerial.write`)。
|
|
Future<int> write(Uint8List data);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// GPIO
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 设置 GPIO 输出(对应 7.1.1 `UsbSerial.setGpioOutput`)。
|
|
Future<bool> setGpioOutput({required int gpioNumber, required int level});
|
|
|
|
/// 读取 GPIO 输入(对应 7.1.2 `UsbSerial.getGpioInput`)。
|
|
Future<int> getGpioInput(int gpioNumber);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Modem
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 设置 Modem 控制(对应 7.2.1 `UsbSerial.setModemControl`)。
|
|
Future<bool> setModemControl({required int dtr, required int rts});
|
|
|
|
/// 获取 Modem 状态(对应 7.2.2 `UsbSerial.getModemStatus`)。
|
|
Future<int> getModemStatus();
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 异常回调
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 注册异常回调(对应 7.3.1 `UsbSerial.setExceptionCallback`)。
|
|
///
|
|
/// 当原生层触发异常(例如设备拔出)时,会通过
|
|
/// [onException] 中传入的回调通知调用方。
|
|
Future<void> setExceptionCallback(
|
|
void Function(Ch934xException exception) onException,
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 新增接口(文档补充的完整 SDK API)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 设置 Break 信号(对应 `CH934XManager.setBreak`)。
|
|
Future<bool> setBreak(bool valid);
|
|
|
|
/// 获取当前串口模式(对应 `CH934XManager.getCurrentMode`)。
|
|
/// 返回值见 [Ch934xMode];仅 CH934X 有效,CH348 返回 -1。
|
|
Future<int> getCurrentMode();
|
|
|
|
/// 获取 GPIO 数量(对应 `CH934XManager.getGPIOCount`)。
|
|
Future<int> getGpiocount(int deviceId);
|
|
|
|
/// 获取 GPIO 组数(对应 `CH934XManager.getGPIOGroup`)。
|
|
Future<int> getGpiogroup(int deviceId);
|
|
|
|
/// 使能 GPIO(对应 `CH934XManager.enableGPIO`)。
|
|
/// [chipType] 由 [Ch934xDeviceType] 常量标识,[gpioGroup] 为组号,
|
|
/// [enable] 含义见文档: CH9344 传 1/0 控制整组;CH348 使用位掩码。
|
|
Future<bool> enableGpio({
|
|
required int chipType,
|
|
required int gpioGroup,
|
|
required int enable,
|
|
});
|
|
|
|
/// 设置 GPIO 方向(对应 `CH934XManager.setGPIODir`)。
|
|
/// [dir] 取值见 [GpioDirection]。
|
|
Future<bool> setGpioDir({
|
|
required int gpioGroup,
|
|
required int gpioNumber,
|
|
required int dir,
|
|
});
|
|
|
|
/// 从缓存查询 GPIO 方向(对应 `CH934XManager.queryGPIODirFromCache`)。
|
|
/// 返回值见 [GpioDirection];-1 表示查询失败。
|
|
Future<int> queryGpioDirFromCache({
|
|
required int gpioGroup,
|
|
required int gpioNumber,
|
|
});
|
|
|
|
/// 查询设备是否已打开(对应 `CH934XManager.isConnected`)。
|
|
Future<bool> isConnected(int deviceId);
|
|
|
|
/// 获取当前已打开的设备 ID 列表(对应 `CH934XManager.getConnectedDevices`)。
|
|
Future<List<int>> getConnectedDevices();
|
|
|
|
/// 配置当前活跃串口的参数(对应 `CH934XManager.setSerialParameter`)。
|
|
Future<bool> setSerialParameter({
|
|
required int baud,
|
|
int dataBit = 8,
|
|
int stopBit = 1,
|
|
int parityBit = 0,
|
|
bool flow = false,
|
|
});
|
|
}
|