我看你写的太复杂了,还是用API吧。
很简单的。
open
wirte
close

解决方案 »

  1.   

    下面这个程序是我用在监控程序里的。
    用些地方是多余的可以不看。
    function OpenCommPort(port:string; Baud ,_tOut: integer):Thandle;
    var
      tOut : _COMMTIMEOUTS;
      DCB  : _DCB;
      hCom : Thandle;
    begin
      hCom := CreateFile(pchar(Port),GENERIC_WRITE or GENERIC_READ,0,NiL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
      if (hCom <> INVALID_HANDLE_VALUE) then
      begin
        GetCommState(hCom,DCB);
        with DCB do begin
          BaudRate := Baud;
          ByteSize :=8;                                                             // number of bits/byte, 4-8
          Parity := NOPARITY;                                                       // 0-4=no,odd,even,,space
          StopBits := ONESTOPBIT;                                                   // 0,1,2 = 1, 1.5, 2
        end;
        SetCommState(hCom,DCB);
        GetCommTimeouts(hCom,tOut);
        with tOut do begin
          ReadTotalTimeoutMultiplier := 5;
          ReadTotalTimeoutConstant := _tOut;
          WriteTotalTimeoutMultiplier := 5;
          WriteTotalTimeoutConstant := _tOut;
        end;
        SetCommTimeouts(hCom,tOut);
        result := hCom;
      end else Result := 0;
    end;
      

  2.   

    你的那个函数显然不会被执行阿。你想想看,如果你的程序只有1个进程或者线程的话。你的程序当前是在发呢?还是在收阿?,,所以我想你开2个进程,也就写2个程序,分别编译,然后接收的那个先启动,独立运行着,等待消息的到来。然后启动发送的程序。点击你的button.然后再去看看你的接收的那个程序的memo里面的内容。这样我想有可能成功。
      

  3.   

    建议你看一本书:delphi串口编程
      

  4.   

    ActiveLF(程序人生) 
    你和我想的一样呀。
    好。
    去试试。
      

  5.   

    to ActiveLF(程序人生):
    我按照呢说的方法做了,可是还是收不到数据。
    能不能给点另外的帮助呢?
    谢谢!!!!!! 
      

  6.   

    现在我将程序改为下面:
    初始化过程:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Form1.Caption:='串口通讯';
      MSComm1.InBufferCount:=0;
      MSComm1.InputLen:=0;
      MSComm1.RThreshold:=1;
      MSComm1.CommPort:=1;
      MSComm1.DTREnable:=true;
      MSComm1.RTSEnable:=true;
      MSComm1.PortOpen:=true;
      MSComm1.InputMode:=1;
      MSComm1.Settings:='9600,n,8,1';
      Mscomm2.Settings:='9600,n,8,1';
      MSComm2.InputMode:=1;
      MSComm2.InBufferCount:=0;
      MSComm2.InputLen:=0;
      MSComm2.RThreshold:=1;
      MSComm2.CommPort:=2;
      MSComm2.DTREnable:=true;
      MSComm2.RTSEnable:=true;
      MSComm2.PortOpen:=true;
      Memo2.text:='';
      Memo1.text:='';
    end;
    发送过程:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
      s,datastr:string;
    begin
      s:=memo1.text;
      datastr:='';
      if MSComm1.PortOpen=false then
      showmessage('error!')
      else
      begin
      showmessage('ok!');
      for i:=0 to (length(s) div 2)-1 do
      begin
        datastr:=datastr+chr(strtoint('$'+ copy(s,2*i+1,2))); 
      end;
      begin
        MSComm1.output:=datastr;
      end;
    end;
    end;
    怎样证明发送成功?
    接收过程:
    procedure TForm1.Button3Click(Sender: TObject);
      var
        recstr:string;
        strtemp:string;
        i:integer;
      begin
        if Mscomm2.PortOpen=false then
          showmessage('error')
        else
          showmessage('ok');
        if Mscomm2.CommEvent=comEvReceive then
          begin
            sleep(350);
            recstr:=Mscomm1.input;
            strtemp:='';
            for i:=0 to length(recstr) do
              strtemp:=strtemp+inttohex(ord(recstr[i]),2);
              memo2.text:=memo2.text+strtemp;
          end;
      end;
    memo2框中看不到数据,单步执行时发现程序在接收过程中运行到:
    if Mscomm2.CommEvent=comEvReceive then就步往下执行了,为什么呢?
    希望大家能给我找到错误,再一次感谢大家的光临。
      

  7.   

    if Mscomm2.CommEvent=comEvReceive then
          begin
            sleep(350);
            recstr:=Mscomm1.input;
            strtemp:='';
            for i:=0 to length(recstr) do
              strtemp:=strtemp+inttohex(ord(recstr[i]),2);
              memo2.text:=memo2.text+strtemp;
          end;
      end;
    把这段代码放到onComm事件中去看看。
      

  8.   

    to  deathcat(从死亡边缘站起来的患了狂猫病的猫):
    现在可以肯定的是:串口线的连接没有问题.
    下面这段代码放在onComm事件中根本就不会执行。
    if Mscomm2.CommEvent=comEvReceive then
          begin
            sleep(350);
            recstr:=Mscomm1.input;
            strtemp:='';
            for i:=0 to length(recstr) do
              strtemp:=strtemp+inttohex(ord(recstr[i]),2);
              memo2.text:=memo2.text+strtemp;
          end;
      end;
    我将其放在一个按钮事件中,点击按钮表示接收。
      

  9.   

    花2个小时学习一下API,再花30分钟查一下API函数。
      

  10.   

    我现在不是不想用API嘛,现在用控件。
      

  11.   

    Mscomm漏洞较多,可以在“DELPHI深度探索”中下载Spcomm控件,不错
      

  12.   

    我用Spcomm控件做了一个利用本机两个串口进行通讯的程序,你帮我看看对不对?
    谢谢你啦。。
    如果你愿意我贴代码上来。。