用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制.doc_第1页
用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制.doc_第2页
用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制.doc_第3页
用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制.doc_第4页
用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用Delphi 7.0 和 Mscomm控件实现和arduino 通信控制大家知道一般控制arduino是用USB模拟的串口通信。通过Delphi对串口编程虽然有很多方法,比如直接用api 操作IO端口、spcomm控件等。但是经过本人摸索,最好用的还是用微软的OCX控件Mscomm。以下用一个实例来说明:arduino UNO单片机程序是一个6LED灯控制程序27脚输出,接受USB模拟的串口的命令控制。接到字符27表示单独点亮相应脚上的LED灯接到字符A或a 表示点亮所有6个LED灯。接收字符C或c表示熄灭所有LED灯。接收字符表示逐个点亮27脚上的LED灯,然后熄灭如此循环。接收字符#表示执行一个跑马灯循环。arduino UNO单片机接受到字符命令并执行后,向上位机返回接受命令并成功执行的回应。int BASE = 2;int NUM = 6;char iword;char loop_stat = ;int out_led;void low_all()/灯全灭过程 for (int i = BASE; i BASE + NUM; i +) digitalWrite(i,LOW); void setup() for (int i = BASE; i 0) /判断串口缓冲器是否有数据装入 iword = Serial.read(); /读取串口 if (iword=2) & (iword=7) low_all(); out_led=byte(iword)-48; digitalWrite(out_led,HIGH); Serial.print(OK ); Serial.println(out_led); iword= ; loop_stat= ; else if (iword=c) | (iword=C) ) low_all(); Serial.print(OK ); Serial.println(CLEAR!); iword= ; loop_stat= ; else if (iword=a) | (iword=A) for (int i = BASE; i ); else if (iword=#) loop_stat=#; Serial.println(OK # -); if (loop_stat != ) if (loop_stat=) low_all(); delay(100); for (int i = BASE; i BASE + NUM; i +) digitalWrite(i,HIGH); delay(100); if (loop_stat=#) low_all(); delay(100); for (int i = BASE; i 2) digitalWrite(i-1,LOW); digitalWrite(i,HIGH); delay(100); Delphi上位机的程序:unit UnitComm;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls, Menus;type TForm1 = class(TForm) MSComm1: TMSComm; Edit1: TEdit; Button1: TButton; Memo1: TMemo; Edit2: TEdit; Label2: TLabel; Button2: TButton; Memo2: TMemo; Label3: TLabel; Label4: TLabel; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Button1Click(Sender: TObject); procedure MSComm1Comm(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure Button2Click(Sender: TObject); private Private declarations public Public declarations end;var Form1: TForm1;implementation$R *.DFMprocedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin if mscomm1.PortOpen=true then mscomm1.PortOpen:=false; /关闭串口end;procedure TForm1.Button1Click(Sender: TObject);var i:integer; t:DWORD;begin memo1.lines.Clear; if mscomm1.PortOpen=true then mscomm1.PortOpen:=false; /关闭串口 mscomm1.CommPort:=strtoint(edit2.text); mscomm1.PortOpen:=true; /打开串口end;procedure TForm1.MSComm1Comm(Sender: TObject);/处理端口触发的事件。读、写或发生错误等事件时,此函数被自动调用。var s:string;begin if mscomm1.CommEvent=comEvReceive then /如果是接收数据事件 begin s:=MSComm1.Input; /读串口 memo1.Lines.Text:=memo1.Lines.Text+s; /显示 memo1.SelStart:=memo1.MaxLength; memo1.SelLength:=1; end;end;procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin if key=chr(13) then begin mscomm1.Output:=edit1.text+chr(13); /向传输缓冲区写数据流 While mscomm1.OutBufferCount 0 do application.ProcessMessages; /如果写操作没有完成,则等待 memo2.Lines.Add(edit1.Text); edit1.Clear; end;end;procedure TForm1.Button2Click(Sender: TObject);begin if msc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论