ComPort for Android USB

Delphi and C++ Builder serial communication library for USB connected devices for Android.
  • uses usb-serial-for-android library
  • supports FT232R, CDC/ACM (Arduino Uno), CP2102 and CH34x chips
  • no special root access privileges needed
  • available for Delphi/C++ Builder XE6 - 10.2
  • source code included in registered version
  • royalty free distribution in applications

API

type
  TDataBits = (db5, db6, db7, db8);
  TParity = (paNone, paOdd, paEven, paMark, paSpace);
  TStopBits = (sb1, sb15, sb2);

  TOnDeviceAttached = procedure(Device: JUsbDevice) of object;
  TOnDeviceDetached = procedure(Device: JUsbDevice) of object;

  EUsbSerialError = class(Exception);

  TUsbSerial = class
    constructor Create;

    function GetDevice(Driver: JUsbSerialDriver): JUsbDevice;
    function GetDevice(Port: JUsbSerialPort): JUsbDevice;

    function HasPermission(UsbDevice: JUsbDevice): Boolean;
    procedure RequestPermission(UsbDevice: JUsbDevice);

    procedure Connect(Port: JUsbSerialPort);
    procedure Disconnect;

    procedure Open;
    procedure Close;

    function Read(Buffer: TJavaArray<Byte>; Timeout: Integer = 0): Integer;
    function Read(Buffer: TArray<Byte>; Timeout: Integer = 0): Integer;
    function Read(var Data: Byte; Timeout: Integer = 0): Integer;

    function Write(Buffer: TJavaArray<Byte>; Timeout: Integer = 0): Integer;
    function Write(Buffer: TArray<Byte>; Timeout: Integer = 0): Integer;
    function Write(Data: Byte; Timeout: Integer = 0): Integer;

    function Purge: Boolean;
    function PurgeInput: Boolean;
    function PurgeOutput: Boolean;

    property BaudRate: Integer read write;
    property CD: Boolean read;
    property Connected: Boolean read;
    property CTS: Boolean read;
    property DataBits: TDataBits read write;
    property DSR: Boolean read;
    property DTR: Boolean read write;
    property Opened: Boolean read;
    property Parity: TParity read write;
    property RI: Boolean read;
    property RTS: Boolean read write;
    property StopBits: TStopBits read write;
    property UsbDevice: JUsbDevice read;
    property UsbDeviceConnection: JUsbDeviceConnection read;
    property UsbSerialDrivers: TArray<JUsbSerialDriver> read;
    property UsbSerialPorts[const Driver: JUsbSerialDriver]: TArray<JUsbSerialPort> read;
    property UsbSerialPort: JUsbSerialPort read;
    property UsbManager: JUsbManager read;

    property OnDeviceAttached: TOnDeviceAttached read write;
    property OnDeviceDetached: TOnDeviceDetached read write;
  end;

function ToByteArray(JavaArray: TJavaArray<Byte>): TArray<Byte>;
						

Download and order

FAQ

What files should be deployed to Android device?
Add these files in Delphi Deployment window:
Local NameRemote Path  Note
usbserial.dexassets\internal\  Delphi 10 and higher: add classes.jar to Libraries list in Project Manager instead
device_filter.xmlres\xml\

Add these lines to AndroidManifest.template.xml file:
<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" />
							
How can I determine chip used in USB/RS232 adapter?
Use HWiNFO on PC and USB Library for Android demo example on Android.

Related links