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

如何撰寫Console Application?

尚未結案
wb3599
一般會員


發表:32
回覆:32
積分:12
註冊:2004-02-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-02-23 02:34:06 IP:61.230.xxx.xxx 未訂閱
請問: 我想撰寫在Linux的Console模式下的程式. 這個程式有點像Windows NT的Service一樣, 就是在背景一直執行, 程式的用途為將Com port所收到的資料做進一步的處理. 問題是, 我如何讓程式一直執行而不要結束呢? 以下分別是是我的程式碼(test與main): ============================================= program test; {$APPTYPE CONSOLE} uses SysUtils, main in 'main.pas'; var xx : Txx; begin { TODO -oUser -cConsole Main : Insert code here } xx := Txx.Create; xx.Createnrcomm; while xx.nrComm1.Active do begin Sleep(1000); end; end. ====================================================== unit main; interface uses SysUtils, Windows, Messages, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, nrclasses, nrcomm; type Txx = class(TObject) nrComm1: TnrComm; procedure nrComm1AfterReceive(Com: TObject; Buffer: Pointer; Received: Cardinal); procedure Createnrcomm; private { Private declarations } public { Public declarations } end; var nrComm1: TnrComm; implementation { Txx } procedure Txx.Createnrcomm; begin nrComm1 := TnrComm.Create(Application); nrComm1.BaudRate := 2400; nrComm1.Active := True; nrComm1.OnAfterReceive := nrComm1AfterReceive; end; procedure Txx.nrComm1AfterReceive(Com: TObject; Buffer: Pointer; Received: Cardinal); var i, iBalls: Integer; ch : Char; x : String; y : Integer; Buff : String; begin for i:= 0 to Received - 1 do begin ch := PChar(Buffer)[i]; if ch = #5 then begin nrcomm1.SendChar(#6); end; Buff := Buff IntToStr(Ord(ch)); end; WriteLn(Buff); end; end. 在建立好我的Com port之後(xx.Createnrcomm;), 程式就結束了, 所以根本沒機會去等待Com port上的資料來處理. 於是我才加了一個whilw的迴圈, 可是, 這個迴圈幾乎把程式的控制權佔住了, 我在com元件上設的事件之程式碼, 根本也沒機會去執行. 我有試著參考一般IDE的Project程式碼, 在最後加入: Application.Run; 這辦法行不通, 因為好像Console Application不認得Application這個變數. 請問大家, 我該怎麼做呢?
系統時間:2024-04-24 1:32:42
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!