直接用Moxa Technoloiges的PComm.dll在Delphi环境下使用PComm Pro通讯函数库必须先引入函数声明,将用到的3个单元文件分别是Global.pas、Mxtool.pas、PComm.pas,预先复制到工程目录中,在工程使用加入单元功能将这3个模块加入工程中即可。 数据的输入输出包括了串行数据的送出、读入、硬件线路的控制/检测及事件的引发与执行等函数;所有函数均有返回值,错误返回值均以常数定义在PComm.pas模块中,如下:(********** PComm.pas -- PComm Lib unit for Delphi (32 bit version). History: Date Author Comment 5/29/98 Casper Wrote it. 12/11/98 Casper Update**********)unit PComm;interfaceconst { 波特率设置 baud rate setting } B50 = $0;
B75 = $1;
B110 = $2;
B134 = $3;
B150 = $4;
B300 = $5;
B600 = $6;
B1200 = $7;
B1800 = $8;
B2400 = $9;
B4800 = $A;
B7200 = $B;
B9600 = $C;
B19200 = $D;
B38400 = $E;
B57600 = $F;
B115200 = $10;
B230400 = $11;
B460800 = $12;
B921600 = $13;
{ 数据位 data bit } BIT_5 = $0;
BIT_6 = $1;
BIT_7 = $2;
BIT_8 = $3;
{ 停止位 stop bit } STOP_1 = $0;
STOP_2 = $4;
{ 校验位 parity } P_EVEN = $18;
P_ODD = $8;
P_SPC = $38;
P_MRK = $28;
P_NONE = $0;
{ 调制解调器控制设置 modem control setting } C_DTR = $1;
C_RTS = $2;
{ 调制解调器线路状态 modem line status } S_CTS = $1;
S_DSR = $2;
S_RI = $4;
S_CD = $8;
{ 错误代码 error code } SIO_OK = 0;
//正确 SIO_BADPORT = -1;
{ 没有此端口或端口未打开 No such port or port not opened } SIO_OUTCONTROL = -2;
{ 无法控制此板 Can't control board } SIO_NODATA = -4;
{ 没有数据供读取或没有缓冲区供写入 No data to read or no buffer to write } SIO_OPENFAIL = -5;
{ 没有此端口或端口已打开 No such port or port has opened } SIO_RTS_BY_HW = -6;
{ 因为H/W流量控制而不能设置RTS Can't set because H/W flowctrl } SIO_BADPARM = -7;
{ 无效参数 Bad parameter } SIO_WIN32FAIL = -8;
(* 调用WIN32函数失败请调用GetLastError函数以获取错误代码 Call win32 function fail, please call } GetLastError to get the error code *) SIO_BOARDNOTSUPPORT = -9;
{ 此版不支持这个函数 Boarddo
es not support this function} SIO_FAIL = -10;
{ PCOMM函数执运行结果失败 PComm function run result fail } SIO_ABORT_WRITE = -11;
{ 写入已被锁定,用户也放弃写入 Write has blocked, and user abort write } SIO_WRITETIMEOUT = -12;
{ 已发生写入超时 Write timeout has happened } { 文件传输错误代码 file transfer error code } SIOFT_OK = 0;
//正确 SIOFT_BADPORT = -1;
{ 通讯端口不存在或未打开 No such port or port not open } SIOFT_TIMEOUT = -2;
{ 协议超时 Protocol timeout } SIOFT_ABORT = -3;
{ 用户键入的放弃 User key abort } SIOFT_FUNC = -4;
{ 函数返回的放弃 Func return abort } SIOFT_FOPEN = -5;
{ 无法打开文件 Can not open files } SIOFT_CANABORT = -6;
{ YMODEM取消信号终止 Ymodem CAN signal abort } SIOFT_PROTOCOL = -7;
{ 协议错误检测终止 Protocol checking error abort } SIOFT_SKIP = -8;
{ ZMODEM远程忽略此文件 Zmodem remote skip this send file } SIOFT_LACKRBUF = -9;
{ ZMODEM接收缓冲区过小 Zmodem Recv-Buff size must >= 2K bytes } SIOFT_WIN32FAIL = -10;
(* 操作系统失败 OS fail } GetLastError to get the error code *) //调用GetLastError函数取得错误代码 SIOFT_BOARDNOTSUPPORT = -11;
{ 此版不支持这个函数 Boarddo
es not support this function}type IrqProc = procedure(port: Longint);stdcall;
CallBackProc = function(len: Longint;
rlen: Longint;
buf: PChar;
flen: Longint): Longint;stdcall;{Import routine from PComm.dll}function sio_open(port: Longint): Longint;
stdcall;
(*打开通讯端口。参数需给定通讯端口号码,} 例如使用COM1就将参数设为1;可设置的范围为1~256。*)function sio_close(port: Longint): Longint;
stdcall;
(*关闭通讯端口,此举会导致所有的传送机接收的动作 都停止。参数为通讯端口号码*)function sio_ioctl(port, baud, mode: Longint): Longint;
stdcall;
(*设置传输的参数,需给定的参数有三个, 包括COM端口号、BaudRate、Mode(含Payity、DataBits、SotpBit三项)。 BaudRate设置以下不同的整数代表不同的设置,如下: ---------- | 整数|设置值(bps) ||整数|设置值(bps)| 整数|设置值(bps) ||整数|设置值(bps) | —————————— |0 | 50 || 5 | 300 | 10 | 4800 || 15 | 57600 | |1 | 75 || 6 | 600 | 11 | 7200 || 16 | 115200 | |2 | 110 || 7 | 1200 | 12 | 9600 || 17 | 230400 | |3 | 134.5 || 8 | 1800 | 13 | 9600 || 18 | 460800 | |4 | 150 || 9 | 2400 | 14 | 38400 || 19 | 921600 | ---------- Mode参数含有三项,如下所示: bit_cnt(bit 0,1): 0x00=bit_5 0x01=bit_6 0x02=bit_7 0x03=bit_8 stop_bit(bit 2): 0x00=stop_1 0x04=stop_2 parity(bit 3,4,5): 0x00=none 0x08=odd 0x18=even 0x28=mark 0x38=space 不同的参数使用到不同的位合成,已经都将可能的数值列出,由于有三个参数混在一起,设置前需将 每个所代表的数值先算出来,再用相加的运算加起来代入mode参数即可。*)function sio_flowctrl(port, mode: Longint): Longint;
stdcall;
(* 设置软件或硬件流量控制。有两个参数, 第一个是通讯端口号码,第二个是流量控制的设置,如下: mode=bit 0:CTS flow control bit 1:RTS flow control bit 2:Tx XON/XOFF flow control bit 3:Rx XON/XOFF flow control(0=OFF,1=ON) *)function sio_flush(port, func: Longint): Longint;
stdcall;
(*清空输入或输出缓冲区。有两个参数, 第一个是通讯端口号码,第二个是清空选项,如下: func=flush function 0:flush input buffer 1:flush output buffer 2:flush input & output buffer *)function sio_DTR(port, mode: Longint): Longint;
stdcall;
(* 设置DTR的线路状态。有两个参数, 第一个是通讯端口号码,第二个设0时降低DTR电压,设1时升高DTR电压 *)function sio_RTS(port, mode: Longint): Longint;
stdcall;
(* 设置RTS的线路状态。*)function sio_lctrl(port, mode: Longint): Longint;
stdcall;
(* 同时设置DTR和RTS的线路状态.有两个参数, 第一个是通讯端口号码,第二个是DTR和RTS的组合。*)function sio_baud(port, speed: Longint): Longint;
stdcall;function sio_getch(port: Longint): Longint;
stdcall;
(* 自输入缓冲区中读取一个字符(实际是一个字节)。 有一个参数,即通讯端口号。返回值介于0~255(ASCLL表上的最大数值)。*)function sio_read(port: Longint;
buf: PChar;
len: Longint): Longint;
stdcall;
(* 读取字符串。有三个参数, 其中的一个是端口号;第二个是字节数组地址,用以存放接收到的字节数据;的三个是每一次所读取的数据 长度(字节数)。*)function sio_linput(port: Longint;
buf
Char;
len: Longint;
term:Longint): Longint;
stdcall;function sio_putch(port, term: Longint): Longint;
stdcall;
(*将一个字符写到输出缓冲区。有两个参数,其一是 端口号,第二是将传送出去的字节(数值0~255)。*)function sio_putb(port: Longint;
buf
Char;
len: Longint): Longint;
stdcall;function sio_write(port: Longint;
buf
Char;
len: Longint): Longint;
stdcall;
(*输出字符串。有三个参数, 第一个是端口号;第二是输出字符串的地址;第三是输出字符串的长度。*)function sio_putb_x(port: Longint;
buf
Char;
len: Longint;
tick:Longint): Longint;
stdcall;function sio_putb_x_ex(port: Longint;
buf
Char;
len: Longint;
tms:Longint): Longint;
stdcall;function sio_lstatus(port: Longint): Longint;
stdcall;
(* 得到现在硬件线路的状态(DCD、CTS、DSR、RI)。 参数只有一个,通讯端口号码;返回值由0位至第3位分别表示CTS、DSR、RI、CD四条线的状态。*)function sio_iqueue(port: Longint): Longint;
stdcall;function sio_oqueue(port: Longint): Longint;
stdcall;function sio_Tx_hold(port: Longint): Longint;
stdcall;function sio_getbaud(port: Longint): Longint;
stdcall;function sio_getmode(port: Longint): Longint;
stdcall;function sio_getflow(port: Longint): Longint;
stdcall;function sio_data_status(port: Longint): Longint;
stdcall;//事件触发函数function sio_term_irq(port: Longint;
func: IrqProc;
code: Byte): Longint;
stdcall;
(*当收到终止字符串时触发事件程序。 有三个参数,1:端口号吗;2:函数地址;3:终止字符。*)function sio_cnt_irq(port: Longint;
func: IrqProc;
count: Longint): Longint;
stdcall;
(* 接收到固定字符时 触发事件。使用手册特别说明必须将字符数设置为1,也就是一有字符进来就触发事件。有三个参数, 1:通讯端口号码;2:函数地址;3:字符数。*)function sio_modem_irq(port: Longint;
func: IrqProc): Longint;
stdcall;
(*当硬件线路的电压发生变化时 触发事件,硬件线路包括DCD、DSR、CTS、RI这四个引线。有两个参数,1:端口号码;2:函数地址 *)function sio_break_irq(port: Longint;
func: IrqProc): Longint;
stdcall;
(*当接收到中断信号时,触发事件。 有两个参数;1:端口号;2:函数地址。*)function sio_Tx_empty_irq(port: Longint;
func: IrqProc): Longint;
stdcall;(*当传送缓冲区全空时触发事件。 有两个参数;1:端口号;2:函数地址。*){PComm针对事件的处理是采用回调函数的方式处理,与建立事件,需给定一个函数的地址,当事件发生时,PComm变到该函数所在地址去执行该代码。}{传送给中断服务例程的函数的写法必须依照PCommm Pro的格式,在上面所提到的这些需传送地址的函数中,其参数均是通讯端口号码,而内容则由工程师自由发挥。 上述的事件中,最有用的是sio_cnt_irq及sio_modem_irq这两个中断函数。}function sio_break(port, time: Longint): Longint;
stdcall;function sio_view(port: Longint;
buf: PChar;
len: Longint): Longint;
stdcall;function sio_TxLowWater(port, size: Longint): Longint;
stdcall;function sio_AbortWrite(port: Longint): Longint;
stdcall;
(*终止输出的动作。有一个参数,即通讯端口号码*)function sio_AbortRead(port: Longint): Longint;
stdcall;
(*中断字符或字符串的读取。有一个参数,端口号*)function sio_SetWriteTimeouts(port, timeouts: Longint): Longint;
stdcall;function sio_GetWriteTimeouts(port: Longint;
var TotalTimeouts:Longint): Longint;
stdcall;function sio_SetReadTimeouts(port, TotalTimeouts, IntervalTimeouts: Longint): Longint;
stdcall;function sio_GetReadTimeouts(port: Longint;
var TotalTimeouts, IntervalTimeouts: Longint): Longint;
stdcall;//文件传输函数function sio_FtASCIITx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用ASCII协议传送文件。参数4个:第一个是端口号码;第二个是传送的文件名;第三个是回调函数,用来指明传输的相关状态;第四个是用户定义的取消快速键}function sio_FtASCIIRx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint;
sec:Longint): Longint;
stdcall;{使用ASCII协议接收文件。参数5个,第一个是端口号码;第二个是文件名称;第三个是回调函数,用来指明传输的相关状态;第四个是用户定义的取消快速键;第五个是以秒计的超时时间}function sio_FtXmodemCheckSumTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用XMODEM,Checksum协议传送文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtXmodemCheckSumRx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用XMODEM,Checksum协议接收文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtXmodemCRCTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用XMODEM,CRC协议传送文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtXmodemCRCRx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;
{使用XMODEM,CRC协议接收文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtXmodem1KCRCTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用XMODEM,1K CRC协议传送文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtXmodem1KCRCRx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用XMODEM,1K CRC协议接收文件。有四个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtYmodemTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用YXMODEM协议传送文件。有4个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtYmodemRx(port:Longint;
var fname
Char;fno:LongInt;func:CallBackProc;
key:Longint): Longint;
stdcall;{使用YXMODEM协议接收文件。有5个参数:第一个是端口号;第二个是文件数据数组地址;第三个是可接收的最多文件数;第四个是回调函数;第五个是用户定义的取消快速键}function sio_FtZmodemTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用ZXMODEM协议传送文件。有4个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtZmodemRx(port:Longint;
var fname
Char;fno:LongInt;func:CallBackProc;
key:Longint): Longint;
stdcall;{使用ZXMODEM协议接收文件。有5个参数:第一个是端口号;第二个是文件数据数组地址;第三个是可接收的最多文件数;第四个是回调函数;第五个是用户定义的取消快速键}function sio_FtKermitTx(port:Longint;
fname
Char;
func:CallBackProc;
key:Longint): Longint;
stdcall;{使用Kermit协议传送文件。有4个参数:第一个是端口号;第二个是文件名;第三个是回调函数;第四个是用户定义的取消快速键}function sio_FtKermitRx(port:Longint;
var fname
Char;fno:LongInt;func:CallBackProc;
key:Longint): Longint;
stdcall;{使用Kermit协议接收文件。有5个参数:第一个是端口号;第二个是文件数据数组地址;第三个是可接收的最多文件数;第四个是回调函数;第五个是用户定义的取消快速键}implementationfunction sio_open;
external 'PComm.dll';function sio_close;
external 'PComm.dll';function sio_ioctl;
external 'PComm.dll';function sio_flowctrl;
external 'PComm.dll';function sio_flush;
external 'PComm.dll';function sio_DTR;
external 'PComm.dll';function sio_RTS;
external 'PComm.dll';function sio_lctrl;
external 'PComm.dll';function sio_baud;
external 'PComm.dll';function sio_getch;
external 'PComm.dll';function sio_read;
external 'PComm.dll';function sio_linput;
external 'PComm.dll';function sio_putch;
external 'PComm.dll';function sio_putb;
external 'PComm.dll';function sio_write;
external 'PComm.dll';function sio_putb_x;
external 'PComm.dll';function sio_putb_x_ex;
external 'PComm.dll';function sio_lstatus;
external 'PComm.dll';function sio_iqueue;
external 'PComm.dll';function sio_oqueue;
external 'PComm.dll';function sio_Tx_hold;
external 'PComm.dll';function sio_getbaud;
external 'PComm.dll';function sio_getmode;
external 'PComm.dll';function sio_getflow;
external 'PComm.dll';function sio_data_status;
external 'PComm.dll';function sio_term_irq;
external 'PComm.dll';function sio_cnt_irq;
external 'PComm.dll';function sio_modem_irq;
external 'PComm.dll';function sio_break_irq;
external 'PComm.dll';function sio_Tx_empty_irq;
external 'PComm.dll';function sio_break;
external 'PComm.dll';function sio_view;
external 'PComm.dll';function sio_TxLowWater;
external 'PComm.dll';function sio_AbortWrite;
external 'PComm.dll';function sio_AbortRead;
external 'PComm.dll';function sio_SetWriteTimeouts;
external 'PComm.dll';function sio_GetWriteTimeouts;
external 'PComm.dll';function sio_SetReadTimeouts;
external 'PComm.dll';function sio_GetReadTimeouts;
external 'PComm.dll';function sio_FtASCIITx;
external 'PComm.dll';function sio_FtASCIIRx;
external 'PComm.dll';function sio_FtXmodemCheckSumTx;
external 'PComm.dll';function sio_FtXmodemCheckSumRx;
external 'PComm.dll';function sio_FtXmodemCRCTx;
external 'PComm.dll';function sio_FtXmodemCRCRx;
external 'PComm.dll';function sio_FtXmodem1KCRCTx;
external 'PComm.dll';function sio_FtXmodem1KCRCRx;
external 'PComm.dll';function sio_FtYmodemTx;
external 'PComm.dll';function sio_FtYmodemRx;
external 'PComm.dll';function sio_FtZmodemTx;
external 'PComm.dll';function sio_FtZmodemRx;
external 'PComm.dll';function sio_FtKermitTx;
external 'PComm.dll';function sio_FtKermitRx;
external 'PComm.dll';
end.
;==========数字输入/输出控制例程:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) rdCOM: TRadioGroup;
Label1: TLabel;
Label2: TLabel;
btnOpenPort: TButton;
btnEnd: TButton;
spCD: TShape;
spDSR: TShape;
spCTS: TShape;
Label3: TLabel;
Label4: TLabel;
spRI: TShape;
Timer1: TTimer;
spDTR: TShape;
Label5: TLabel;
Label6: TLabel;
spRTS: TShape;
btnDTR: TButton;
btnRTS: TButton;
procedure btnOpenPortClick(Sender: TObject);
procedure btnEndClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure btnDTRClick(Sender: TObject);
procedure btnRTSClick(Sender: TObject);
private public { Public declarations } end;
var Form1: TForm1;
DTRMode,RTSMode,Port:Integer;
Function PortSet():Boolean;implementation uses PComm,MxTool,EXGLOBAL;
//PCOmm引用声明放于此{$R *.DFM}//以下是打开通信端口的程序procedure TForm1.btnOpenPortClick(Sender: TObject);var Ret:Integer;
begin
//打开通信端口 Port := rdCom.ItemIndex+1;
//指定通信端口 Ret := si
pen(Port);
If ret <> SIO_OK then
begin
ShowMessage('打开通信端口错误');
sio_close (Port);
Exit;
end;
If Not PortSet() then
//参数设置 begin
sio_close(Port);
ShowMessage('通信端口参数设置发生错误');
Exit;
end;
//降低DTR及RTS的电压 ret := sio_DTR(Port, 0);
ret := sio_RTS(Port, 0);
//激活定时器 Timer1.Enabled := True;
end;
//以下是结束按钮的动作procedure TForm1.btnEndClick(Sender: TObject);
begin
//关闭通信端口 sio_Close(Port);
//结束程序 close;
end;
procedure TForm1.Timer1Timer(Sender: TObject);var Ret:Integer;
begin
ret := sio_lstatus(Port);//读取状态 If ret < 0 then
begin
ShowMessage('状态错误');
Timer1.Enabled := False;
end else
begin
//各线路状态检查 If (ret And S_DSR) > 0 then
spDSR.Brush.Color := clRed else
spDSR.Brush.Color := clWhite;
If (ret And S_CD) > 0 then
spCD.Brush.Color := clRed else
spCD.Brush.Color := clWhite;
If (ret And S_RI) > 0 then
spRI.Brush.Color := clRed else
spRI.Brush.Color := clWhite;
If (ret And S_CTS) > 0 then
spCTS.Brush.Color := clRed else
spCTS.Brush.Color := clWhite;
end;
end;
procedure TForm1.btnDTRClick(Sender: TObject);var ret:Integer;
begin
//计算DTR线路状态 DTRMode := (DTRMode + 1) Mod 2 ;
If DTRMode = 1 then
spDTR.Brush.Color:=clRed else
spDTR.Brush.Color:=clWhite;
//控制DTR线路状态 ret := sio_DTR(Port, DTRMode);
If ret <> SIO_OK then
begin
ShowMessage('DTR控制错误');
Exit;
end;
end;
procedure TForm1.btnRTSClick(Sender: TObject);var ret:Integer;
begin
//计算RTS状态 RTSMode := (RTSMode + 1) Mod 2 ;
If RTSMode = 1 then
spRTS.Brush.Color:=clRed else
spRTS.Brush.Color:=clWhite;
//控制RTS状态 ret := sio_RTS(Port, RTSMode);
If ret <> SIO_OK then
begin
ShowMessage('RTS控制错误');
Exit;
end;
end;
//以下是通信参数的函数实现Function PortSet():Boolean;var mode,Hw,Sw,ret,tout:LongInt;
begin
//参数设置子程序 mode := P_NONE Or BIT_8 Or STOP_1;
Hw := 0 ;
//没有硬件流量控制 Sw := 0 ;
//没有软件流量控制 Result := False ;
ret := sio_ioctl(Port, B38400, mode);
//Setting If ret <> SIO_OK then
begin
ShowMessage('设置时发生错误');
Exit;
end;
ret := sio_flowctrl(Port, Hw or Sw);
//Flow Control If ret <> SIO_OK then
begin
ShowMessage('流量设置时发生错误');
Exit;
end;
Result := True;
end;
end.
;==========数据的传送例程:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) rdCOM: TRadioGroup;
btnOpenPort: TButton;
btnEnd: TButton;
btnSend: TButton;
btnReceive: TButton;
mSend: TMemo;
mReceive: TMemo;
procedure btnOpenPortClick(Sender: TObject);
procedure btnEndClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure btnReceiveClick(Sender: TObject);
private public { Public declarations } end;
var Form1: TForm1;
DTRMode,RTSMode,Port:Integer;
Function PortSet():Boolean;implementation uses PComm,MxTool,EXGLOBAL;
//PCOmm引用声明放于此{$R *.DFM}//以下是打开通信端口的程序procedure TForm1.btnOpenPortClick(Sender: TObject);var Ret:Integer;
begin
//打开通信端口 Port := rdCom.ItemIndex+1;
//指定通信端口 Ret := si
pen(Port);
If ret <> SIO_OK then
begin
ShowMessage('打开通信端口错误');
sio_close (Port);
Exit;
end;
If Not PortSet() then
//参数设置 begin
sio_close(Port);
ShowMessage('通信端口参数设置发生错误');
Exit;
end;
//降低DTR及RTS的电压 ret := sio_DTR(Port, 0);
ret := sio_RTS(Port, 0);
end;
//以下是结束按钮的动作procedure TForm1.btnEndClick(Sender: TObject);
begin
//关闭通信端口 sio_Close(Port);
//结束程序 close;
end;
procedure TForm1.btnSendClick(Sender: TObject);var TxtStr:String;
begin
//输出字符串指定 TxtStr := mSend.
Text;
//把Pascal字符串转成C字符串后送出 sio_write(Port, PChar(TxtStr), Length(TxtStr))end;
procedure TForm1.btnReceiveClick(Sender: TObject);var rLen:LongInt;
PBuf
Char;
Buf:String;
begin
//给一个空间存数据,一定要有 PBuf := PChar(StringOfChar(' ',1024));
rlen := sio_read(Port,PBuf , 1024);
//读取数据 If rlen = 0 then
Exit;
//若无数据则跳出 Buf := StrPas(PBuf);
Buf := Copy(Buf,1,rlen);
//将数据显示在Memo中 mReceive.Text := mReceive.Text + Buf;
mReceive.SelStart := Length(mReceive.Text);
mReceive.SelLength := 0;
end;
//以下是通信参数的函数实现Function PortSet():Boolean;var mode,Hw,Sw,ret:LongInt;
begin
//参数设置子程序 mode := P_NONE Or BIT_8 Or STOP_1;
Hw := 0 ;
//没有硬件流量控制 Sw := 0 ;
//没有软件流量控制 Result := False ;
ret := sio_ioctl(Port, B38400, mode);
//Setting If ret <> SIO_OK then
begin
ShowMessage('设置时发生错误');
Exit;
end;
ret := sio_flowctrl(Port, Hw or Sw);
//Flow Control If ret <> SIO_OK then
begin
ShowMessage('流量设置时发生错误');
Exit;
end;
Result := True;
end;
end.
;==========事件触发例程:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) rdCOM: TRadioGroup;
Label1: TLabel;
Label2: TLabel;
btnOpenPort: TButton;
btnEnd: TButton;
spCD: TShape;
spDSR: TShape;
spCTS: TShape;
Label3: TLabel;
Label4: TLabel;
spRI: TShape;
mSend: TMemo;
mReceive: TMemo;
Label5: TLabel;
Label6: TLabel;
procedure btnOpenPortClick(Sender: TObject);
procedure btnEndClick(Sender: TObject);
Function PortSet():Boolean;
Procedure ClearIrq();
Function InitIrq():Boolean;
procedure mSendKeyPress(Sender: TObject;
var Key: Char);
private public { Public declarations } end;
var Form1: TForm1;
DTRMode,RTSMode,Port:Integer;
Procedure CntIrq(iPort:LongInt);stdcall;
Procedure ModemIrq(iPort:LongInt);stdcall;implementation uses PComm,MxTool,EXGLOBAL;
//PCOmm引用声明放于此{$R *.DFM}//以下是打开通信端口的程序procedure TForm1.btnOpenPortClick(Sender: TObject);var Ret:Integer;
begin
//打开通信端口 Port := rdCom.ItemIndex+1;
//指定通信端口 Ret := si
pen(Port);
If ret <> SIO_OK then
begin
ShowMessage('打开通信端口错误');
sio_close (Port);
Exit;
end;
If Not PortSet() then
//参数设置 begin
sio_close(Port);
ShowMessage('通信端口参数设置发生错误');
Exit;
end;
//降低DTR及RTS的电压 ret := sio_DTR(Port, 0);
ret := sio_RTS(Port, 0);
//设置中断 if not InitIrq then
begin
sio_close(Port);
ShowMessage('通信端口参数设置发生错误');
Exit;
end;
end;
//以下是结束按钮的动作procedure TForm1.btnEndClick(Sender: TObject);
begin
//关闭通信端口 ClearIrq;
sio_Close(Port);
//结束程序 close;
end;
//以下是通信参数的函数实现Function TForm1.PortSet():Boolean;var mode,Hw,Sw,ret:LongInt;
begin
//参数设置子程序 mode := P_NONE Or BIT_8 Or STOP_1;
Hw := 0 ;
//没有硬件流量控制 Sw := 0 ;
//没有软件流量控制 Result := False ;
ret := sio_ioctl(Port, B38400, mode);
//Setting If ret <> SIO_OK then
begin
ShowMessage('设置时发生错误');
Exit;
end;
//设置流量控制 ret := sio_flowctrl(Port, Hw or Sw);
//Flow Control If ret <> SIO_OK then
begin
ShowMessage('流量设置时发生错误');
Exit;
end;
Result := True;
end;
//准备被调用的回调函数,此函数必须独立,不可放进窗体//此函数用来接收它方所传送过来的数据Procedure CntIrq(iPort:LongInt);stdcall;var rLen:LongInt;
PBuf
Char;
Buf:String;
begin
//给一个空间存数据,一定要有 PBuf := PChar(StringOfChar(' ',1024));
rlen := sio_read(iPort,PBuf , 1024);
//读取数据 If rlen = 0 then
Exit;
//若无数据则跳出 Buf := StrPas(PBuf);
Buf := Copy(Buf,1,rlen);
//将数据显示在Memo中,并将光标拉至最低处 Form1.mReceive.Text := Form1.mReceive.Text + Buf;
Form1.mReceive.SelStart := Length(Form1.mReceive.Text);
Form1.mReceive.SelLength := 0;
end;
//准备被调用的回调函数,此函数必须独立,不可放进窗体//此函数用来检测硬件线路状态Procedure ModemIrq(iPort:LongInt);stdcall;var ret:integer;
begin
ret := sio_lstatus(iPort);
//读取状态If ret < 0 then
ShowMessage('状态错误')else
//以下依状况改变灯号的颜色 begin
If (ret And S_DSR) > 0 then
Form1.spDSR.Brush.Color := clRed else
Form1.spDSR.Brush.Color := clWhite;
If (ret And S_CD) > 0 then
Form1.spCD.Brush.Color := clRed else
Form1.spCD.Brush.Color := clWhite;
If (ret And S_RI) > 0 then
Form1.spRI.Brush.Color := clRed else
Form1.spRI.Brush.Color := clWhite;
If (ret And S_CTS) > 0 then
Form1.spCTS.Brush.Color := clRed else
Form1.spCTS.Brush.Color := clWhite;
End ;
end ;//中断的初始化函数//在此指定了Count的中断及硬件线路的中断Function TForm1.InitIrq():Boolean;var ret:LongInt;
begin
Result := False;
ret := sio_cnt_irq(Port, CntIrq, 1);
//指定接收事件中断 If ret <> SIO_OK then
begin
ShowMessage('事件设置时发生错误-cntIrq');
Exit;
end;
ret := sio_modem_irq(Port, ModemIrq);
//指定硬件线路中断 If ret <> SIO_OK then
begin
ShowMessage('事件设置时发生错误-ModemIrq');
Exit;
end;
Result := TrueEnd ;//中断的清除//将原先设置的中断全部清除掉Procedure TForm1.ClearIrq();var ret:LongInt;
begin
sio_cnt_irq(Port, nil, 0);
sio_modem_irq(Port,nil);
end;
//将输入的字符送出procedure TForm1.mSendKeyPress(Sender: TObject;
var Key: Char);
begin
//送出字符 sio_putch(Port,Ord(Key));
end;
end.
;==========文件传输例程:unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) txtReceive: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
txtTransfer: TEdit;
Button2: TButton;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
txtSize: TEdit;
txtName: TEdit;
txtLen: TEdit;
Button3: TButton;
rdCOM: TRadioGroup;
btnOpenPort: TButton;
Function PortSet():Boolean;
Procedure RefreshStatus(SxLen:LongInt;
SfLen:LongInt;
SFname
Char);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure btnOpenPortClick(Sender: TObject);
private { Private declarations } public { Public declarations } end;
var Form1: TForm1;implementation uses FtProc,PComm;
//使用的单元声明在此{$R *.DFM}procedure TForm1.Button2Click(Sender: TObject);
begin
//决定要传输的文件 if not OpenDialog1.Execute then
exit;
lstrcpy(GxFname,PChar(OpenDialog1.FileName));
//文件名显示在画面上 txtTransfer.Text := OpenDialog1.FileName;
//指定各项须使用的变量 GftCancel := False;
GProtocol := FTZMDM;
//使用ZModem GDirection := FT_XMIT;
//指定传输的动作 TFtProc.Create(false);
//线程建立并执行end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GftCancel := False;
GProtocol := FTZMDM;
//使用ZModem GDirection := FT_RECV;
//指定接收的动作 TFtProc.Create(false);
//线程建立并执行end;
//以下是状态显示的回调函数Procedure TForm1.RefreshStatus(SxLen:LongInt;
SfLen:LongInt;
SFname
Char);
begin
//文件大小 txtSize.Text := IntToStr(SfLen);
//文件名称 txtName.Text := StrPas(SFName);
//已传输的文件位元组数 txtLen.Text := IntToStr(SxLen);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
sio_Close(Port);
//关闭程序 Close;
end;
procedure TForm1.btnOpenPortClick(Sender: TObject);var Ret:Integer;
begin
//开启通讯端口 Port := rdCom.ItemIndex+1;
//指定通讯端口 Ret := si
pen(Port);
If ret <> SIO_OK then
begin
ShowMessage('打开通讯端口错误');
sio_close (Port);
Exit;
end;
If Not PortSet() then
//参数设定 begin
sio_close(Port);
ShowMessage('通讯端口参数设定发生错误');
Exit;
end;
//降低DTR及RTS的电压 ret := sio_DTR(Port, 0);
ret := sio_RTS(Port, 0);
end;
//以下是通讯参数的函数实现Function TForm1.PortSet():Boolean;var mode,Hw,Sw,ret:LongInt;
begin
//参数设定副程序 mode := P_NONE Or BIT_8 Or STOP_1;
Hw := 0 ;
//没有硬件流量控制 Sw := 0 ;
//没有软件流量控制 Result := False ;
//指定必要的参数 ret := sio_ioctl(Port, B38400, mode);
//Setting If ret <> SIO_OK then
begin
ShowMessage('设定时发生错误');
Exit;
end;
//设定流量控制 ret := sio_flowctrl(Port, Hw or Sw);
//Flow Control If ret <> SIO_OK then
begin
ShowMessage('流量设定时发生错误');
Exit;
end;
Result := True;
end;
end.
;----------END