频道分类

Delphi Winsoft ComPort for Android USB

作者:admin 来源: 日期:2019/10/13 16:16:32 人气: 标签:

 
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.

相关地址:http://www.winsoft.sk/acomport.htm  可惜是收费的