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.
程序如下:(事件不会触发,请高人指点)
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.