libserialport_plus 1.0.1
libserialport_plus: ^1.0.1 copied to clipboard
A Dart wrapper (FFI package) for the libserialport library. This package provides a simple API for communicating over serial ports.
libserialport_plus #
A Flutter wrapper (FFI plugin) for the libserialport library.
This package provides a simple API for communicating over serial ports.
Features #
- Cross-platform support for Android, Linux, macOS and Windows.
- Reading and writing bytes to serial ports.
- Reading bytes from serial ports in a stream.
- Getting a list of available serial ports.
- Getting information about serial ports.
- Getting and setting serial port settings (baud rate, data bits, parity, stop bits, etc.).
Platform support #
| Platform | Supported |
|---|---|
| Windows | ✅ |
| Linux | ✅ |
| macOS | ✅ |
| Android | ✅ |
| iOS | ❌ |
| Web | ❌ |
On unsupported platforms the native library is simply not built, so an app that
targets, for example, both iOS and Windows will still compile for iOS. Serial
functionality must be guarded by a platform check (e.g. Platform.isWindows);
calling into the library on an unsupported platform fails at runtime.
Usage #
Add libserialport_plus as a dependency in your pubspec.yaml file.
flutter pub add libserialport_plus
Import the package in your Dart code:
import 'package:libserialport_plus/libserialport_plus.dart';
// Get a list of available serial ports
List<String> ports = SerialPort.getAvailablePorts();
// Create a serial port instance (MUST BE DISPOSED AFTER USE)
SerialPort port = SerialPort("COM3");
// Get information about the serial port
SerialPortInfo info = port.getInfo();
// Open the serial port
port.open(SerialPortMode.readWrite);
// Check if the serial port is open
bool isOpen = port.isOpen();
// Read bytes from the serial port
Uint8List bytes = port.read(1024);
// Write some bytes to the serial port
port.write(bytes);
// Close the serial port
port.close();
// Dispose the serial port
port.dispose();
Reader #
// Create and open a serial port
SerialPort port = SerialPort("COM3");
port.open();
SerialPortReader reader = SerialPortReader(port);
reader.stream.listen((Uint8List bytes) {
// Do something with the bytes
});
// After you are done, close the reader
reader.close();
macOS #
If creating an app for macOS, serial permissions are required. Enable this by adding the following two lines to DebugProfile.entitlements and Release.entitlements:
<key>com.apple.security.device.serial</key>
<true/>
Development #
To get started you need to initialize the libserialport submodule with:
git submodule update --init --recursive
The native code is compiled at build time by the Dart/Flutter code-assets build
hook in hook/build.dart, which uses package:native_toolchain_c to build
libserialport for the target platform. Platforms without a serial backend
(such as iOS and web) are skipped by the hook, so no native asset is produced
for them and consuming apps still build.
Binding to native code #
To use the native code, bindings in Dart are needed.
To avoid writing these by hand, they are generated from the header file
(src/libserialport/libserialport.h) by package:ffigen.
Regenerate the bindings by running dart run ffigen --config ffigen.yaml.