jpnurmi / libserialport.dart

Serial Port for Dart
https://pub.dev/packages/libserialport
GNU Lesser General Public License v3.0
86 stars 35 forks source link

written bytes: 1 then written bytes: 1 #76

Open uevan2 opened 1 year ago

uevan2 commented 1 year ago

I am getting this issue when running the following code:

flutter: Available Ports: [COM3, COM6]
flutter: written bytes: -1

(It sometimes states that written bytes is 1 at the start but then upon a hot restart it prints -1)

`import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
// import 'package:flutter_libserialport/flutter_libserialport.dart';
import 'package:libserialport/libserialport.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<String> availablePort = SerialPort.availablePorts;
    print('Available Ports: $availablePort');
    SerialPort port4 = SerialPort('COM6');
    port4.openReadWrite();
    try {
      print('written bytes: ${port4.write(_stringToUint8List("Hello"))}');

    } on SerialPortError catch (err, _) {
      print(SerialPort.lastError);
      port4.close();
    }
    return Container();
  }
}

Uint8List _stringToUint8List(String s) {
  List<int> codeUnits = s.codeUnits;
  Uint8List uint8list = Uint8List.fromList(codeUnits);
  return uint8list;
}

class SerialPortExample extends StatefulWidget {
  @override
  _SerialPortExampleState createState() => _SerialPortExampleState();
}
`
mingpepe commented 1 year ago

Did you check return value of openReadWrite?