Skip to content

AsCress/flusbserial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flusbserial

flusbserial

A cross-platform USB Serial plugin for Flutter desktop apps (Windows, Linux, macOS).

flusbserial provides direct access to USB serial devices via libusb, bypassing traditional COM ports.

It is inspired and based on code from UsbSerial and quick_usb.

This library is under active development. Please report any issues you encounter.


Supported Devices

  • CP210x
  • CDC ACM
  • CH34x

Prerequisites

This plugin requires libusb to access USB devices.

Windows

By default, Windows does not allow direct USB access via libusb. You can use Zadig to replace your device's driver with WinUSB:

  1. Plug in your USB device.
  2. Open Zadig and select your device from the list.
  3. Choose WinUSB and click Install Driver.

Note: This will replace the existing driver for the selected device. Make sure you select the correct device from the list.

Linux

libusb is usually available by default. If not:

# Ubuntu / Debian
sudo apt-get install libusb-1.0-0

# Fedora / RHEL
sudo dnf install libusb1

Apps built with this plugin require udev rules to access USB devices without root. You can create /etc/udev/rules.d/99-<your-app>.rules with your device's vendor and product IDs:

SUBSYSTEM=="usb|tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="xxxx", MODE="666"

Then reload the rules:

sudo udevadm control --reload-rules && sudo udevadm trigger

macOS

libusb is usually available by default. If not:

brew install libusb

Installing

  1. Add dependency to pubspec.yaml

    Get the latest version from the 'Installing' tab on pub.dev

dependencies:
    flusbserial: <latest_version>

Note: Based upon the other dependencies included in your project, you may have to add a dependency override for the ffi package.

  1. Import the package
import 'package:flusbserial/flusbserial.dart';

Usage

Here are some examples to show the usage:

Initialize plugin

UsbSerialDevice.init();

List available devices

List<UsbDevice> devices = await UsbSerialDevice.listDevices();

Instantiate a new object of the UsbSerialDevice class

UsbDevice? device;
...
// Auto-detect interface
UsbSerialDevice? mDevice = UsbSerialDevice.createDevice(device);

// Specific interface
UsbSerialDevice? mDevice = UsbSerialDevice.createDevice(device, interfaceId: 0);

// Specific driver (eg:- CDC ACM)
UsbSerialDevice? mDevice = UsbSerialDevice.createDevice(device, type: UsbSerialDevice.cdc);

Open a device and set it up

await mDevice.open();
await mDevice.setBaudRate(1000000);
await mDevice.setDataBits(UsbSerialInterface.dataBits8);
await mDevice.setStopBits(UsbSerialInterface.stopBits1);
await mDevice.setParity(UsbSerialInterface.parityNone);

Set flow control if needed (only supported in CP210x & CH34x devices)

await mDevice.setFlowControl(UsbSerialInterface.flowControlRtsCts);

Read / Write

int bytesWritten = await mDevice.write(data, timeout);

Uint8List bytesRead = await mDevice.read(bytesToRead, timeout);

Change the state of DTR/RTS lines

await mDevice.setDtr(true);
await mDevice.setDtr(false);
await mDevice.setRts(true);
await mDevice.setRts(false);

Set Auto Detach Kernel Driver (only for Linux)

UsbSerialDevice.setAutoDetachKernelDriver(true);

Close the device

await mDevice.close();

License

MIT License

Copyright (c) 2025 Anashuman Singh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

A cross-platform USB serial plugin for Flutter desktop apps (Windows, Linux, macOS).

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors