今天在写一个关于RS232串口连接的程序,在发送指令时老是达不到要求 请问下谁有这相关的程式给我个 谢谢
[email protected]

解决方案 »

  1.   

    给你一个SPcomm的例子,参考一下。
    unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
          Dialogs,   SPComm,   StdCtrls,cport;   
        
      type   
          TForm1   =   class(TForm)   
              Comm1:   TComm;   
              Memo1:   TMemo;   
              Button1:   TButton;   
              Button2:   TButton;   
              CheckBox1:   TCheckBox;   
              ComboBox1:   TComboBox;   
              procedure   Comm1ReceiveData(Sender:   TObject;   Buffer:   Pointer;   
                  BufferLength:   Word);   
              procedure   Button1Click(Sender:   TObject);   
              procedure   Button2Click(Sender:   TObject);   
              procedure   FormCreate(Sender:   TObject);   
              procedure   ComboBox1Change(Sender:   TObject);   
          private   
              {   Private   declarations   }   
          public   
              {   Public   declarations   }   
          end;   
        
      var   
          Form1:   TForm1;   
        //   rbuf:array[0..500]of   char;   
      implementation   
      {$R   *.dfm}   
        
      procedure   TForm1.Comm1ReceiveData(Sender:   TObject;   Buffer:   Pointer;   
          BufferLength:   Word);   
      type   
          ss=array[1..500]of   char   ;   
      var   
          str1:^ss;   
          i:integer;   
          s:array[0..15]   of   char   ;   
          n:integer;   
          str:string;   
      begin   
            str1:=Buffer;   
            str:='';   
        //s[]=('0','1','2','3'...'E')   
          if   form1.CheckBox1.Checked   then   
              begin   
                    for   i:=0   to   9   do   //转换成ASCLL码显示   
                    s[i]:=chr(48+i);   
                    for   i:=10   to   15   do   
                    s[i]:=chr(55+i);   
                    for   i:=1   to   bufferlength   do   
                    begin   
                    n:=ord(str1^[i]);   
                    str:=str+'   '+(s[n   div   16]+s[n   mod   16]);   
                    end;   
                    memo1.Lines.Add(str);//转换成ASCLL码显示   
              end   
          else   
              begin   
                  str:=str1^;   
                  delete(str,1,1);   
                  memo1.Lines.Add(str);//以十六进制原样显示   
              end;   
        
        
      end;   
        
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
          comm1.StartComm;   
          form1.Caption:='处于接收数据中.....';   
      end;   
        
      procedure   TForm1.Button2Click(Sender:   TObject);   
      begin   
          comm1.StopComm;   
          form1.Caption:='串口已关闭';   
      end;   
        
      procedure   TForm1.FormCreate(Sender:   TObject);   
      begin   
          EnumComPorts(ComboBox1.Items);   
      end;   
        
      procedure   TForm1.ComboBox1Change(Sender:   TObject);   
      begin   
          comm1.StopComm;   
          comm1.CommName:=form1.ComboBox1.Text;   
          comm1.StartComm;   
          form1.Caption:=form1.ComboBox1.Text+'串口处于接收数据中....';   
      end;   
        
      end.   
      

  2.   

    遇到什么问题?写出来大家帮你解决。我用过很多控件,Comm32,Cport,Mscomm,Spcomm,API,感觉还是SPcomm好用点
      

  3.   

    procedure TFrmMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    var
    ibyte:array of byte;
    iIndex,i:integer;
    viewstring:string;
    begin
      if not gbSampling then exit;
      try
        SetLength(iByte,BufferLength);
        iByte:=buffer;
        if (iByte[0]=Byte('#')) then
        begin
           giReceiveDataLength:=0;
        end;
        iIndex:=giReceiveDataLength;
        inc(giReceiveDataLength,bufferlength);
        setLength(giReceiveData,giReceiveDataLength);
        copyMemory(@giReceiveData[iIndex],buffer,BufferLength);    for i:=1 to bufferlength do
         viewstring:=viewstring+ inttohex(iByte[i],2)+' ' ;
          viewstring:='接收'+ viewstring;
          memo1.lines.add(viewstring);
           memo1.lines.add('');
       // if  (iByte[bufferLength-2]=13) and (iByte[bufferLength-1]=10) then
        //begin    //  DealWithReceiveData(@giReceiveData[0]);
        //end;
       // showmessage(viewstring);
      finally
         ibyte:=nil;
      end;   我是这样写的 ,就是读取不到数据,设备都能连接上