Unit1.pas
上传用户:anlu_slm
上传日期:2022-07-30
资源大小:283k
文件大小:6k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit Unit1;
  2. //**********************************************************
  3. //深圳市东方数码技术有限公司
  4. //曾伟才
  5. //2004年11月9日
  6. //**********************************************************
  7. interface
  8. uses
  9.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  10.   Dialogs, OleCtrls, EdSockServerApi, StdCtrls, Types, SysConst, VarUtils,
  11.   ExtCtrls;
  12. type
  13.     TForm1 = class(TForm)
  14.     edtPeerIP: TEdit;
  15.     edtPeerPort: TEdit;
  16.     btnConnect: TButton;
  17.     lblStatus: TLabel;
  18.     btnCloseConnect: TButton;
  19.     Lable3: TLabel;
  20.     lblMAC: TLabel;
  21.     GroupBox1: TGroupBox;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     GroupBox2: TGroupBox;
  25.     Label3: TLabel;
  26.     edtLocalPort: TEdit;
  27.     btnListen: TButton;
  28.     Label4: TLabel;
  29.     memSend: TMemo;
  30.     Label5: TLabel;
  31.     memRecv: TMemo;
  32.     btnSend: TButton;
  33.     btnClear: TButton;
  34.     Label6: TLabel;
  35.     lblConnectID: TLabel;
  36.     btnTest: TButton;
  37.     Timer1: TTimer;
  38.     procedure btnConnectClick(Sender: TObject);
  39.     procedure btnCloseConnectClick(Sender: TObject);
  40.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  41.     procedure btnListenClick(Sender: TObject);
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure btnSendClick(Sender: TObject);
  44.     procedure btnClearClick(Sender: TObject);
  45.     procedure btnTestClick(Sender: TObject);
  46.     procedure Timer1Timer(Sender: TObject);
  47.   private
  48.     { Private declarations }
  49.   public
  50.     { Public declarations }
  51.   end;
  52. var
  53.   Form1: TForm1;
  54. implementation
  55. {$R *.dfm}
  56. var g_ConnectID : Integer =0;
  57. var times :Integer =0;
  58. procedure TForm1.btnConnectClick(Sender: TObject);
  59. var
  60.   len : Integer;
  61.   var ip: string;
  62.   port: Integer;
  63. begin
  64.     StopListen();
  65.     len := edtPeerIP.GetTextLen();
  66.     if len=0 then Exit;
  67.     if edtPeerPort.GetTextLen()=0 then Exit;
  68.     ip := edtPeerIP.Text;
  69.     if not TryStrToInt(edtPeerPort.Text ,port) then Exit;
  70.     OpenConnect(PAnsiChar(ip), port, nil , 0);
  71. end;
  72. procedure EdSockAccept(ConnectID: Longint); stdcall
  73. var
  74.   mac: array[0..6] of Byte;
  75.   strmac: array[0..30] of Byte;
  76.   len : Integer;
  77. begin
  78.     if (g_ConnectID<>0) then CloseConnect(g_ConnectID);
  79.     g_ConnectID :=ConnectID;
  80.     Form1.lblStatus.Caption :='已连接';
  81.     GetMAC(g_ConnectID, PByte(@mac[0]), 6);
  82.     len := mac_ntoa(PByte(@mac[0]),6,PByte(@strmac[0]),30);
  83.     strmac[len] := 0;
  84.     Form1.lblMAC.Caption := PChar(PByte(@strmac[0]));
  85.     Form1.lblConnectID.Caption := IntToStr(g_ConnectID);
  86. end;
  87. procedure EdSockConnectClose(ConnectID: Longint); stdcall;
  88. begin
  89.     try
  90.     g_ConnectID := 0 ;
  91.     Form1.lblStatus.Caption :='未连接';
  92.     Form1.lblMAC.Caption :='';
  93.     Form1.lblConnectID.Caption := '';
  94.     finally
  95.     end;
  96. end;
  97. procedure EdSockReceFromCOM(ConnectID: Longint;
  98.   COMNum: Longint; pDataBuf: PByte; DataLength: Longint); stdcall
  99. var
  100.     recvdata :array of byte ;
  101.     str : String;
  102.     i : Integer;
  103. begin
  104.     if (ConnectID<>g_ConnectID) then Exit;
  105.     setlength(recvdata,DataLength+1);
  106.     for i := 0 to DataLength-1 do
  107.     begin
  108.       recvdata[i] := pDataBuf^;
  109.       pDataBuf := PByte(PChar(pDataBuf) + 1);
  110.     end;
  111.     str := String(recvdata);
  112.     if length(Form1.memRecv.Lines.Text)>1000 then
  113.         Form1.memRecv.Lines.Text :=''
  114.     else
  115.         Form1.memRecv.Lines.Text := Form1.memRecv.Lines.Text+str;
  116. end;
  117. //procedure OnError(SocketID: Longint; ErrorCode: Longint); stdcall;
  118. //procedure OnReceConfigData(ConnectID: Longint; pDataBuf: PByte; DataLength: Longint); stdcall;
  119. //procedure OnSwitchChange(ConnectID: Longint; PortNum: Longint; Value: Longint); stdcall;
  120. procedure TForm1.btnCloseConnectClick(Sender: TObject);
  121. begin
  122.     if (g_ConnectID <> 0) then
  123.     begin
  124.         CloseConnect(g_ConnectID);
  125.         g_ConnectID :=0;
  126.         lblStatus.Caption :='未连接';
  127.         lblMAC.Caption :='';
  128.         lblConnectID.Caption := '';
  129.     end;
  130. end;
  131. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  132. begin
  133.     if (g_ConnectID<>0) then CloseConnect(g_ConnectID);
  134. end;
  135. procedure TForm1.btnListenClick(Sender: TObject);
  136. var
  137.   LocalPort: Integer;
  138.   LocalIP: PAnsiChar;
  139. begin
  140.     if (g_ConnectID<>0) then CloseConnect(g_ConnectID);
  141.     LocalIP := '0.0.0.0';
  142.     LocalPort := StrToInt(edtLocalPort.Text);
  143.     StartListen(LocalIP, LocalPort);
  144. end;
  145. procedure TForm1.FormCreate(Sender: TObject);
  146. var
  147.   OnAccept : TOnAccept;
  148.   OnConnectClose : TOnConnectClose;
  149.   OnReceFromCOM : TOnReceFromCOM;
  150. begin
  151.     memSend.Text := '';
  152.     memRecv.Text := '';
  153.     OnAccept := TOnAccept(@EdSockAccept);
  154.     OnConnectClose := TOnConnectClose(@EdSockConnectClose);
  155.     OnReceFromCOM := TOnReceFromCOM(@EdSockReceFromCOM);
  156.     SetCallback(OnAccept,OnConnectClose,nil,nil,OnReceFromCOM,nil);
  157. end;
  158. procedure TForm1.btnSendClick(Sender: TObject);
  159. var
  160.     DataSend : array of byte ;
  161.     len : Longint;
  162.     com : Integer;
  163.     p : PChar;
  164. begin
  165.     if g_ConnectID = 0 then Exit;
  166.     len := StrLen(memSend.Lines.GetText());
  167.     setlength(DataSend,len);
  168.     p := memSend.Lines.GetText();
  169.     move(p[0],DataSend[0],len);
  170.     com := GetCOM(g_ConnectID);
  171.     SendToCOM(g_ConnectID,com,@DataSend[0],len);
  172. end;
  173. procedure TForm1.btnClearClick(Sender: TObject);
  174. begin
  175.     memRecv.Text := '';
  176. end;
  177. procedure TForm1.btnTestClick(Sender: TObject);
  178. begin
  179.   timer1.Enabled :=true;
  180. end;
  181. procedure TForm1.Timer1Timer(Sender: TObject);
  182. var
  183.     strmsg :string;
  184. begin
  185.     times := times+1;
  186.     if (times > 5000) then
  187.     begin
  188.         timer1.Enabled :=false;
  189.         times :=0;
  190.         strmsg := '完成';
  191.         MessageDlg(strmsg,mtInformation,[mbOK],0);
  192.         EXIT;
  193.     end;
  194.     if (g_ConnectID = 0) then
  195.     begin
  196.         timer1.Enabled :=false;
  197.         strmsg :=IntToStr(times);
  198.         MessageDlg(strmsg,mtInformation,[mbOK],0);
  199.         times :=0;
  200.         exit;
  201.     end;
  202.     btnSendClick(btnTest);
  203. end;
  204. end.