?? 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 + -