ComPort for Android USB Serial

Delphi and C++ Builder serial communication library for USB connected devices for Android.
  • uses UsbSerial library
  • supports CP210x, CP2130, CDC, FTDI, PL2303 and CH34x devices
  • 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);
  TStopBits = (sb1, sb15, sb2);
  TParity = (paNone, paOdd, paEven, paMark, paSpace);
  TFlowControl = (fcOff, fcRtsCts, fcDsrDtr, fcXonXoff);

  TOnBreakInterrupt = procedure of object;
  TOnCtsChanged = procedure(State: Boolean) of object;
  TOnDsrChanged = procedure(State: Boolean) of object;
  TOnFramingError = procedure of object;
  TOnOverrunError = procedure of object;
  TOnParityError =  procedure of object;
  TOnReceivedData = procedure(Data: TJavaArray<Byte>) of object;

  EUsbSerialError = class(Exception);

  TUsbSerial = class
    constructor Create;

    procedure Connect(UsbDevice: JUsbDevice);
    procedure Disconnect;

    procedure Open(Synchronous: Boolean = False);
    procedure Close;

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

    procedure ClearDtr;
    procedure SetDtr;

    procedure ClearRts;
    procedure SetRts;

    property BaudRate: Integer read write;
    property Connected: Boolean read;
    property DataBits: TDataBits read write;
    property FlowControl: TFlowControl read write;
    property Opened: Boolean read;
    property Parity: TParity read write;
    property StopBits: TStopBits read write;
    property Synchronous: Boolean read;
    property UsbDevice: JUsbDevice read;
    property UsbDeviceConnection: JUsbDeviceConnection read;
    property UsbDevices: TArray<JUsbDevice> read;
    property UsbManager: JUsbManager read;
    property UsbSerialInterface: JUsbSerialInterface read;

    property OnBreakInterrupt: TOnBreakInterrupt read write;
    property OnCtsChanged: TOnCtsChanged read write;
    property OnDsrChanged: TOnDsrChanged read write;
    property OnFramingError: TOnFramingError read write;
    property OnOverrunError: TOnOverrunError read write;
    property OnParityError: TOnParityError read write;
    property OnReceivedData: TOnReceivedData 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" />
							

Related links