線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:8780
推到 Plurk!
推到 Facebook!

Spcomm簡易用法示範(新手適用)

 
juneo
高階會員


發表:103
回覆:190
積分:118
註冊:2004-05-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-22 15:57:34 IP:211.75.xxx.xxx 未訂閱
找了很多文章也看了很多文章,由於我是新手所以在spcomm元件的用法上完全不了解,今天終於弄懂了,很高興的機器也有正確的回應(自動控制),這是最簡單的用法,給跟我一樣式新手的人參考 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SPComm; type TForm1 = class(TForm) Comm1: TComm; Edit1: TEdit; Button2: TButton; Memo1: TMemo; Label1: TLabel; Label2: TLabel; Label3: TLabel; procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin memo1.Clear; //清除螢幕 Comm1.startcomm; //啟動通訊 end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin comm1.StopComm; //結束程式也停止通訊 end; procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var S:string; begin SetLength(S,BufferLength); //接收RS232的數據並顯示Memo1上。 Move(Buffer^, PChar(S)^, BufferLength); Memo1.Lines.Add(S); Memo1.Invalidate; end; procedure TForm1.Button2Click(Sender: TObject); var s:string; begin s:=Edit1.Text; //要送出的字元 Comm1.WriteCommData(PChar(s),Length(s)); //送出指令給遠端 end; //程式目的測試spcomm //程式摘要 以最簡單的程式展現出spcomm的用法 end. 整各程式重點在 SetLength(S,BufferLength); //接收RS232的數據並顯示Memo1上。 Comm1.WriteCommData(PChar(s),Length(s)); //送出指令給遠端 這兩行程式應該算以最簡單的方式運作spcomm 希望對新手有幫助 接下來我要將程式轉成Dll到時後在跟大家分享 分享比獲得更快樂
juneo
高階會員


發表:103
回覆:190
積分:118
註冊:2004-05-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-22 16:03:58 IP:211.75.xxx.xxx 未訂閱
忘記說明如何測試程式 以下是RS-232單機的測試方式 請把RS-232 Port 的 2 3 接角連接在一起,也就是短路 這樣你送出字串的時候memo1上面就會顯示出你的字串 這樣程式就ok的 RS-232 第二接腳 功能是寫 RS-232 第三接腳 功能是讀 所以短路後剛好寫給自己讀 ^^ PS:我是拿一條線直接從電腦上短路起來,就可以測試程式有沒有問題了 分享比獲得更快樂
spaiftghotmail
一般會員


發表:0
回覆:1
積分:0
註冊:2005-03-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-21 17:38:18 IP:211.76.xxx.xxx 未訂閱
安安 大大.. 可否再請教幾各問題呢 1.使用showmessage(inttostr(comm1.GetModemState)); 得到的是一組數字..如何對應到她的狀態呢?_? 代碼48 2. showmessage(inttostr(eventmask)); 代碼0 和 16 3.您最後有提到的短路測試(把port2&3連在一起)..要如何連呢 Regards PS:W2000 Delphi5....
juneo
高階會員


發表:103
回覆:190
積分:118
註冊:2004-05-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-22 09:03:24 IP:211.75.xxx.xxx 未訂閱
function TComm.GetModemState : DWORD; var    dwModemState : DWORD; begin      if not GetCommModemStatus( hCommFile, dwModemState ) then         Result := 0      else          Result := dwModemState end;    //********************************* GetModemState Call WinAPI GetCommModemStatus Result hCommFile, dwModemState     WinApi-- The GetCommModemStatus function retrieves modem control-register values.     BOOL GetCommModemStatus(        HANDLE hFile,        // handle of communications device       LPDWORD lpModemStat        // address of control-register values    );              Parameters    hFile    Identifies the communications device. The CreateFile function returns this handle.     lpModemStat    Points to a 32-bit variable that specifies the current state of the modem control-register values. This parameter can be a combination of the following values:     Value        Meaning MS_CTS_ON        The CTS (clear-to-send) signal is on. MS_DSR_ON        The DSR (data-set-ready) signal is on. MS_RING_ON        The ring indicator signal is on. MS_RLSD_ON        The RLSD (receive-line-signal-detect) signal is on.      Return Values    If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.     Remarks    The GetCommModemStatus function is useful when you are using the WaitCommEvent function to monitor the CTS, RLSD, DSR, or ring indicator signals. To detect when these signals change state, use WaitCommEvent and then use GetCommModemStatus to determine the state after a change occurs.  The function fails if the hardware does not support the control-register values.     See Also    CreateFile, WaitCommEvent     如果還不清楚 你可以上網Google 找GetModemState 或 GetCommModemStatus http://www.delphibbs.com/delphibbs/dispq.asp?lid=2020133 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=36692 http://www.omnicomtech.com/download/bin/oleapi.html    3.如圖 你也可以用單心線短路^^ 數學+程式+經驗=不懂 發表人 - juneo 於 2005/03/22 09:06:24
系統時間:2024-04-26 7:05:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!