패키지, 저장소 및 앱 카테고리 검색

패키지 상세v0.5.2

usb_serial

이 플러그인을 사용하면 FTDI 또는 CDC(그리고 더 많은 것!) 프로토콜 기반의 USB UART 하드웨어를 쉽게 사용할 수 있습니다.

Android
30일 다운로드
1.9만
좋아요
194
Pub 점수
150

패키지 설치

flutter pub add usb_serial

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

usb_serial 패키지는 FTDI, CDC 및 기타 프로토콜을 사용하여 안드로이드에서 Flutter 애플리케이션과 USB UART 장치 간의 통신을 가능하게 합니다. USB 시리얼 포트에서 읽기 및 쓰기 작업을 위한 직관적인 인터페이스를 제공하여 센서, 마이크로컨트롤러 및 산업용 장비와 같은 하드웨어 주변기기를 연결하는 데 이상적입니다. 이 플러그인은 네이티브 안드로이드 USB API를 활용하며, USB를 통해 직접 하드웨어와 상호작용해야 하는 앱 개발자를 위해 설계되었습니다.

사용 사례

  • USB-시리얼 어댑터에 연결
  • 마이크로컨트롤러에서 데이터 읽기
  • 산업용 하드웨어 제어
  • 사물인터넷 장치와의 통합
  • 사용자 정의 하드웨어 인터페이스 개발

주요 기능

  • FTDI 및 CDC 프로토콜을 지원합니다
  • 간단한 읽기/쓰기 API
  • Android 전용 플랫폼 지원
  • 네이티브 USB 통신
  • 오픈소스 구현

적합한 대상

  • IoT 애플리케이션 개발자
  • 하드웨어 통합업체
  • 임베디드 시스템 엔지니어
  • 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

Pub Flutter

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.

import 'package:usb_serial/usb_serial.dart'
Optional

Add

	<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" />

to your AndroidManifest.xml

and place device_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 0x0403 / 0x6001: FTDI FT232R UART -->
    <usb-device vendor-id="1027" product-id="24577" />

    <!-- 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>

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 = await UsbSerial.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.

  1. Terminated Transformer, this splits incoming data based on a configurable end of message bytes "terminator".
  2. 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().

abstract class DisposableStreamTransformer<T, R> implements StreamTransformer<T, R> {
  void dispose();
}

Steps:

  • Change parent class to DisposableStreamTransformer
  • Implement dispose class, and make sure to dispose of your StreamController
Transaction Class changes

The Transaction class previously did not have access to the transformer, only the stream. The signature of Transaction constructor changed from

Transaction<T>(Stream<T>);

to

Transaction(Stream<Uint8List> stream, DisposableStreamTransformer<Uint8List, T> transformer);

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.

FAQ

Always ask permission to use USB port

https://github.com/altera2015/usbserial/issues/49 https://github.com/altera2015/usbserial/issues/38

Dependencies

This library depends on:

https://github.com/felHR85/UsbSerial

카테고리

의존성

의존성 목록 2 개

소스 저장소

이 패키지를 사용하는 프로젝트