欢迎来到天天文库
浏览记录
ID:15873541
大小:989.50 KB
页数:11页
时间:2018-08-06
《用delphi 7.0 和 mscomm控件实现和arduino 通信控制》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、用Delphi7.0和Mscomm控件实现和arduino通信控制大家知道一般控制arduino是用USB模拟的串口通信。通过Delphi对串口编程虽然有很多方法,比如直接用api操作IO端口、spcomm控件等。但是经过本人摸索,最好用的还是用微软的OCX控件Mscomm。以下用一个实例来说明:arduinoUNO单片机程序是一个6LED灯控制程序2~7脚输出,接受USB模拟的串口的命令控制。接到字符2~7表示单独点亮相应脚上的LED灯接到字符A或a表示点亮所有6个LED灯。接收字符C或c表示熄灭所有LED灯。接收字符@表示逐个点亮2~7脚上
2、的LED灯,然后熄灭如此循环。接收字符#表示执行一个跑马灯循环。arduinoUNO单片机接受到字符命令并执行后,向上位机返回接受命令并成功执行的回应。intBASE=2;intNUM=6;chariword;charloop_stat='';intout_led;voidlow_all()//灯全灭过程{for(inti=BASE;i
3、egin(9600);}voidloop(){if(Serial.available()>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='';}elseif((iword=='c')
4、
5、(iword=
6、='C')){low_all();Serial.print("OK");Serial.println("CLEAR!");iword='';loop_stat='';}elseif((iword=='a')
7、
8、(iword=='A')){for(inti=BASE;i
9、rial.println("OK@>>>");}elseif(iword=='#'){loop_stat='#';Serial.println("OK#->-");}}if(loop_stat!=''){if(loop_stat=='@'){low_all();delay(100);for(inti=BASE;i
10、{if(i>2){digitalWrite(i-1,LOW);}digitalWrite(i,HIGH);delay(100);}}}}Delphi上位机的程序:unitUnitComm;interfaceusesWindows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,OleCtrls,MSCommLib_TLB,StdCtrls,Menus;typeTForm1=class(TForm)MSComm1:TMSComm;Edit1:TEdit;Button1:TButt
11、on;Memo1:TMemo;Edit2:TEdit;Label2:TLabel;Button2:TButton;Memo2:TMemo;Label3:TLabel;Label4:TLabel;procedureFormClose(Sender:TObject;varAction:TCloseAction);procedureButton1Click(Sender:TObject);procedureMSComm1Comm(Sender:TObject);procedureEdit1KeyPress(Sender:TObject;varKey:
12、Char);procedureButton2Click(Sender:TObject);private{Privatedeclarations}pub
此文档下载收益归作者所有