Dobot Magician Wi-Fi (UDP) 2020/03/12
・2020/03/16 IdUDPServer1.SendBuffer の引数を変数に変更、isQueued を Falseに変更
■Dobot Magician Wi-Fi 接続の Delphi サンプルです。
Indy UDP Server コンポーネントを使っています。
UART 通信のサンプルでもあります。
※Wi-Fi アダプターは、前もって DobotStudio で 無線LANに登録しておく必要があります。
ポート番号は、8899 です。
unit DobotUDPTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdGlobal, IdUDPBase,
IdUDPServer, IdSocketHandle;
type
TForm6 = class(TForm)
Button2: TButton;
IdUDPServer1: TIdUDPServer;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form6: TForm6;
implementation
{$R *.dfm}
// 吸引カップ ON / OFF
procedure TForm6.Button2Click(Sender: TObject);
var
B : TIdBytes;
i : integer;
checkSum : Byte;
host : string;
port : word;
begin
SetLength(B, 8);
// 送信データ
B[0] := $AA;
B[1] := $AA;
B[2] := 2 + 2; // len = 2 + Payload Len;
B[3] := 62; // Command ID;
B[4] := 1; // ctrl r/w($01) = W, isQueued($02) = True;
B[5] := 1; // isCtrlEnabled 吸引カップ 無効(0) / 有効(1)
B[6] := 0; // isSucked 吸引カップ OFF (0) / ON (1)
// チェックサム
checkSum := 0;
for i := 3 to 6 do checkSum := checkSum + B[i];
checkSum := $100 - checkSum;
B[7] := checkSum;
// 送信
host := '192.168.0.25';
port := 8899;
IdUDPServer1.SendBuffer(host, port, Id_IPv4, B);
end;
procedure TForm6.FormCreate(Sender: TObject);
// 起動と同時にポートをオープンし、受信を開始する
begin
IdUDPServer1.DefaultPort := 8899;
IdUDPServer1.Active := True;
end;
procedure TForm6.IdUDPServer1UDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
// Dobot からの返信データ
begin
// isQueued($02) = true の時は、14文字.false では 8 文字
ShowMessage(Length(Adata).ToString);
if (Length(Adata) >= 8) and (AData[3] = 62) then
ShowMessage('SetEndEffectorSuctionCup OK');
end;
end.