feat(ch934x): 添加GPIO方向控制和串口参数配置功能

- 添加Ch934xMode和GpioDirection枚举类定义
- 实现GPIO方向控制相关方法(setGpioDir/queryGpioDirFromCache)
- 添加GPIO使能控制(enableGpio)和数量/组数查询(getGpiocount/getGpiogroup)
- 实现串口参数配置(setSerialParameter)包括波特率/数据位/停止位/校验位/流控
- 添加Break信号控制(setBreak)功能
- 实现当前串口模式查询(getCurrentMode)
- 添加设备连接状态查询(isConnected)和已打开设备列表获取(getConnectedDevices)
- 更新文档说明GPIO方向控制和串口参数配置用法
- 在示例应用中添加相关功能按钮和操作逻辑
This commit is contained in:
Developer
2026-07-07 16:15:11 +08:00
parent 4813e79c80
commit 020904bd2a
8 changed files with 765 additions and 9 deletions
+76
View File
@@ -168,4 +168,80 @@ class Ch934xSerial {
await _platform.setExceptionCallback(controller.add);
return subscription;
}
// ---------------------------------------------------------------------------
// 新增接口(文档补充的完整 SDK API)
// ---------------------------------------------------------------------------
/// 设置 Break 信号。
Future<bool> setBreak(bool valid) => _platform.setBreak(valid);
/// 获取当前串口模式。
Future<int> getCurrentMode() => _platform.getCurrentMode();
/// 获取 GPIO 数量。
Future<int> getGpiocount(int deviceId) =>
_platform.getGpiocount(deviceId);
/// 获取 GPIO 组数。
Future<int> getGpiogroup(int deviceId) =>
_platform.getGpiogroup(deviceId);
/// 使能 GPIO。
Future<bool> enableGpio({
required int chipType,
required int gpioGroup,
required int enable,
}) =>
_platform.enableGpio(
chipType: chipType,
gpioGroup: gpioGroup,
enable: enable,
);
/// 设置 GPIO 方向。
Future<bool> setGpioDir({
required int gpioGroup,
required int gpioNumber,
required int dir,
}) =>
_platform.setGpioDir(
gpioGroup: gpioGroup,
gpioNumber: gpioNumber,
dir: dir,
);
/// 从缓存查询 GPIO 方向。
Future<int> queryGpioDirFromCache({
required int gpioGroup,
required int gpioNumber,
}) =>
_platform.queryGpioDirFromCache(
gpioGroup: gpioGroup,
gpioNumber: gpioNumber,
);
/// 查询设备是否已打开。
Future<bool> isConnected(int deviceId) =>
_platform.isConnected(deviceId);
/// 获取当前已打开的设备 ID 列表。
Future<List<int>> getConnectedDevices() =>
_platform.getConnectedDevices();
/// 配置当前活跃串口的参数。
Future<bool> setSerialParameter({
required int baud,
int dataBit = 8,
int stopBit = 1,
int parityBit = 0,
bool flow = false,
}) =>
_platform.setSerialParameter(
baud: baud,
dataBit: dataBit,
stopBit: stopBit,
parityBit: parityBit,
flow: flow,
);
}
+111
View File
@@ -215,6 +215,117 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
);
}
@override
Future<bool> setBreak(bool valid) {
return methodChannel.invokeMethod<bool>(
'setBreak',
<String, Object>{'valid': valid},
).then((v) => v ?? false);
}
@override
Future<int> getCurrentMode() {
return methodChannel.invokeMethod<int>('getCurrentMode')
.then((v) => v ?? -1);
}
@override
Future<int> getGpiocount(int deviceId) {
return methodChannel.invokeMethod<int>(
'getGPIOCount',
<String, Object>{'deviceId': deviceId},
).then((v) => v ?? -1);
}
@override
Future<int> getGpiogroup(int deviceId) {
return methodChannel.invokeMethod<int>(
'getGPIOGroup',
<String, Object>{'deviceId': deviceId},
).then((v) => v ?? -1);
}
@override
Future<bool> enableGpio({
required int chipType,
required int gpioGroup,
required int enable,
}) {
return methodChannel.invokeMethod<bool>(
'enableGPIO',
<String, Object>{
'chipType': chipType,
'gpioGroup': gpioGroup,
'enable': enable,
},
).then((v) => v ?? false);
}
@override
Future<bool> setGpioDir({
required int gpioGroup,
required int gpioNumber,
required int dir,
}) {
return methodChannel.invokeMethod<bool>(
'setGPIODir',
<String, Object>{
'gpioGroup': gpioGroup,
'gpioNumber': gpioNumber,
'dir': dir,
},
).then((v) => v ?? false);
}
@override
Future<int> queryGpioDirFromCache({
required int gpioGroup,
required int gpioNumber,
}) {
return methodChannel.invokeMethod<int>(
'queryGPIODirFromCache',
<String, Object>{
'gpioGroup': gpioGroup,
'gpioNumber': gpioNumber,
},
).then((v) => v ?? -1);
}
@override
Future<bool> isConnected(int deviceId) {
return methodChannel.invokeMethod<bool>(
'isConnected',
<String, Object>{'deviceId': deviceId},
).then((v) => v ?? false);
}
@override
Future<List<int>> getConnectedDevices() async {
final raw = await methodChannel.invokeMethod<List<dynamic>>('getConnectedDevices');
if (raw == null) return const <int>[];
return raw.whereType<int>().toList(growable: false);
}
@override
Future<bool> setSerialParameter({
required int baud,
int dataBit = 8,
int stopBit = 1,
int parityBit = 0,
bool flow = false,
}) {
return methodChannel.invokeMethod<bool>(
'setSerialParameter',
<String, Object>{
'baud': baud,
'dataBit': dataBit,
'stopBit': stopBit,
'parityBit': parityBit,
'flow': flow,
},
).then((v) => v ?? false);
}
/// 释放内部资源,通常仅在测试或热重载场景下使用。
@visibleForTesting
void dispose() {
+56
View File
@@ -112,4 +112,60 @@ abstract class Ch934xSerialPlatform extends PlatformInterface {
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,
});
}
+27
View File
@@ -26,3 +26,30 @@ class Ch934xDeviceType {
/// 未知或非 CH934X 设备。
static const int unknown = -1;
}
/// 串口当前模式,对应 SDK [Mode] 枚举。
///
/// 仅对 CH934X 型号有效(CH348 调用 [Ch934xSerial.getCurrentMode] 无意义)。
class Ch934xMode {
const Ch934xMode._();
/// 普通模式。
static const int normal = 0;
/// 硬件流控模式。
static const int hardflow = 1;
/// GPIO 模式。
static const int gpio = 2;
}
/// GPIO 方向,对应 SDK [GPIO_DIR] 枚举。
class GpioDirection {
const GpioDirection._();
/// 输入方向。
static const int in_ = 0;
/// 输出方向。
static const int out = 1;
}