feat(ch934x_serial): 添加串口写入功能的端口索引参数
- write方法新增可选的serialPortIndex参数,支持直接写入指定端口 - 传入端口索引时不会改变当前活跃端口,保持原有活跃端口行为 - 添加对负数端口索引的验证处理,直接返回零避免异常 - 更新文档说明多端口并发客户端应使用显式端口写入方式 - 增加相关单元测试覆盖新参数的各种使用场景 - 同步更新Android原生层实现以支持端口索引传递
This commit is contained in:
@@ -45,6 +45,7 @@ class _MockCh934xSerialPlatform extends Ch934xSerialPlatform
|
||||
bool closeResult;
|
||||
Uint8List? readBytes;
|
||||
int writeResult;
|
||||
int? lastWritePort;
|
||||
bool gpioOutputResult;
|
||||
int gpioInput;
|
||||
bool modemControlResult;
|
||||
@@ -63,8 +64,7 @@ class _MockCh934xSerialPlatform extends Ch934xSerialPlatform
|
||||
Future<List<Ch934xSerialPortInfo>> getSerialPortList(
|
||||
int deviceId, {
|
||||
required int interfaceNumber,
|
||||
}) async =>
|
||||
ports;
|
||||
}) async => ports;
|
||||
|
||||
@override
|
||||
Future<bool> openPort(Ch934xPortTarget target) async => openResult;
|
||||
@@ -79,17 +79,20 @@ class _MockCh934xSerialPlatform extends Ch934xSerialPlatform
|
||||
Future<bool> requestUsbPermission(int deviceId) async => true;
|
||||
|
||||
@override
|
||||
Future<Uint8List> read(int length, {int? serialPortIndex}) async => readBytes ?? Uint8List(0);
|
||||
Future<Uint8List> read(int length, {int? serialPortIndex}) async =>
|
||||
readBytes ?? Uint8List(0);
|
||||
|
||||
@override
|
||||
Future<int> write(Uint8List data) async => writeResult;
|
||||
Future<int> write(Uint8List data, {int? serialPortIndex}) async {
|
||||
lastWritePort = serialPortIndex;
|
||||
return writeResult;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> setGpioOutput({
|
||||
required int gpioNumber,
|
||||
required int level,
|
||||
}) async =>
|
||||
gpioOutputResult;
|
||||
}) async => gpioOutputResult;
|
||||
|
||||
@override
|
||||
Future<int> getGpioInput(int gpioNumber) async => gpioInput;
|
||||
@@ -105,13 +108,19 @@ class _MockCh934xSerialPlatform extends Ch934xSerialPlatform
|
||||
Future<void> setExceptionCallback(
|
||||
void Function(Ch934xException exception) onException,
|
||||
) async {}
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
}
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
test('默认平台实现是 MethodChannelCh934xSerial', () {
|
||||
expect(Ch934xSerialPlatform.instance, isInstanceOf<MethodChannelCh934xSerial>());
|
||||
expect(
|
||||
Ch934xSerialPlatform.instance,
|
||||
isInstanceOf<MethodChannelCh934xSerial>(),
|
||||
);
|
||||
});
|
||||
|
||||
test('Ch934xSerial 委托 platform 调用 getDeviceList', () async {
|
||||
@@ -131,6 +140,20 @@ void main() {
|
||||
expect(list.single.serialNumber, 'SN-1');
|
||||
});
|
||||
|
||||
test('Ch934xSerial write 透传可选串口索引', () async {
|
||||
final mock = _MockCh934xSerialPlatform(writeResult: 3);
|
||||
final plugin = Ch934xSerial.withPlatform(mock);
|
||||
|
||||
expect(await plugin.write(Uint8List.fromList([1, 2, 3])), 3);
|
||||
expect(mock.lastWritePort, isNull);
|
||||
|
||||
expect(
|
||||
await plugin.write(Uint8List.fromList([1, 2, 3]), serialPortIndex: 4),
|
||||
3,
|
||||
);
|
||||
expect(mock.lastWritePort, 4);
|
||||
});
|
||||
|
||||
test('ModemStatus.isSet 按位与工作', () {
|
||||
const status = ModemStatus.cts | ModemStatus.dcd;
|
||||
expect(ModemStatus.isSet(status, ModemStatus.cts), isTrue);
|
||||
|
||||
Reference in New Issue
Block a user