• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

曾经用Delphi写的一个上位机

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
基于SPCOMM控件。
源码:
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SPComm, ExtCtrls;
 
type
  TForm1 = class(TForm)
    Comm1: TComm;
    GroupBox1: TGroupBox;
    go: TButton;
    left: TButton;
    right: TButton;
    back: TButton;
    left30: TButton;
    right30: TButton;
    yuan90: TButton;
    yuan360: TButton;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Memo4: TMemo;
    Memo5: TMemo;
    Memo6: TMemo;
    GroupBox3: TGroupBox;
    opencom: TButton;
    lianjie: TButton;
    xinxi: TMemo;
    procedure SendHex(S: String);
    procedure left30Click(Sender: TObject);
    procedure opencomClick(Sender: TObject);
    procedure goClick(Sender: TObject);
    procedure backClick(Sender: TObject);
    procedure CommReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
    procedure leftClick(Sender: TObject);
    procedure rightClick(Sender: TObject);
    procedure w(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  BUF: string;
 
 
implementation
 
{$R *.dfm}
function StrToHex(mStr:String;stlen:word):string;
var
    I:Integer;
begin
    Result:='';
    for I := 1 to stlen do
    begin
        if mstr[i]=#0 then
            Result:=Result+'00 '
        else
         Result:=Result+IntToHex(Ord(mStr[I]),2)+' ';
    end;
end;
 
 
procedure TForm1.SendHex(S: String);
var
  s2:string;
  buf1:array[0..50000] of char;
  i:integer;
begin
  s2:='';
  for i:=1 to  length(s) do
  begin
    if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
        or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
    begin
        s2:=s2+copy(s,i,1);
    end;
  end;
  for i:=0 to (length(s2) div 2-1) do
    buf1[i]:=char(strtoint('$'+copy(s2,i*2+1,2)));
  Comm1.WriteCommData(buf1,(length(s2) div 2));
 
end;
 
procedure TForm1.left30Click(Sender: TObject);
begin
  SendHex('aa');   //发送十六进制
end;
 
 
 
procedure TForm1.opencomClick(Sender: TObject);
 
begin
  buf:='0';
   SendHex(buf);
if opencom.Caption = '打开端口' then
  begin
  Comm1.StartComm;
 
  opencom.Caption := '关闭端口';
  end
else //if Button1.Caption = '关闭串口' then
  begin
  Comm1.StopComm;
  opencom.Caption := '打开端口';
  end;
end;
 
procedure TForm1.goClick(Sender: TObject);
begin
  buf:='11';
  SendHex(buf);
end;
 
procedure TForm1.backClick(Sender: TObject);
begin
  buf:='12';
  SendHex(buf);
end;
 
procedure TForm1.leftClick(Sender: TObject);
begin
  buf:='14';
  SendHex(buf);
end;
 
procedure TForm1.rightClick(Sender: TObject);
begin
  buf:='13';
  SendHex(buf);
end;
 
procedure Tform1.CommReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
  strRecv : string;
begin
  setLength(strRecv,BufferLength);
  Move(Buffer^,pchar(strRecv)^,BufferLength);
  xinxi.Lines.Add('已收到:'+intTostr(BufferLength)+'字节的数据');
  xinxi.Lines.Add(strRecv);
  xinxi.Invalidate ;
end;
 
 
 
procedure TForm1.w(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  buf:='11';
  SendHex(buf);
end;
 
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=87 then
     buf:='11';
      SendHex(buf);
  if Key=83 then
     buf:='12';
     SendHex(buf);
  if Key=68 then
     buf:='13';
     SendHex(buf);
  if Key=65 then
    buf:='14';
     SendHex(buf);
 
 
 
end;
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if Key=87 then
      SendHex('ff');
    if Key=65 then
      SendHex('ff');
   if Key=68 then
       SendHex('ff');
   if Key=83 then
      SendHex('ff');
end;
 
end.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用Delphi程序控制Excel的操作发布时间:2022-07-18
下一篇:
delphihookalt+F4ctrl+delete+altwin键等发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap