feat(ch934x_serial): 添加串口写入功能的端口索引参数
- write方法新增可选的serialPortIndex参数,支持直接写入指定端口 - 传入端口索引时不会改变当前活跃端口,保持原有活跃端口行为 - 添加对负数端口索引的验证处理,直接返回零避免异常 - 更新文档说明多端口并发客户端应使用显式端口写入方式 - 增加相关单元测试覆盖新参数的各种使用场景 - 同步更新Android原生层实现以支持端口索引传递
This commit is contained in:
+11
-13
@@ -29,16 +29,14 @@ class Ch934xSerial {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 获取所有已连接的 CH934X 设备信息。
|
||||
Future<List<Ch934xDeviceInfo>> getDeviceList() =>
|
||||
_platform.getDeviceList();
|
||||
Future<List<Ch934xDeviceInfo>> getDeviceList() => _platform.getDeviceList();
|
||||
|
||||
/// 获取指定设备序列号;非 CH934X 设备时返回 null。
|
||||
Future<String?> getSerialNumber(int deviceId) =>
|
||||
_platform.getSerialNumber(deviceId);
|
||||
|
||||
/// 获取指定设备类型,取值见 [Ch934xDeviceType]。
|
||||
Future<int> getDeviceType(int deviceId) =>
|
||||
_platform.getDeviceType(deviceId);
|
||||
Future<int> getDeviceType(int deviceId) => _platform.getDeviceType(deviceId);
|
||||
|
||||
/// 获取指定设备的串口列表。
|
||||
Future<List<Ch934xSerialPortInfo>> getSerialPortList(
|
||||
@@ -85,7 +83,11 @@ class Ch934xSerial {
|
||||
Future<Uint8List> read(int length) => _platform.read(length);
|
||||
|
||||
/// 写入数据,返回实际写入的字节数。
|
||||
Future<int> write(Uint8List data) => _platform.write(data);
|
||||
///
|
||||
/// [serialPortIndex] 为空时沿用当前活跃串口;传入时直接写入指定端口,
|
||||
/// 不会改变当前活跃串口。
|
||||
Future<int> write(Uint8List data, {int? serialPortIndex}) =>
|
||||
_platform.write(data, serialPortIndex: serialPortIndex);
|
||||
|
||||
/// 构造一个持续从串口拉取数据的 `Stream<Uint8List>`。
|
||||
///
|
||||
@@ -180,12 +182,10 @@ class Ch934xSerial {
|
||||
Future<int> getCurrentMode() => _platform.getCurrentMode();
|
||||
|
||||
/// 获取 GPIO 数量。
|
||||
Future<int> getGpiocount(int deviceId) =>
|
||||
_platform.getGpiocount(deviceId);
|
||||
Future<int> getGpiocount(int deviceId) => _platform.getGpiocount(deviceId);
|
||||
|
||||
/// 获取 GPIO 组数。
|
||||
Future<int> getGpiogroup(int deviceId) =>
|
||||
_platform.getGpiogroup(deviceId);
|
||||
Future<int> getGpiogroup(int deviceId) => _platform.getGpiogroup(deviceId);
|
||||
|
||||
/// 使能 GPIO。
|
||||
Future<bool> enableGpio({
|
||||
@@ -222,12 +222,10 @@ class Ch934xSerial {
|
||||
);
|
||||
|
||||
/// 查询设备是否已打开。
|
||||
Future<bool> isConnected(int deviceId) =>
|
||||
_platform.isConnected(deviceId);
|
||||
Future<bool> isConnected(int deviceId) => _platform.isConnected(deviceId);
|
||||
|
||||
/// 获取当前已打开的设备 ID 列表。
|
||||
Future<List<int>> getConnectedDevices() =>
|
||||
_platform.getConnectedDevices();
|
||||
Future<List<int>> getConnectedDevices() => _platform.getConnectedDevices();
|
||||
|
||||
/// 配置当前活跃串口的参数。
|
||||
Future<bool> setSerialParameter({
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,9 @@ abstract class Ch934xSerialPlatform extends PlatformInterface {
|
||||
Future<Uint8List> read(int length, {int? serialPortIndex});
|
||||
|
||||
/// 向串口写入数据(对应 6.2.1 `UsbSerial.write`)。
|
||||
Future<int> write(Uint8List data);
|
||||
///
|
||||
/// [serialPortIndex] 为空时使用当前活跃端口;传入时直接写入指定端口。
|
||||
Future<int> write(Uint8List data, {int? serialPortIndex});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GPIO
|
||||
|
||||
Reference in New Issue
Block a user