unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, SPComm;type
  TForm1 = class(TForm)
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label2: TLabel;
    ComboBox2: TComboBox;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    Comm1: TComm;
    send_button: TButton;
    Memo1: TMemo;
    procedure RadioGroup1Click(Sender: TObject);
    procedure RadioGroup2Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ComboBox2Change(Sender: TObject);
    procedure send_buttonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
   Form1: TForm1;
   str_p:string; 
   str_s:string;    
   str_i:string;  
  implementation{$R *.dfm}
var
  str_com:string;//保存用户选择的串口号  Viewstring:string;
  sbuf:array[1..6] of string;
  procedure TForm1.FormShow(Sender: TObject);
begin
     Comm1.BaudRate:=9600;
     Comm1.ByteSize:=_8;
     Comm1.StopBits:=_1;
    
     ComboBox2.Text:='00';
     str_i:='00';
end;procedure TForm1.ComboBox1Change(Sender: TObject);
begin
    case ComboBox1.ItemIndex of
          0: begin comm1.StopComm; Str_com:='com1';  Comm1.CommName:=Str_com;
             comm1.StartComm;  end;
          
          1: begin comm1.StopComm; Str_com:='com2';   Comm1.CommName:=Str_com;
           end;
          
          2: begin comm1.StopComm; Str_com:='com3';  Comm1.CommName:=Str_com;
             comm1.StartComm;   end;
          
          3: begin comm1.StopComm; Str_com:='com4';   Comm1.CommName:=Str_com;
            comm1.StartComm;  end;
          
          4: begin comm1.StopComm; Str_com:='com5';   Comm1.CommName:=Str_com;
            comm1.StartComm;  end;
     end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);  //close com and form
begin
    Comm1.StopComm;
     
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);   
begin
    case RadioGroup1.ItemIndex  of
    0:   str_p:='PV';                        
    end;
end;procedure TForm1.RadioGroup2Click(Sender: TObject);  
begin
    case RadioGroup2.ItemIndex of
    0:str_s:='SW';                    
    end;
end;procedure TForm1.ComboBox2Change(Sender: TObject);   
begin
     case ComboBox2.ItemIndex of
     0:str_i:='00';
     end;
end;
procedure senddata; //自定义的发送过程
var
i:integer;
commflg:boolean;
begin
  showmessage('inter send fuction!');
  viewstring:='';
  commflg:=true;   //判断串口是否正在占用
  for i:=1 to 6 do
  begin
 if not Form1.Comm1.WriteCommData(@sbuf[i],1) then
   begin
        commflg:=false;
        showmessage('writecommdate falure!');
        break;
   end ;
       viewstring:=viewstring+sbuf[i];
  end;  //end for()
  viewstring:='发送'+viewstring;  Form1.memo1.lines.add(viewstring);
  Form1.memo1.lines.add('');
  if not commflg then
  messagedlg('发送失败!',mterror,[mbyes],0);
end;
procedure TForm1.send_buttonClick(Sender: TObject);//发送按钮的点击事件
begin
 sbuf[1]:=str_p;        
 sbuf[2]:=str_i;      
 sbuf[3]:=str_s;      
 sbuf[4]:='01';
 sbuf[5]:='00';
 sbuf[6]:='NT';        senddata;                   //调用自定义的发送函数
 showmessage('send ok !');
end;
 
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);  //接受串口数据
var
strRecv:string;
begin
showmessage('start recive');
setlength(strRecv,bufferlength);
move(buffer^,pchar(strrecv)^,bufferlength);
memo1.Lines.Add('已收到:'+inttostr(bufferlength)+'字节的数据');
memo1.Lines.Add(strRecv);end;end.运行时会提示 “send ok”,发送PV00SW0100NT。但是接受不显示,好像接受函数没执行(触发),按理说当数据发送出去后,就会激发它的,难道是我的数据没有发送到串口的缓冲区?那我的数据发到哪里去了?writedata()函数是那样用的,没错啊!不懂了,请大家帮忙指点指点!谢谢~!

解决方案 »

  1.   

    你的程序不对吧.
              1: begin comm1.StopComm; Str_com:='com2';  Comm1.CommName:=Str_com; 
              end; 
      

  2.   

    保证ComboBox1Change、RadioGroup1Click、RadioGroup2Click、ComboBox2Change都执行过至少一次了吗?
      

  3.   

    以前写的代码,参考一下:procedure TForm1.Button1Click(Sender: TObject);
    begin
      self.Comm1.CommName:=edit1.Text;
      self.Comm1.BaudRate:=strtoint(edit2.Text);
      self.Comm1.ByteSize:=Tbytesize(3);
      self.Comm1.StopBits:=Tstopbits(0);
      self.Comm1.Parity:=tparity(0);
      try
        self.Comm1.StartComm;
      except
        raise exception.Create('打开串口失败!');
      end;    self.StatusBar1.Panels[0].Text:=edit1.text+'已打开';
      self.StatusBar1.Refresh;
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      self.Comm1.StopComm;
      self.StatusBar1.Panels[0].Text:=edit1.text+'已关闭';
      self.StatusBar1.Refresh;
    end;procedure TForm1.Button2Click(Sender: TObject);
    var
      str:Pchar;
      count:integer;
    begin
      str:=pchar(edit3.Text);
      count:=length(str);
      if self.Comm1.WriteCommData(str,count) then
        self.Memo1.Lines.Add('已发送'+inttostr(count)+'个字符')
      else
        raise  exception.Create('发送错误');
    end;procedure TForm1.Button4Click(Sender: TObject);
    begin
       close;
    end;procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    var
      str:string;
      strrecv:string;
    begin
      setlength(strrecv,bufferlength);
      move(buffer^,Pchar(strrecv)^,bufferLength);
      self.Memo2.Lines.Add('已收到:'+inttostr(bufferlength)+'字节的数据');
      self.Memo2.Lines.Add(strrecv);   
    end;