1
1
import 'dart:typed_data' ;
2
2
3
- import 'package:flutter_libserialport/flutter_libserialport .dart' ;
3
+ import 'package:flusbserial/flusbserial .dart' ;
4
4
import 'package:pslab/communication/handler/base.dart' ;
5
5
import 'package:pslab/others/logger_service.dart' ;
6
6
@@ -9,7 +9,7 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
9
9
static const int pslabProductIdV5 = 223 ;
10
10
static const int pslabVendorIdV6 = 0x10C4 ;
11
11
static const int pslabProductIdV6 = 0xEA60 ;
12
- SerialPort ? mPort ;
12
+ UsbSerialDevice ? mDevice ;
13
13
14
14
@override
15
15
bool connected = false ;
@@ -19,22 +19,22 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
19
19
20
20
@override
21
21
void close () {
22
- if (! connected || mPort == null ) return ;
23
- mPort ? .close ();
22
+ if (! connected || mDevice == null ) return ;
23
+ mDevice ? .close ();
24
24
connected = false ;
25
25
}
26
26
27
27
@override
28
28
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)) {
36
36
deviceFound = true ;
37
- mPort = port ;
37
+ mDevice = UsbSerialDevice . createDevice (device) ;
38
38
break ;
39
39
}
40
40
}
@@ -60,11 +60,11 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
60
60
if (! deviceFound) {
61
61
throw Exception ("Device not connected" );
62
62
}
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) ;
68
68
connected = true ;
69
69
}
70
70
@@ -74,14 +74,16 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
74
74
int bytesToBeReadTemp = bytesToRead;
75
75
try {
76
76
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 " );
79
81
if (readNow == 0 ) {
80
82
logger.e ("Read Error: $bytesToBeReadTemp " );
81
83
return numBytesRead;
82
84
} 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! );
85
87
numBytesRead += readLength;
86
88
bytesToBeReadTemp -= readLength;
87
89
}
@@ -96,7 +98,6 @@ class DesktopUSBCommunicationHandler implements CommunicationHandler {
96
98
97
99
@override
98
100
void write (Uint8List src, int timeoutMillis) {
99
- mPort? .write (src, timeout: 0 );
100
- mPort? .flush ();
101
+ mDevice? .write (src, timeoutMillis);
101
102
}
102
103
}
0 commit comments