📄 readme.txt
字号:
spcomm
搜集整理:giver 来源:不详 作者:佚名
收录时间:2004-1-28 阅读次数:本日/总计: 2/120
Delphi 是新一代可视化开发工具,它具有功能强大、简便易用和代码执行速度快等特点,是全球公认的快速应用开发工具技术的先驱者,它越来越在构架企业信息系统方面发挥着重要作用。由于Delphi 这些显著特点,许多程序员选择Delphi作为开发工具编制各种应用程序。但是,令人惋惜的是Delphi没有自带串口通讯的控件,在它的帮助文档里也没有提及串口通讯,这就给编制通讯程序的开发人员带来众多麻烦,影响了开发进度,下面就这一技术进行讨论。
用Delphi 实现串口通讯,常用的几种方法为:使用控件如MSCOMM和SPCOMM,使用API函数或者在Delphi 中调用其它串口通讯程序。利用API编写串口通信程序较为复杂,需要掌握大量通信知识,其优点是可实现的功能更强大,应用面更广泛,更适合于编写较为复杂的低层次通信程序。相比较而言,利用SPComm控件则相对较简单,该控件具有丰富的与串口通信密切相关的属性及事件,提供了对串口的各种操作。
使用控件这一方法容易掌握,而SPCOMM支持多线程,所以SPCOMM控件的应用更加广泛。结合实例详细介绍SPCOMM的使用。
一.SPCOMM控件的安装
1.选择下拉菜单Component的第二项Install Component 。
图1
弹出图1所示的窗口,在Unit file name 处填写控件SPCOMM控件所在路径,其它可用默认值,点击OK按纽。
2.安装成功后,system控件面板中将出现一个红色控件COMM。现在使用COMM控件可以象Delphi自带控件一样使用。
二.SPCOMM的主要属性,方法和事件
1.属性
CommName:填写COM1,COM2…等串口的名字,在打开串口前,必须填写好此值。
BaudRate:设定波特率9600,4800等,根据实际需要来定,在串口打开后也可更改波特率,实际波特率随之更改。
ParityCheck:奇偶校验。
ByteSize:字节长度_5,_6,_7,_8等,根据实际情况设定。
Parity:奇偶校验位
pBits:停止位
SendDataEmpty:这是一个布尔属性,为true时表示发送缓存为空,或者发送队列里没有信息;为False时表示表示发送缓存不为空,或者发送队列里有信息。
2.方法
Startcomm过程用于打开串口,当打开失败时通常会报错,错误主要有7种:
⑴串口已经打开 ;
⑵打开串口错误 ;
⑶文件句柄不是通讯句柄;
⑷不能够安装通讯缓存;
⑸不能产生事件 ;
⑹不能产生读进程;
⑺不能产生写进程;
StopComm过程用于关闭串口,没有返回值。
函数WriteCommData(pDataToWrite: PChar;dwSizeofDataToWrite:Word ): boolean 用于发送一个字符串到写线程,发送成功返回true,发送失败返回false, 执行此函数将立即得到返回值,发送操作随后执行。函数有两个参数,其中 pdatatowrite是要发送的字符串,dwsizeofdatatowrite 是发送的长度。
3.事件
OnReceiveData : procedure (Sender: TObject;Buffer: Pointer;BufferLength: Word) of object
当输入缓存有数据时将触发该事件,在这里可以对从串口收到的数据进行处理。Buffer中是收到的数据,bufferlength是收到的数据长度。
OnReceiveError : procedure(Sender: TObject; EventMask : DWORD)
当接受数据时出现错误将触发该事件。
三.SPCOMM的使用
下面,我们结合一个串口通讯的例子来说明SPCOMM的使用。
为了实现PC与单片机8051之间的通讯,首先要调通它们之间的握手信号,假定它们之间的通讯协议是,PC到8051一帧数据6个字节,8051到PC一帧数据也为6个字节,当PC发出(F0,01,FF,FF,01,F0)后能收到这样一帧(F0,01,FF,FF,01,F0),表示数据通信握手成功,两者之间就可以按照协议相互传输数据。在PC方要发送及接受数据需要以下步骤:
1.创建一个新的工程COMM.DPR,把窗体的NAME属性改为FCOMM,把窗体的标题改为测试通讯,添加控件。
对COMM1(黑色矩形围住的控件)进行属性设计,设波特率4800,校验位无,字节长度_8,停止位_1,串口选择COM1。Memo1中将显示发送和接受的数据。选择File/Save As将新的窗体存储为Comm.pas。
2.编写源代码
变量说明
var
FCOMM: TFCOMM;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..6] of byte;
打开串口
procedure TFCOMM.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
关闭串口
procedure TFCOMM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
发送数据
自定义的发送过程
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:="";
commflg:=true;
for i:=1 to 6 do
begin
if not fcomm.comm1.writecommdata(@sbuf[i],1) then
begin
commflg:=false;
break;
end;
sleep(2); {发送时字节间的延时}
viewstring:=viewstring+inttohex(sbuf[i],2)+" ";
end;
viewstring:="发送"+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add("");
if not commflg then messagedlg("发送失败!",mterror,[mbyes],0);
end;
procedure TFCOMM.Btn_sendClick(Sender: TObject);{发送按钮的点击事件}
begin
sbuf[1]:=byte($f0); {帧头}
sbuf[2]:=byte($01); {命令号}
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($0f); {帧尾}
senddata;{调用发送函数}
end;
接收过程
procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:="";
move(buffer^,pchar(@rbuf^),bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf[i],2)+" ";
viewstring:="接受"+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add("");
end;
如果memo1上显示发送F0 01 FF FF 0F 和 接受F0 01 FF FF F0
这表示串口已正确的发送出数据并正确的接受到数据,串口通讯成功。
COMM32.PAS
==========
Version 1.00
Comm32.pas is a simple Communications VC for Borland Delphi 2.0 which
demonstrates the Win32 Communications functions and the new Delphi
'TThread' class. It is implemented using two threads: one for reading
from, and one for writing to a Comm Port.
It probably needs to be implemented as a single thread for read/write
operations if it is needed for any synchronisation functions (such as
file-transfer algorithms).
I started to create a 'TAPI' component to use in conjunction with this
component (hence the 'OnRequestHangup' property) but as of this version
it is incomplete.
Version 1.02 - by small-pig team
1. Add Read/Write timing control
2. Add Data bits, Parity, Stop bits properties
3. Support software and hardware flow control: DTR/DSR, CTS/CTS, XON/XOFF
4. Add 'Sender' parameter in OnReceiveData
Version 2.0 - - by small-pig team
1. Support separatly DTR/DSR and RTS/CTS hardware flow control setting
2. Support separatly OutX and InX software flow control setting
3. Log file(for debug) may used by many comms at the same time
4. Add DSR sensitivity property
5. You can set the error char. replacement when parity error
6. Let XonLim/XoffLim and XonChar/XoffChar setting by yourself
7. You may change flow-control when comm is still opened
8. Change TComm32 to TComm
9. Add OnReceiveError event handler when overrun, framing error,
parity error
10. Fix some bug
Version 2.01 - - by small-pig team
1. Support some property about modem.
2. Add OnModemStateChange event hander when RLSD(CD) change state
Version 2.02 - - by small-pig team
1. Bug fix: When receive XOFF character, the system FAULT!!!!
2. Remove CommFileLog property
SPCOMM.PAS
==========
Version 2.5
1. Add OnSendDataEmpty event handler when all data in buffer
are sent(send-buffer become empty) this handler is called.
You may call send data here.
2. Change the ModemState parameters in OnModemStateChange
to ModemEvent to indicate what modem event make this call
3. Add RING signal detect. When RLSD changed state or
RING signal was detected, OnModemStateChange handler is called
4. Change XonLim and XoffLim from 100 to 500
5. Remove TWriteThread.WriteData member
6. PostHangupCall is re-design for debuging function
7. Add a boolean property SendDataEmpty, True when send buffer
is empty
USAGE
=====
To use the component once it is installed:
1) Attach an event handler to 'OnReceiveData'.
2) Call 'StartComm' to open the port.
3) Use the 'WriteCommData' method to write to the Comm port.
4) Call 'StopComm' to close the port.
PROPERTY
========
CommName : String
The name of comm port. The comm port is named 'COM1', 'COM2',...
This comm port must exist when you open it.
BaudRate : DWORD
The baud rate for this comm port. It must be a legal value for your
serial port can accept it.
You can change this value when the comm is open and
the real baud rate is changed immediately.
ParityCheck : Boolean
Specifies whether parity checking is enabled. If this member is TRUE,
parity checking is performed and errors are reported
(to OnReceiveError handler).
You can change this value when the comm is open.
Outx_CtsFlow : Boolean
Specifies whether the CTS (clear-to-send) signal is monitored
for output flow control. If this member is TRUE and CTS is turned off,
output is suspended until CTS is sent again.
CTS is a input pin. You can read its state from MSR register. It
usually connect to RTS pin in the other end.
It is often used for hardware flow control to indicate that the other
end if being waiting for data.
You can change this value when the comm is open.
CTS Input on | the other end is waiting for data
------------+----------------------------------------
Input off | the other end will NOT receive any data
Outx_DsrFlow : Boolean
Specifies whether the DSR (data-set-ready) signal is monitored for
output flow control. If this member is TRUE and DSR is turned off,
output is suspended until DSR is sent again.
DSR is a input pin. You can read its state from MSR register. It
usually connect to DTR pin in the other end.
It is often used for hardware flow control to indicate that the other
end is working(active, ready, wait for data...)
You can change this value when the comm is open.
DSR Input on | the other end is ready, and wait for your data
------------+----------------------------------------------------
Input off | the other end is not ready, we cannot send data out
DtrControl : ( DtrEnable, DtrDiable, DtrHandshake )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -