gpt4 book ai didi

android - Android 上的 Delphi XE5 串口

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:29:29 29 4
gpt4 key购买 nike

有人知道如何使用 delphi XE5 访问 android 中的串口吗?我正在使用带有 android 4.1 的 Cubieboard

最佳答案

我已成功使用 Winsoft ComPort for Android 执行串行 USB 通信。

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

查看代码。

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.

在组件属性中,您必须包含创建串行端口通信的基本特征

例如:设备名称、数据位、波特率、奇偶校验和停止位,例如。

enter image description here

关于android - Android 上的 Delphi XE5 串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20576312/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com