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
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user