用控件spcomm写的上位机程序

本文介绍了一个使用Delphi和SPComm控件实现的上位机程序,用于与单片机进行串口通信。作者遇到的问题是,当上位机发送数据给单片机后,无法接收到单片机返回的数据。文章提供了详细的代码示例,包括串口的打开、关闭、发送数据和接收数据等操作。通过阅读本文,你可以了解到如何使用SPComm控件进行串口通信,并解决可能遇到的通信故障。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我先用上位机给单片机发数据,单片机接收到数据后在发给上位机并显示出来.问什么上位机接收不到单片机发给的数据,怎么知道上位机已经给单片机发数据了.我的单片机程序好用我已经用串口助手调试过了,谢谢  
  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写的上位机程序》的全文内容...

转载于:https://www.cnblogs.com/delphi2007/archive/2009/05/26/1490263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值