欢迎来到天天文库
浏览记录
ID:1191522
大小:36.57 KB
页数:5页
时间:2017-11-08
《基于delphi通过spcomm控件实现串口数据读写》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、一、界面二、spcomm属性设置三、全部代码unitcommmtest;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,SPComm,StdCtrls;typeTFCOMM=class(TForm)COMM1:TComm;btn1:TButton;memo1:TMemo;btn2:TButton;edt1:TEdit;procedureFormClose(Sender:TObject;varAct
2、ion:TCloseAction);procedurebtn1Click(Sender:TObject);proceduresenddata;procedureCOMM1ReceiveData(Sender:TObject;Buffer:Pointer;BufferLength:Word);procedurebtn2Click(Sender:TObject);private{Privatedeclarations}public{Publicdeclarations}end;varFCOMM:TFCOMM;Viewstrin
3、g:string;i:integer;sbuf:string;rbuf:array[1..20]ofByte;implementation{$R*.dfm}procedureTFCOMM.btn1Click(Sender:TObject);beginsbuf:=edt1.Text+#13#10;senddata();end;procedureTFCOMM.btn2Click(Sender:TObject);begincomm1.StartComm;end;procedureTFCOMM.COMM1ReceiveData(S
4、ender:TObject;Buffer:Pointer;BufferLength:Word);vari:integer;beginviewstring:='';move(buffer^,pchar(@rbuf)^,bufferlength);fori:=1tobufferlengthdoviewstring:=viewstring+inttohex(rbuf[i],2)+'';//inttohex把一个整型数字转换成16进制形式的字符串viewstring:=viewstring;memo1.lines.add(view
5、string);memo1.lines.add('');end;procedureTFCOMM.FormClose(Sender:TObject;varAction:TCloseAction);begincomm1.StopComm;end;procedureTFCOMM.senddata;vari:integer;commflg:boolean;beginviewstring:='';commflg:=true;fori:=1toLength(sbuf)dobeginifnotfcomm.comm1.writecommd
6、ata(@sbuf[i],1)thenbegincommflg:=false;break;end;sleep(2);{发送时字节间的延时}viewstring:=viewstring+sbuf[i]+'';end;viewstring:=viewstring;fcomm.memo1.lines.add(viewstring);fcomm.memo1.lines.add('');ifnotcommflgthenmessagedlg('发送失败!',mterror,[mbyes],0);end;end.四、该程序实现编辑框中代
7、码通过串口发送,接收串口数据,显示在memo1中
此文档下载收益归作者所有