usb_serial 包使 Flutter 应用程序能够使用 FTDI、CDC 和其他协议在 Android 上与 USB UART 设备通信。它提供了一个简单的接口,用于读取和写入 USB 串行端口,非常适合连接传感器、微控制器和工业设备等硬件外设。该插件利用原生 Android USB API,专为需要通过 USB 进行直接硬件交互的开发者设计。
适合场景
连接到USB转串口适配器
从微控制器读取数据
控制工业硬件
与物联网设备集成
开发自定义硬件接口
主要特点
支持 FTDI 和 CDC 协议
简单的读写 API
仅限 Android 的平台支持
原生USB通信
开源实现
适合对象
物联网应用开发者
硬件集成商
嵌入式系统工程师
使用 USB 的 Android 应用开发人员
使用USB连接设备进行原型设计
使用建议
仅限 Android 平台
需要 USB OTG 支持
需要正确的设备权限
可能需要额外的硬件驱动程序
与 iOS 不兼容
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Looking for maintainer
I'm looking for a new maintainer for this project, please indicate your interest here: https://github.com/altera2015/usbserial/issues/121
usb_serial
An Android USB Serial Flutter Plugin
This plugin allows Flutter code to interact with USB serial devices connected to your Android device. For example an FTDI or CDC based USB device.
Getting Started
Add a dependency to your pubspec.yaml
dependencies:
usb_serial: ^0.5.0
include the usbserial package at the top of your dart file.
in the res/xml directory. This will notify your app when one of the specified devices
is plugged in.
Usage of Asynchronous API
...
onPressed: () async {
List<UsbDevice> devices = awaitUsbSerial.listDevices();
print(devices);
UsbPort port;
if (devices.length == 0) {
return;
}
port = await devices[0].create();
bool openResult = await port.open();
if ( !openResult ) {
print("Failed to open");
return;
}
await port.setDTR(true);
await port.setRTS(true);
port.setPortParameters(115200, UsbPort.DATABITS_8,
UsbPort.STOPBITS_1, UsbPort.PARITY_NONE);
// print first result and close port.
port.inputStream.listen((Uint8List event) {
print(event);
port.close();
});
await port.write(Uint8List.fromList([0x10, 0x00]));
}
...
Usage of transaction API
This API is a layer on top of the asynchronous part of the library. It provides two
Stream Transformers and a Transaction helper based on the StreamQueue class.
Terminated Transformer, this splits incoming data based on a configurable end of message bytes "terminator".
Magic Header + Length byte, this splits incoming data based on a configurable header ( with wildcards! ) and a length byte directly following the header.
In case neither is a fit, you can use one of those Transformers to create you own that is specific
to the binary format you are dealing with.
...
Transaction<String> transaction = Transaction.stringTerminated(port.inputStream, Uint8List.fromList([13,10]));
...
// While using transactions you can still listen to all// incoming messages!
transaction.stream.listen( (String data) {
print(data);
});
// you can write asynchronous messages as before!
p.write(Uint8List.fromList([65,66,13,10]));
// BUT you can also write 'transactions'. This is a combination of a flush, write and wait for response// with a timeout. If no response is received within the timeout a null value is returned.// this sends "AB\r\n"var response = await transaction.transaction(p, Uint8List.fromList([65,66,13,10]), Duration(seconds: 1) );
print("The response was $response");
Upgrading from pre-0.3.0
In version 0.3.0 a resource bug was fixed (issue #35) which required signature
changes.
Transformer Class changes
The Transformer classes previously inherited directly from StreamTransformer, this class
however has no dispose method. So a new abstract class was added to include dispose and
called by Transaction.dispose().
If you are using the static factory methods you should not have to make any changes to your
code. Only if you created your own Transformer/Transactions.