Skip to content

snabble/flutter_libserialport

 
 

Repository files navigation

Serial Port for Flutter

flutter_libserialport is a Flutter plugin that provides cross-platform access to serial ports, utilizing Flutter's build system to build and deploy the libserialport C-library for the target platform.

The Dart API (FFI bindings, port management, configuration, etc.) from the libserialport.dart package has been merged directly into this package to remove the external Git dependency and ensure full Dart 3 compatibility.

Supported platforms

  • Linux
  • macOS
  • Windows
  • Android

Usage

Add flutter_libserialport as a dependency in your pubspec.yaml:

dependencies:
  flutter_libserialport: ^0.7.0

Then import the package:

import 'package:flutter_libserialport/flutter_libserialport.dart';

screenshot

Architecture

The native C library (libserialport) is compiled from source via the platform build systems (CMake for Linux/Windows/Android, CocoaPods + Swift Package Manager for macOS).

The Dart layer uses FFI to communicate with the C library. The FFI bindings are auto-generated from the C header using ffigen.

Regenerating FFI bindings

The file lib/src/bindings.dart is auto-generated and must not be edited manually. To regenerate it after updating the C library sources in third_party/libserialport/:

dart run ffigen --config ffigen.yaml

ffigen configuration notes

The ffigen.yaml uses a carefully chosen enums.include list. Only enums that are truly exclusive (non-bitmask, non-mixed-return) are mapped to Dart enums: sp_mode, sp_parity, sp_rts, sp_cts, sp_dtr, sp_dsr, sp_xonxoff, sp_flowcontrol, sp_transport.

Enums such as sp_return, sp_event, sp_buffer, and sp_signal are intentionally not mapped as Dart enums because:

  • sp_return is also used as a return type for functions that can return byte counts (e.g. sp_input_waiting, sp_nonblocking_read), making the Dart enum's fromValue() throw on positive values.
  • sp_event, sp_buffer, and sp_signal are bitmask flags that can be OR-combined, which is incompatible with exclusive Dart enums.

For these types, raw int-returning native function lookups are provided in lib/src/dylib.dart.

About

Serial Port for Flutter

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Dart 62.9%
  • C++ 14.3%
  • CMake 11.9%
  • C 7.8%
  • Swift 1.8%
  • Kotlin 0.8%
  • Other 0.5%