serial_port_win32 0.4.9
serial_port_win32: ^0.4.9 copied to clipboard

A SerialPort library using win32 API, for connecting real device by serial port. Only supports Windows.

serial_port_win32 #

A SerialPort library using win32 API.

pub

Getting Started #

Get Ports #

final ports = SerialPort.getAvailablePorts();
print(ports);
/// result like [COM3, COM4]

Create Serial Port #

The port instance is Singleton Pattern. Don't re-create port for same Com name.

final port = SerialPort("COM5", openNow: false, ByteSize: 8);
// port.open()
port.openWithSettings(BaudRate: CBR_115200);
// final port = SerialPort("COM5"); /// auto open with default settings

Set parameters #

port.BaudRate = CBR_115200;
port.ByteSize = 8;
port.StopBits = ONESTOPBIT;
port.Parity = NOPARITY;
port.ReadIntervalTimeout = 10;
/// and so on, parameters like win32.

Read #

port.readBytesOnListen(8, (value) => print(value));
// or
port.readOnListenFunction = (value) {
  print(value);
};
// port.readOnListenFunction = (value) {
//   print(value);
// };
// Future.delayed(Duration(seconds: 5)).then((value) {
//   port.writeBytesFromString('close');
//   sleep(Duration(seconds: 1));
//   port.close();
// });

Write #

Write String

String buffer = "hello";
port.writeBytesFromString(buffer);

Write Uint8List

final uint8_data = Uint8List.fromList([1, 2, 3, 4, 5, 6]);
print(port.writeBytesFromUint8List(uint8_data));

Get Port Connection Status #

port.isOpened == false;

Close Serial Port #

Close Without Listen

port.close();

Close On Listen

port.closeOnListen(
  onListen: () => print(port.isOpened),
)
  ..onError((err) {
    print(err);
  })
  ..onDone(() {
    print("is closed");
    print(port.isOpened);
  });

Small Example #

import 'package:serial_port_win32/src/serial_port.dart';

void main() {
    var ports = SerialPort.getAvailablePorts();
    print(ports);
    if(ports.isNotEmpty){
      var port = SerialPort(ports[0]);
      port.BaudRate = CBR_115200;
      port.StopBits = ONESTOPBIT;
      port.close();
    }
}
7
likes
120
pub points
70%
popularity

A SerialPort library using win32 API, for connecting real device by serial port. Only supports Windows.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

ffi, win32

More

Packages that depend on serial_port_win32