feat(ch934x_serial): 添加串口写入功能的端口索引参数

- write方法新增可选的serialPortIndex参数,支持直接写入指定端口
- 传入端口索引时不会改变当前活跃端口,保持原有活跃端口行为
- 添加对负数端口索引的验证处理,直接返回零避免异常
- 更新文档说明多端口并发客户端应使用显式端口写入方式
- 增加相关单元测试覆盖新参数的各种使用场景
- 同步更新Android原生层实现以支持端口索引传递
This commit is contained in:
2026-07-13 20:51:27 +08:00
parent 020904bd2a
commit 7fe87f8d7c
10 changed files with 137 additions and 73 deletions
+14 -11
View File
@@ -14,8 +14,7 @@ import 'src/models/models.dart';
class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
/// 测试时可被替换的 MethodChannel。
@visibleForTesting
final MethodChannel methodChannel =
const MethodChannel('ch934x_serial');
final MethodChannel methodChannel = const MethodChannel('ch934x_serial');
/// 通知 Dart 侧的异常事件流,用于支持 `setExceptionCallback`。
final StreamController<Ch934xException> _exceptionController =
@@ -39,7 +38,8 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
@override
Future<List<Ch934xDeviceInfo>> getDeviceList() async {
final raw = await methodChannel.invokeMethod<List<dynamic>>('getDeviceList');
final raw =
await methodChannel.invokeMethod<List<dynamic>>('getDeviceList');
if (raw == null) {
return const <Ch934xDeviceInfo>[];
}
@@ -137,14 +137,15 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
}
@override
Future<int> write(Uint8List data) async {
if (data.isEmpty) {
Future<int> write(Uint8List data, {int? serialPortIndex}) async {
if (data.isEmpty || (serialPortIndex != null && serialPortIndex < 0)) {
return 0;
}
final result = await methodChannel.invokeMethod<int>(
'write',
<String, Object>{'data': data},
);
final args = <String, Object>{'data': data};
if (serialPortIndex != null) {
args['serialPortIndex'] = serialPortIndex;
}
final result = await methodChannel.invokeMethod<int>('write', args);
return result ?? 0;
}
@@ -225,7 +226,8 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
@override
Future<int> getCurrentMode() {
return methodChannel.invokeMethod<int>('getCurrentMode')
return methodChannel
.invokeMethod<int>('getCurrentMode')
.then((v) => v ?? -1);
}
@@ -301,7 +303,8 @@ class MethodChannelCh934xSerial extends Ch934xSerialPlatform {
@override
Future<List<int>> getConnectedDevices() async {
final raw = await methodChannel.invokeMethod<List<dynamic>>('getConnectedDevices');
final raw =
await methodChannel.invokeMethod<List<dynamic>>('getConnectedDevices');
if (raw == null) return const <int>[];
return raw.whereType<int>().toList(growable: false);
}