本文来自
http://blog.csdn.net/hellogv/ ,转载必须注明出处!
以下代码可到:
http://download.csdn.net/source/611385 下载
Lazarus最吸引人的地方就是她的开发方式类似Delphi,支持超好用的RAD开发方式,并且最厉害的地方是她还支持多个平台,多个CPU,例如ARM9的WINCE。
本文要讲述的就是“如何使用LAZARUS开发Wince上的串口程序”,并且,本文的串口程序同时支持WINCE和WINXP系统,当然编译时要选择平台啦。WINCE与WINXP在本文中的代码区别只是
OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);//wince用COM1:表示串口1;WINXP用COM1表示串口1.
一、建立一个可重用的类,文件名为
CE_Series.pas:
-
unitCE_Series;
-
interface
-
uses
- Windows,Classes,SysUtils,LResources,StdCtrls,ExtCtrls;
-
type
-
TCE_Series=class(TObject)
-
private
- hComm:THandle;
-
public
-
FunctionOpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
-
procedureSend(str:String);
- FunctionReceive():String;
-
procedureClosePort();
-
end;
-
implementation
-
-
-
-
-
-
-
functionTCE_Series.OpenPort(Port:LPCWSTR;BaudRate,ByteSize,Parity,StopBits:integer):String;
-
var
- cc:TCOMMCONFIG;
-
begin
-
result:='';
-
hComm:=CreateFile(port,GENERIC_READorGENERIC_WRITE,
-
0,nil,OPEN_EXISTING,0,0);
-
if(hComm=INVALID_HANDLE_VALUE)thenbegin
-
result:='CreateFileError!';
- exit;
-
end;
-
GetCommState(hComm,cc.dcb);
-
cc.dcb.BaudRate:=BaudRate;
-
cc.dcb.ByteSize:=ByteSize;
-
cc.dcb.Parity:=Parity;
-
cc.dcb.StopBits:=StopBits;
-
ifnotSetCommState(hComm,cc.dcb)thenbegin
-
result:='SetCommStateError!';
- CloseHandle(hComm);
- exit;
-
end;
-
end;
-
-
-
-
-
-
-
procedureTCE_Series.Send(str:String);
-
var
- lrc:LongWord;
-
begin
-
if(hComm=0)thenexit;
-
WriteFile(hComm,str,Length(str),lrc,nil);
-
end;
-
-
-
-
-
-
-
FunctionTCE_Series.Receive():String;
-
var
-
inbuff:array[0..2047]ofChar;
- nBytesRead,dwError:LongWORD;
- cs:TCOMSTAT;
-
begin
-
ClearCommError(hComm,dwError,@CS);
-
-
ifcs.cbInQue>sizeof(inbuff)thenbegin
-
PurgeComm(hComm,PURGE_RXCLEAR);
- exit;
-
end;
-
ReadFile(hComm,inbuff,cs.cbInQue,nBytesRead,nil);
-
-
result:=Copy(inbuff,1,cs.cbInQue);
-
end;
-
-
-
-
-
-
-
procedureTCE_Series.ClosePort();
-
begin
-
SetCommMask(hcomm,$0);
- CloseHandle(hComm);
-
end;
-
end.
二、写调用程序演示如何使用这个类,请自行加入控件,所用的控件不多:
-
unitUnit1;
-
{$modeobjfpc}{$H+}
-
interface
-
uses
- Windows,Classes,SysUtils,LResources,Forms,Controls,Graphics,Dialogs,StdCtrls,ExtCtrls
- ,CE_Series;
-
type
-
-
TForm1=class(TForm)
- btn_OpenPort:TButton;
- btn_ClosePort:TButton;
- btn_Send:TButton;
- edt_Receive:TMemo;
- GroupBox1:TGroupBox;
- edt_Send:TMemo;
- GroupBox2:TGroupBox;
- Timer1:TTimer;
-
procedurebtn_ClosePortClick(Sender:TObject);
-
procedurebtn_OpenPortClick(Sender:TObject);
-
procedurebtn_SendClick(Sender:TObject);
-
procedureTimer1Timer(Sender:TObject);
-
private
-
-
public
-
-
end;
-
var
- Form1:TForm1;
- myseries:TCE_Series;
-
implementation
-
-
procedureTForm1.btn_OpenPortClick(Sender:TObject);
-
begin
-
myseries:=TCE_Series.Create;
-
myseries.OpenPort('COM1:',CBR_9600,8,NOPARITY,ONESTOPBIT);
-
Timer1.Enabled:=true;
-
end;
-
procedureTForm1.btn_SendClick(Sender:TObject);
-
begin
-
myseries.Send(edt_Send.Text);
-
end;
-
procedureTForm1.Timer1Timer(Sender:TObject);
-
var
-
receive:string;
-
begin
-
receive:=myseries.Receive();
-
ifreceive<>''then
-
begin
-
edt_Receive.Lines.Add(receive);
-
end;
-
end;
-
procedureTForm1.btn_ClosePortClick(Sender:TObject);
-
begin
-
Timer1.Enabled:=false;
-
myseries.ClosePort();
- close;
-
end;
-
initialization
-
{$Iunit1.lrs}
-
end.
分享到:
相关推荐
Lazarus实战开发之串口通信(WINCE-WIN32)[文].pdf
wince/win32下的串口调试助手,完全用Lazarus编写。界面和功能仿龚建伟的串口调试助手 V2.2 支持文件发送和存储 支持16禁止发送和接收。
Win32/Win64/WinCE/Linux/BSD/MAC)上运行的程序。FreePascal有丰富的运行库,而且 绝大部分代码都是开源并且允许用于商业开发的。全国青少年信息学奥林匹克竞赛使用的 Pascal编译器就是FreePascal。
lazarus-1.0.8-fpc-2.6.2-cross-arm-wince-win32 freepascal 用于开发WINCE程序
WINCE和WIN32 下Modbus通讯动态库。支持回调读取。 实现了MOdbus的 01-16 功能码。支持windows和wince6.0(ARM处理器)。支持消息和事件驱动读取,不卡死。有Lazarus和delphi2007的测试例子。 完全免费使用。经过...
本例子说明了,如何在LAZARUS(开源的,类似Delphi的环境)上开发WINCE系统的串口读写程序。
lazarus-0.9.30-fpc-2.4.2-win32lazarus-0.9.30-fpc-2.4.2-win32
版本能运行于Linux,Win32和FreeBSD。整个界面的外观和操作和Delphi IDE一样,因此,如果你会使用Delphi的话,用起Lazarus IDE来就一定能得心应手了。 引集成开发环境 Lazarus是一个用于FreePascal的快速应用...
开发PDA的软件系统 Pascal语言 IDE 与Delphi 相仿
Lazarus下WinCE调试方法 Lazarus下WinCE调试方法
Lazarus的设计目标是应用Free Pascal,所以所有凡是Free ...最新版本能运行于Linux,Win32和Mac OS。整个界面的外观和操作和Delphi IDE一样,因此,如果你会使用Delphi的话,用起Lazarus IDE来就一定能得心应手了。
lazarus-1.4.2-fpc-2.6.4-win32-gree 绿色版,可以自由拷贝 感谢www.fpccn.com的各位大牛们
最新版本能运行于Linux,Win32和FreeBSD。整个界面的外观和操作和Delphi IDE一样,因此,如果你会使用Delphi的话,用起Lazarus IDE来就一定能得心应手了。 引集成开发环境 Lazarus是一个用于FreePascal的快速...
已在Debian6.06中安装lazarus 的indy组件包 已在Debian6.06中安装lazarus 的indy组件包,用的是indy 10.2.0.3。 安装过程:Version installed on Lazarus 1.04 using component indy-10.2.0.3. * Unzip the ...
lazarus-1.6.4-fpc-3.0.2-cross-i386-win32-win64,1.84版本 ,
在lazarus 环境下面开发wince项目,资源库用sqlite3小型数据库,做个例子用来学习一下。
lazarus-1.2.6-fpc-2.6.4-win32.exe
可以跑pascal(包括win10),也可以跑复杂程序
Linux平台下运用Lazarus Firebird开发数据库应用程序.pdf