feat(ch934x_serial): 添加多端口并发轮询流功能
- 新增多端口并发轮询流 portDataStream 方法,支持每周期轮询 8 个端口 - 修改 read 方法增加 serialPortIndex 参数,支持指定端口读取数据 - 更新 Android 原生实现,支持通过 serialPortIndex 参数指定读取端口 - 移除 IDataCallback 注册逻辑,改用轮询方式拉取数据避免缓冲区冲突 - 添加主线程切换机制,确保异常推送在主线程执行 - 更新 pubspec.yaml 添加 ch934x_serial 本地插件依赖 - 更新项目路线图,标记 Phase 42 完成,进度更新至 50%
This commit is contained in:
@@ -107,6 +107,29 @@ class Ch934xSerial {
|
||||
}
|
||||
}
|
||||
|
||||
/// 多端口并发轮询流:每周期轮询 8 个端口各一次,返回 (portIndex, data) 元组。
|
||||
///
|
||||
/// 各端口数据互不干扰,消费者端需要用每个端口独立的缓冲区拆分 \r\n 行。
|
||||
/// 不传 [ports] 时默认轮询 0..7 全部端口。
|
||||
Stream<(int, Uint8List)> portDataStream({
|
||||
int chunkSize = 1024,
|
||||
Duration interval = const Duration(milliseconds: 25),
|
||||
List<int> ports = const [0, 1, 2, 3, 4, 5, 6, 7],
|
||||
}) async* {
|
||||
if (chunkSize <= 0) {
|
||||
throw ArgumentError.value(chunkSize, 'chunkSize', '必须大于 0');
|
||||
}
|
||||
while (true) {
|
||||
for (final port in ports) {
|
||||
final chunk = await _platform.read(chunkSize, serialPortIndex: port);
|
||||
if (chunk.isNotEmpty) {
|
||||
yield (port, chunk);
|
||||
}
|
||||
}
|
||||
await Future<void>.delayed(interval);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GPIO
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -124,14 +124,15 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> read(int length) async {
|
||||
Future<Uint8List> read(int length, {int? serialPortIndex}) async {
|
||||
if (length <= 0) {
|
||||
return Uint8List(0);
|
||||
}
|
||||
final raw = await methodChannel.invokeMethod<Uint8List>(
|
||||
'read',
|
||||
<String, Object>{'length': length},
|
||||
);
|
||||
final args = <String, Object>{'length': length};
|
||||
if (serialPortIndex != null) {
|
||||
args['serialPortIndex'] = serialPortIndex;
|
||||
}
|
||||
final raw = await methodChannel.invokeMethod<Uint8List>('read', args);
|
||||
return raw ?? Uint8List(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,8 @@ abstract class Ch934xSerialPlatform extends PlatformInterface {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 从串口读取数据(对应 6.1.1 `UsbSerial.read`)。
|
||||
Future<Uint8List> read(int length);
|
||||
/// [serialPortIndex] 可选,指定读取哪个端口的缓冲区;不传则读上次 setActivePort 设置的端口。
|
||||
Future<Uint8List> read(int length, {int? serialPortIndex});
|
||||
|
||||
/// 向串口写入数据(对应 6.2.1 `UsbSerial.write`)。
|
||||
Future<int> write(Uint8List data);
|
||||
|
||||
Reference in New Issue
Block a user