本帖最后由 u012130806 于 2014-07-24 23:07:25 编辑

解决方案 »

  1.   

    很久没用过MSComm控件了,现在比较常用的是SPComm你的代码在接受部分貌似有点问题,家里的机子没环境。给你找段以前做的代码参考下~~procedure TForm1.MSComm1Comm(Sender: TObject);
    var S:string;
            bufferlength:integer;
    begin
            if not MSComm1.CommEvent = comEvReceive then
                    exit;
            if mscomm1.InBufferCount>0 then  S:=mscomm1.Input;
            bufferlength:=length(S);
            memo1.Lines.Add(S);
    end;
      

  2.   

    又找到个发送接收都有的unit Unit1;interfaceuses
      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.
     
      

  3.   

    新手习惯用MSCOMM,我刚学也是用MSCOMM,但是强烈建议你用spcomm,真的,你不会后悔的
      

  4.   

    首先很感谢你的帮助,对于新手来说这段程序我还要消化一下,我也是接触了Delphi一个星期,我看小甲鱼的,现在赶着做上位机,有什么书好介绍,或好的资料?不胜感激~!
      

  5.   

    、我是用两头都是usb连接电脑和单片机的,但是我在网上找了一些串口调试工具,他都没有回显?难道我硬件接错了还是。。?
      

  6.   

    串口调试工具没回显,如果发送数据没错,串口参数没错,那就是硬件问题,检查下USB转口的设备串口控件可以用cport,spcomm,比MSCOMM好用
      

  7.   

    问题是我用stc烧写可以烧写进去。郁闷了,还有就是我不是不想用其他,我看过其他控件,相对而言mscomm列子比较多,有什么好资料吗?