Spcomm簡易用法示範(新手適用) |
|
juneo
高階會員 ![]() ![]() ![]() ![]() 發表:103 回覆:190 積分:118 註冊:2004-05-13 發送簡訊給我 |
找了很多文章也看了很多文章,由於我是新手所以在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 發送簡訊給我 |
|
spaiftghotmail
一般會員 ![]() ![]() 發表:0 回覆:1 積分:0 註冊:2005-03-21 發送簡訊給我 |
|
juneo
高階會員 ![]() ![]() ![]() ![]() 發表:103 回覆:190 積分:118 註冊:2004-05-13 發送簡訊給我 |
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.如圖
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |