用控制項spcomm寫的上位機程式

來源:互聯網
上載者:User

我先用上位機給單片機發資料,單片機接收到資料後在發給上位機並顯示出來.問什麼上位機接收不到單片機發給的資料,怎麼知道上位機已經給單片機發資料了.我的單片機程式好用我已經用串口助手調試過了,謝謝  
  unit   Unit1;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,  
      Dialogs,   StdCtrls,   SPComm;  
   
  type  
      TForm1   =   class(TForm)  
          Memo1:   TMemo;  
          Button1:   TButton;  
          Button2:   TButton;  
          Button3:   TButton;  
          Button4:   TButton;  
          Button5:   TButton;  
          ComboBox1:   TComboBox;  
          Label1:   TLabel;  
          Button6:   TButton;  
          Comm1:   TComm;  
          Edit1:   TEdit;  
          procedure   Button3Click(Sender:   TObject);  
          procedure   Button2Click(Sender:   TObject);  
          procedure   Button1Click(Sender:   TObject);  
          procedure   Button4Click(Sender:   TObject);  
          procedure   Comm1ReceiveData(Sender:   TObject;Buffer:pointer;BufferLength:Word);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
  var  
      Form1:   TForm1;  
   
   
  implementation  
   
  {$R   *.dfm}  
   
  procedure   TForm1.Comm1ReceiveData(Sender:   TObject;Buffer:pointer;BufferLength:Word);  
  var  
  data:string;  
  begin                   //接收資料  
  SetLength(data,BufferLength);  
  move(Buffer^,pchar(data)^,Bufferlength);  
  Memo1.lines.Add(data);  
  Memo1.Invalidate;  
   
  end;  
  procedure   TForm1.Button3Click(Sender:   TObject);  
  begin  
  Form1.Close;  
  end;  
  procedure   TForm1.Button2Click(Sender:   TObject);  
  begin  
  comm1.StopComm   ;  
  button1.Enabled:=true   ;  
  end;  
   
  procedure   TForm1.Button1Click(Sender:   TObject);  
  begin  
  try  
  //comm1.CommName:='com'+inttostr(combobox1.ItemIndex);     //選擇連接埠號碼  
  comm1.CommName=com1;  
  comm1.BaudRate:=9600;  
  comm1.StartComm;  
  button1.Enabled:=false;  
  button2.Enabled:=true;  
  button3.Enabled:=true;  
  button4.Enabled:=true;  
  button5.Enabled:=true;  
        except  
              ShowMessage('開啟串口錯誤!');  
  end;  
  end;  
   
  procedure   TForm1.Button4Click(Sender:   TObject);  
  var  
  str:string;  
  begin                           //發送資料  
  str:=   Edit1.Text;  
  form1.comm1.writecommdata(pchar(str),length(str));  
  end;  
  end.

繼續閱讀《用控制項spcomm寫的上位機程式》的全文內容...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.