flutter_usb_serial 0.0.2 copy "flutter_usb_serial: ^0.0.2" to clipboard
flutter_usb_serial: ^0.0.2 copied to clipboard

A Flutter project for CH340 on Android and Windows.

flutter_serial_port_to_usb #

U转串Demo

Getting Started #

CH340芯片:在windows平台可直接视为串口使用,在android平台被视为Usb设备。 windows平台 CH340官方给了一个驱动文件https://www.wch.cn/downloads/CH341SER_EXE.html(安装完又卸了程序正常运行)

windows平台使用插件 :https://pub.flutter-io.cn/packages/serial_port_win32 android平台使用插件:https://pub.dev/packages/usb_serial

serial_port_win32: ^0.5.2 usb_serial: ^0.3.0

使用方式封装为usb_serial包下内容; 基本方法有:

abstract class UsbSerial {
  ///初始化
  create();

  ///打开串口
  Future<bool> open();

  ///关闭串口
  Future<bool> close();

  ///写入字节列表
  Future<void> write(Uint8List data) async {}

  ///写入字符串
  Future<void> writeString(String data) async {}

  ///设置读数监听
  void readByteOnListen(Function(Uint8List value) onData);

  ///串口状态
  bool isOpen();

  ///设置参数
  Future<void> setPortParameters() async {}

  ///获取可用串口
  Future<List<String>> getAvailablePorts();
}

使用案例(同main.dart type值为3时):

UsbSerial usbSerialWindows = UsbSerialFactory().getUsbSerial();//封装的结果
usbSerialWindows.create();//初始化
debugPrint('MyTest ${(await usbSerialWindows.getAvailablePorts()).toString()}');//打印可用串口

usbSerialWindows.open();//打开串口
///读数据
usbSerialWindows.readByteOnListen((value) {
print(value);
// String str = utf8.decode(value.sublist(0, value.length - 1));
String str = utf8.decode(value);
print(str);
setState(() {
receive = str;
});
});
usbSerialWindows.writeString(editingController.text);//写数据
usbSerialWindows.close();//关闭串口

##android平台需要申请权限:

        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>

        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<usb-device vendor-id="6790" product-id="29987" />

<!-- 0x0403 / 0x6015: FTDI FT231X -->
<usb-device vendor-id="1027" product-id="24597" />

<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />

<!-- 0x16C0 / 0x0483: Teensyduino  -->
<usb-device vendor-id="5824" product-id="1155" />

<!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
<usb-device vendor-id="4292" product-id="60000" />

<!-- 0x067B / 0x2303: Prolific PL2303 -->
<usb-device vendor-id="1659" product-id="8963" />

<!-- 0x1366 / 0x0105: Segger JLink -->
<usb-device vendor-id="4966" product-id="261" />

<!-- 0x1366 / 0x0105: CH340 JLink -->
<usb-device vendor-id="1A86" product-id="7523" />

</resources>

用代码手动申请权限应该也是可以的,我没有尝试

7
likes
100
pub points
73%
popularity

Publisher

unverified uploader

A Flutter project for CH340 on Android and Windows.

Homepage

Documentation

API reference

License

Apache-2.0 (LICENSE)

Dependencies

flutter, serial_port_win32, usb_serial

More

Packages that depend on flutter_usb_serial