Delphi MSComm 发送接收


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, OleCtrls, MSCommLib_TLB, ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    btnStart: TButton;
    btnStop: TButton;
    btnSend: TButton;
    BitBtn1: TBitBtn;
    Timer1: TTimer;
    Memo2: TMemo;
    Memo3: TMemo;
    Memo4: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    MSComm1: TMSComm;
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure btnSendClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure sendcomm(port:byte);
    procedure Timer1Timer(Sender: TObject);
    procedure MSComm1Comm(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  gate:byte;

implementation

{$R *.DFM}


procedure tform1.sendcomm(port:byte);
var
  s:string;
  i:integer;
begin
  SetLength(s,18);
  s[1]:=chr($7E);
  s[2]:='1';
  s[14]:='0';
  s[15]:='2';
  s[16]:='4';
  case port of
  1:begin
      s[3]:='1';             //校验和0242h~0245h
      s[17]:='2';
      memo1.Lines.Add('发送时间:'+timetostr(now));
    end;
  2:begin
      s[3]:='2';
      s[17]:='3';
      memo2.Lines.Add('发送时间:'+timetostr(now));
    end;
  3:begin
      s[3]:='3';
      s[17]:='4';
      memo3.Lines.Add('发送时间:'+timetostr(now));
    end;
  4:begin
      s[3]:='4';
      s[17]:='5';
      memo4.Lines.Add('发送时间:'+timetostr(now));
    end;
  end;
  for i:=4 to 13 do
  begin
    s[i]:='0';
  end;
  s[18]:=chr($0d);
  if not mscomm1.PortOpen then mscomm1.PortOpen:=true;
  for i:=1 to 18 do
  begin
    mscomm1.Output:=s[i];
    sleep(3);
  end;
end;

procedure TForm1.btnStartClick(Sender: TObject);
begin
  if not mscomm1.PortOpen then mscomm1.PortOpen:=true;
  timer1.Enabled:=true;
  btnstart.Enabled:=false;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  timer1.Enabled:=false;
  btnstart.Enabled:=true;
  if  mscomm1.PortOpen then mscomm1.PortOpen:=false;
end;

procedure TForm1.btnSendClick(Sender: TObject);
begin
  memo1.Clear;
  memo2.Clear;
  memo3.Clear;
  memo4.Clear;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if mscomm1.PortOpen then mscomm1.PortOpen:=false;
  Close;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  sendcomm(gate);
  gate:=gate+1;
  if gate>4 then gate:=1;
end;

procedure TForm1.MSComm1Comm(Sender: TObject);
var
 S:string;
  Ss:array of byte;
  bufferlength:integer;
  i:integer;
  y:single;
begin
  if not MSComm1.CommEvent = comEvReceive then
    Exit;
  S:=mscomm1.input;
  bufferlength:=length(S);
  if not bufferlength=18 then exit;
  for i:=1 to BufferLength do
  begin
     Ss[i-1]:=ord(S[i]);
  end;
  if (Ss[0]<>$7E) or (Ss[17]<>$0d)then exit;
  if (Ss[1]<>$31) then exit;
  if (Ss[9]>$39)or(Ss[9]<$30)then S[10]:='0';
  if (Ss[10]>$39)or(Ss[10]<$30)then S[11]:='0';
  if (Ss[11]>$39)or(Ss[11]<$30)then S[12]:='0';
  if (Ss[12]>$39)or(Ss[12]<$30)then S[13]:='0';
  y:=0.001*strtoint(S[13])+0.01*strtoint(S[12])+0.1*strtoint(S[11])+strtoint(S[10]);
//  y:= 100.88;
  case Ss[2] of
  $31:begin
        Memo1.Lines.Add(formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now));
        edit1.Text:=formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now);
      end;
  $32:begin
        Memo2.Lines.Add(formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now));
        edit2.Text:=formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now);
      end;
  $33:begin
        Memo3.Lines.Add(formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now));
        edit3.Text:=formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now);
      end;
  $34:begin
        Memo4.Lines.Add(formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now));
        edit4.Text:=formatfloat('0.000',y)+'毫米   时间为: '+timetostr(now);
      end;
  end;
end;

end.

串口控件可以用cport,spcomm,比MSCOMM好用
 

相关阅读 >>

Delphi程序在每个windows 会话中只执行一次

Delphi 计算加班时间工时的函数

Delphi webbrowser1 缩放网页

Delphi的枚举类型

Delphi fmx 把内容复制到粘贴板上支持跨平台

Delphi 分解时间 �cdecodedate、decodetime … decodedatetime …

Delphi dbgrid鼠标滚屏

Delphi 深入了解unigui hyperserver

Delphi格式化函数format、formatdatetime和formatfloat

Delphi string转unicode

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...