1
1
import "dart:io" ;
2
2
import "dart:typed_data" ;
3
3
import "package:burt_network/burt_network.dart" ;
4
- import "package:collection/collection.dart" ;
5
4
import "package:libserialport/libserialport.dart" ;
6
5
7
6
final logger = BurtLogger ();
@@ -11,7 +10,7 @@ bool ascii = false;
11
10
Future <void > listenToDevice (String port) async {
12
11
logger.info ("Connecting to $port ..." );
13
12
final device = SerialDevice (
14
- portName: port,
13
+ portName: port,
15
14
readInterval: const Duration (milliseconds: 100 ),
16
15
logger: logger,
17
16
);
@@ -20,22 +19,29 @@ Future<void> listenToDevice(String port) async {
20
19
return ;
21
20
}
22
21
logger.info ("Connected. Listening..." );
23
- device.stream.listen (process );
22
+ device.stream.listen (processAscii );
24
23
device.startListening ();
25
24
}
26
25
27
26
Future <void > listenToFirmware (String port) async {
28
27
logger.info ("Connecting to $port ..." );
29
28
final device = BurtFirmwareSerial (
30
- port: port,
29
+ port: port,
31
30
logger: logger,
32
31
);
33
32
if (! await device.init ()) {
34
33
logger.critical ("Could not connect to $port " );
34
+ await device.dispose ();
35
35
return ;
36
36
}
37
37
logger.info ("Connected? ${device .isReady }. Listening..." );
38
- device.stream? .listen (process);
38
+ constructor = getDataConstructor (device.device);
39
+ if (constructor == null ) {
40
+ logger.error ("Unsupported serial device: ${device .device .name }" );
41
+ await device.dispose ();
42
+ return ;
43
+ }
44
+ device.rawStream.listen (processFirmware);
39
45
}
40
46
41
47
typedef ProtoConstructor = Message Function (List <int > data);
@@ -55,18 +61,7 @@ void main(List<String> args) async {
55
61
logger.info ("Ports: ${SerialPort .availablePorts }" );
56
62
return ;
57
63
} else if (args.contains ("-h" ) || args.contains ("--help" )) {
58
- logger.info ("Usage: dart run -r :serial [-a | --ascii] [port device]" );
59
- return ;
60
- }
61
- final deviceName = args.last;
62
- final device = Device .values.firstWhereOrNull ((d) => d.name.toLowerCase () == deviceName.toLowerCase ());
63
- if (device == null ) {
64
- logger.error ("Enter a device name as the last argument. Unrecognized device: $deviceName " );
65
- return ;
66
- }
67
- constructor = getDataConstructor (device);
68
- if (constructor == null ) {
69
- logger.error ("Unsupported serial device: $deviceName " );
64
+ logger.info ("Usage: dart run -r :serial [-a | --ascii] [port]" );
70
65
return ;
71
66
}
72
67
var port = args.first;
@@ -82,16 +77,16 @@ void main(List<String> args) async {
82
77
}
83
78
}
84
79
85
- void process (Uint8List buffer) {
86
- if (ascii) {
87
- final s = String .fromCharCodes (buffer).trim ();
88
- logger.debug ("Got string: $s " );
89
- } else {
90
- try {
91
- final data = constructor !(buffer);
92
- logger.debug ("Got data: ${data .toProto3Json ()}" );
93
- } catch (error) {
94
- logger.error ("Could not decode DriveData: $error \n Buffer: $buffer " );
95
- }
80
+ void processFirmware (Uint8List buffer) {
81
+ try {
82
+ final data = constructor !(buffer);
83
+ logger.debug ("Got data: ${data .toProto3Json ()}" );
84
+ } catch (error) {
85
+ logger.error ("Could not decode DriveData: $error \n Buffer: $buffer " );
96
86
}
97
87
}
88
+
89
+ void processAscii (Uint8List buffer) {
90
+ final s = String .fromCharCodes (buffer).trim ();
91
+ logger.debug ("Got string: $s " );
92
+ }
0 commit comments