控制台程序 控件 事件处理问题(200分)

  • 主题发起人 主题发起人 liyijincom
  • 开始时间 开始时间
L

liyijincom

Unregistered / Unconfirmed
GUEST, unregistred user!
做一个控制台通讯程序,用到timer及socket
程序如下:(事件不会触发,请高人指点)

program pDosCom;
{$APPTYPE CONSOLE}
uses
Forms,SysUtils,SPComm,extctrls,scktcomp,inifiles;
type
TMyobject=class(Tobject)
Timer1: TTimer;
comm1:Tcomm;
ClientSocket: TClientSocket;
procedure ontimer(Sender: TObject);
procedure ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
procedure onConnect(Sender: TObject;Socket: TCustomWinSocket);
procedure onDisconnect(Sender: TObject;Socket: TCustomWinSocket);
procedure onError(Sender: TObject; Socket: TCustomWinSocket;ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure onRead(Sender: TObject; Socket: TCustomWinSocket);
end;

var
myobject:TMyobject;
rbuf:array[1..64] of byte;
DisSocket:integer;
DisAlert:integer;
ExePath:String;
NetServer:String;
CanExit:Boolean;
function ItemFromIni(const FileName:String;const MainTain:String;const ItemName:String):String;
var
Str:String;
IniFile: TIniFile;
begin
try
if FileExists(FileName) then
begin
//从.INI文件中读取数据
IniFile:=TIniFile.Create(FileName);
Str:=IniFile.ReadString(MainTain,ItemName,'');
IniFile.Free;
end;
result:=Str;
except
result:=Str;
end;
end;

procedure TMyobject.ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
//接收数据的处理过程
var
i,j:integer;
GetStr:String;
// oStr:String;
begin
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
if (rbuf=$02) and (rbuf[i+1]=$01) and (rbuf[i+11]=$03) then
begin
GetStr:='COM;';
for j:=1 to 8 do
begin
if ((rbuf[i+1+j]>=48) and (rbuf[i+1+j]<=57)) then
GetStr:=GetStr+inttostr(rbuf[i+1+j]-48)
else
GetStr:=GetStr+'9';
if j=8 then
begin
ClientSocket.Socket.SendText(GetStr+'-');
end;
end;
end;
end;
procedure TMyobject.onConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln(' connect to');
end;

procedure TMyobject.onDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
DisSocket:=0;
writeln(' disconnect');
end;

procedure TMyobject.onError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode:=0;
DisSocket:=0;//Socket连接上为1,没连接上为0
writeln(' net error');
end;

procedure TMyobject.onRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln('get:'+socket.ReceiveText);
end;

procedure TMyobject.onTimer(Sender: TObject);
begin
myobject.Timer1.Enabled:=false;
writeln('isAlert');
ClientSocket.Socket.SendText('isAlert');
if DisSocket=0 then
begin
ClientSocket.Active := True;
DisSocket:=1;
end;
if DisAlert=0 then
try
Comm1.StartComm;
DisAlert:=1;
ClientSocket.Socket.SendText('isAlert');
except
DisAlert:=0;
// ClientSocket.Socket.SendText('DisAlert');
end;
myobject.Timer1.Enabled:=true;
end;


begin
Application.Initialize;
Writeln('begin my proc ');
DisSocket:=0; //Socket连接上为1,没连接上为0
DisAlert:=0; //com口连接
ExePath:=ExtractFilePath(Application.ExeName);
NetServer:=ItemFromIni(ExePath+'maincomm.dat','NETSERVER','IP');
myobject:=tmyobject.Create();
myobject.timer1:=Ttimer.Create(nil);
with myobject.timer1 do
begin
Interval :=1000;
OnTimer :=myobject.ontimer;
Enabled:=true;
Writeln(' begin create timer ');
end;
myobject.ClientSocket:=TClientSocket.Create(nil);
if Length(NetServer) > 0 then
with myobject.ClientSocket do
begin
Port:=50001;
Address := NetServer;
ClientType:=ctNonBlocking;
OnConnect:=myobject.onConnect;
OnDisconnect:=myobject.onDisconnect;
OnError:=myobject.onError;
OnRead:=myobject.onRead;
Active := True;
// open;
Socket.SendText('test');
Writeln(' begin create clientsocket ');
end;
myobject.comm1:=Tcomm.Create(nil);
with myobject.comm1 do
begin
BaudRate:=9600;
ByteSize:=_8;
StopBits:=_1;
CommName:='COM2';
OnReceiveData:=myobject.ReceiveData;
Writeln(' begin create tcomm ');
end;
try
myobject.Comm1.StartComm;
DisAlert:=1;
except
DisAlert:=0;
myobject.ClientSocket.Socket.SendText('DisAlert');
end;
Application.Run;
CanExit:=False;
writeln('wait for input string:');
while (not CanExit) do
begin
{ readln(IStr);
IStr:='COM;'+IStr+'-';
myobject.ClientSocket.Socket.SendText(IStr);
writeln(IStr);}
end;
end.
 
请多多指点!!!,如果觉得分不够可以再加
 
源码太多了,说实在的不出意外的话没有人理你!你能编出,你下点心思!书上有很我地方
要用到SOCKET;如《DELPHI功能解析》电子工业出版社!不好意思没帮上忙!
 
你讲得有道理,事情有点急
program pDosCom;
{$APPTYPE CONSOLE}
uses
Forms,SysUtils,SPComm,extctrls,scktcomp,inifiles;
type
TMyobject=class(Tobject)
Timer1: TTimer;
ClientSocket: TClientSocket;
procedure ontimer(Sender: TObject);
procedure onRead(Sender: TObject; Socket: TCustomWinSocket);
end;

var myobject:TMyobject;

procedure TMyobject.onRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
writeln('get:'+socket.ReceiveText);
end;

procedure TMyobject.onTimer(Sender: TObject);
begin
myobject.Timer1.Enabled:=false;
writeln('isAlert');
ClientSocket.Socket.SendText('isAlert');
myobject.Timer1.Enabled:=true;
end;


begin
Application.Initialize;
ExePath:=ExtractFilePath(Application.ExeName);
NetServer:=ItemFromIni(ExePath+'maincomm.dat','NETSERVER','IP');
myobject:=tmyobject.Create();
myobject.timer1:=Ttimer.Create(nil);
with myobject.timer1 do
begin
Interval :=1000;
OnTimer :=myobject.ontimer;
Enabled:=true;
end;
myobject.ClientSocket:=TClientSocket.Create(nil);
if Length(NetServer) > 0 then
with myobject.ClientSocket do
begin
Port:=50001;
Address := NetServer;
ClientType:=ctNonBlocking;
OnRead:=myobject.onRead;
Active := True;
end;
Application.Run;
CanExit:=False;
while (not CanExit) do
end.
 
Windows消息是通过窗口传递的.
最简单的方法时建一个不显示的窗口,在窗口上放一个TTimer然后处理.
 
就是关于如果在控制台程序中
让控件的事件可以正常运作
上例中的事件都不触发(timer,clientsocket)
请有这方面的经验的朋友,指点
 
想最终转换为DOS程序,所以不可以有窗口
 
你的这些事件均由Windows消息而来,没有窗口就收不到消息,哪里来事件?
 
呵呵~程序没有仔细看。但是lld的说法可能不准确,刚从《程序员》上看到没有窗口的
CONSOLE project也是可以接收消息的。
 
问题已经解决
 
请问你是怎么解决的,能不能告诉我。
我的电子邮件bobzjh@hzcnc.com
 
后退
顶部