13

Anyone have any idea how to access serial port in android with delphi XE5? I'm using a Cubieboard with android 4.1

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
EversonNovka
  • 181
  • 2
  • 6
  • 2
    There is a 3rd party component for this: [`Comport for Android`](http://www.winsoft.sk/acomport.htm). See also [`Broadcast Receivers in Delphi XE5 Android`](http://stackoverflow.com/q/18891486/576719) for information about Bluetooth serial communication. As I understand, it would require some work to get this implemented, since the needed api's are not fully translated to Delphi yet. – LU RD Dec 14 '13 at 08:31
  • Please clarify what is serial port? It can be USB, COM or BT ... – Sebastian Xawery Wiśniowiecki Apr 04 '14 at 12:44
  • Everson, commonly Android uses serial by USB, using those adapters. In your case you are using a dev board, where you need to see if there is serial onboard, and how it is accessed. I am not aware of api for direct serial. I have made software to access serial by usb only. Make a test with this app and take a look on the links: https://play.google.com/store/apps/details?id=android_serialport_api.sample&hl=pt_BR – Eduardo Elias Aug 10 '14 at 03:56

1 Answers1

0

I have successfully used Winsoft ComPort for Android to perform a serial USB communication.

http://www.winsoft.sk/acomport.htm

Check out the code.

unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Layouts, FMX.Memo, Winsoft.Android.ComPort;

type
  TFormMain = class(TForm)
    Memo: TMemo;
    StatusBar: TStatusBar;
    ToolBar: TToolBar;
    ButtonOpenClose: TButton;
    ButtonSettings: TButton;
    LabelStatus: TLabel;
    AComPort: TAComPort;
    Timer: TTimer;
    procedure ButtonOpenCloseClick(Sender: TObject);
    procedure AComPortAfterWrite(Sender: TObject; Buffer: Pointer;
      Length: Integer);
    procedure TimerTimer(Sender: TObject);
    procedure AComPortAfterRead(Sender: TObject; Buffer: Pointer;
      Length: Integer);
    procedure MemoKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    procedure AComPortError(ComPort: TAComPort; E: EComError;
      var Action: TComAction);
    procedure ButtonSettingsClick(Sender: TObject);
  private
    { Private declarations }
    FReadCount: Integer;
    FWriteCount: Integer;
    procedure UpdateStatus;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

uses Settings;

{$R *.fmx}

procedure TFormMain.UpdateStatus;
begin
  LabelStatus.Text := 'Read bytes: ' + IntToStr(FReadCount) + '     Write bytes: ' + IntToStr(FWriteCount);
end;

procedure TFormMain.AComPortAfterRead(Sender: TObject; Buffer: Pointer; Length: Integer);
begin
  FReadCount := FReadCount + Length;
  UpdateStatus;
end;

procedure TFormMain.AComPortAfterWrite(Sender: TObject; Buffer: Pointer; Length: Integer);
begin
  FWriteCount := FWriteCount + Length;
  UpdateStatus;
end;

procedure TFormMain.AComPortError(ComPort: TAComPort; E: EComError; var Action: TComAction);
begin
  ShowMessage('Error ' + IntToStr(E.ErrorCode) + ': ' + E.Message);
  Action := caAbort;
end;

procedure TFormMain.ButtonOpenCloseClick(Sender: TObject);
begin
  AComPort.Active := not AComPort.Active;
  Timer.Enabled := AComPort.Active;
  Memo.Enabled := AComPort.Active;
  ButtonSettings.Enabled := not AComPort.Active;
  if AComPort.Active then
    ButtonOpenClose.Text := 'Close'
  else
    ButtonOpenClose.Text := 'Open';
end;

procedure TFormMain.ButtonSettingsClick(Sender: TObject);
begin
  FormSettings.Show;
end;

procedure TFormMain.MemoKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  AComPort.WriteByte(Ord(KeyChar));
end;

procedure TFormMain.TimerTimer(Sender: TObject);
var Text: string;
begin
  Text := AComPort.ReadUtf8;
  if Text <> '' then
    Memo.Text := Memo.Text + Text;
end;

end.

At the component Properties, you must include essential characteristics that creates your Serial port communication.

Such as : device name, databits, baudrate, parity and stop bits, for example.

enter image description here

Machado
  • 14,105
  • 13
  • 56
  • 97