Skip to content

Commit 4dae625

Browse files
AsCressmarcnause
authored andcommitted
feat: migrate to flusbserial
1 parent aa164fa commit 4dae625

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

lib/communication/commands_proto.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import 'dart:io';
2+
13
class CommandsProto {
24
int acknowledge = 254;
35
int maxSamples = 10000;
4-
int dataSplitting = 60;
6+
late int dataSplitting;
57

68
int flash = 1;
79
int readFlash = 1;
@@ -200,4 +202,12 @@ class CommandsProto {
200202
int passUart = 1;
201203

202204
int stopStreaming = 253;
205+
206+
CommandsProto() {
207+
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
208+
dataSplitting = 30;
209+
} else {
210+
dataSplitting = 60;
211+
}
212+
}
203213
}

lib/communication/handler/desktop_comms_handler.dart

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:typed_data';
22

3-
import 'package:flutter_libserialport/flutter_libserialport.dart';
3+
import 'package:flusbserial/flusbserial.dart';
44
import 'package:pslab/communication/handler/base.dart';
55
import 'package:pslab/others/logger_service.dart';
66

@@ -9,7 +9,7 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
99
static const int pslabProductIdV5 = 223;
1010
static const int pslabVendorIdV6 = 0x10C4;
1111
static const int pslabProductIdV6 = 0xEA60;
12-
SerialPort? mPort;
12+
UsbSerialDevice? mDevice;
1313

1414
@override
1515
bool connected = false;
@@ -19,22 +19,22 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
1919

2020
@override
2121
void close() {
22-
if (!connected || mPort == null) return;
23-
mPort?.close();
22+
if (!connected || mDevice == null) return;
23+
mDevice?.close();
2424
connected = false;
2525
}
2626

2727
@override
2828
Future<void> initialize() async {
29-
List<String> addresses = SerialPort.availablePorts;
30-
for (final address in addresses) {
31-
final port = SerialPort(address);
32-
if ((port.vendorId == pslabVendorIdV5 &&
33-
port.productId == pslabProductIdV5) ||
34-
(port.vendorId == pslabVendorIdV6 &&
35-
port.productId == pslabProductIdV6)) {
29+
UsbSerialDevice.init();
30+
List<UsbDevice> availableDevices = await UsbSerialDevice.listDevices();
31+
for (final device in availableDevices) {
32+
if ((device.vendorId == pslabVendorIdV5 &&
33+
device.productId == pslabProductIdV5) ||
34+
(device.vendorId == pslabVendorIdV6 &&
35+
device.productId == pslabProductIdV6)) {
3636
deviceFound = true;
37-
mPort = port;
37+
mDevice = UsbSerialDevice.createDevice(device);
3838
break;
3939
}
4040
}
@@ -60,11 +60,11 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
6060
if (!deviceFound) {
6161
throw Exception("Device not connected");
6262
}
63-
mPort?.openReadWrite();
64-
mPort?.config.baudRate = 1000000;
65-
mPort?.config.bits = 8;
66-
mPort?.config.stopBits = 1;
67-
mPort?.config.parity = SerialPortParity.none;
63+
await mDevice?.open();
64+
await mDevice?.setBaudRate(1000000);
65+
await mDevice?.setDataBits(UsbSerialInterface.dataBits8);
66+
await mDevice?.setStopBits(UsbSerialInterface.stopBits1);
67+
await mDevice?.setParity(UsbSerialInterface.parityNone);
6868
connected = true;
6969
}
7070

@@ -74,14 +74,16 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
7474
int bytesToBeReadTemp = bytesToRead;
7575
try {
7676
while (numBytesRead < bytesToRead) {
77-
Uint8List receivedData = mPort!.read(bytesToBeReadTemp, timeout: 0);
78-
int readNow = receivedData.length;
77+
Uint8List? receivedData =
78+
await mDevice?.read(bytesToBeReadTemp, timeoutMillis);
79+
int? readNow = receivedData?.length;
80+
logger.d("Received chunk: $receivedData");
7981
if (readNow == 0) {
8082
logger.e("Read Error: $bytesToBeReadTemp");
8183
return numBytesRead;
8284
} else {
83-
int readLength = readNow.clamp(0, bytesToBeReadTemp);
84-
dest.setRange(numBytesRead, numBytesRead + readLength, receivedData);
85+
int readLength = readNow!.clamp(0, bytesToBeReadTemp);
86+
dest.setRange(numBytesRead, numBytesRead + readLength, receivedData!);
8587
numBytesRead += readLength;
8688
bytesToBeReadTemp -= readLength;
8789
}
@@ -96,7 +98,6 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
9698

9799
@override
98100
void write(Uint8List src, int timeoutMillis) {
99-
mPort?.write(src, timeout: 0);
100-
mPort?.flush();
101+
mDevice?.write(src, timeoutMillis);
101102
}
102103
}

pubspec.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ dependencies:
6969
path_provider: ^2.1.5
7070
file_picker: ^10.3.2
7171
flutter_screenutil: ^5.9.3
72-
flutter_libserialport: ^0.6.0
72+
flusbserial: ^0.1.1
73+
74+
dependency_overrides:
75+
ffi: ^2.1.3
7376

7477

7578
dev_dependencies:

0 commit comments

Comments
 (0)