SOCKET - Teclado WILBOR - RESOLVIDO
Posted: Thu Apr 24, 2008 2:33 pm
Amigos, estou tentando comunicar com um teclado WILBOR via socket, consegui me conectar ao mesmo usando o exemplo abaixo, agora preciso interagir com o mesmo, como devo fazer para escrever na porta ou receber os dados digitados no teclado?
// Socket server connection sample
#include "FiveWin.ch"
#define ST_COMMAND 1
#define ST_SENDFILE 2
#define FILE_BLOCK 8000
static oWnd, oSocket, oClient
//------------------------------------------------------------------------//
function Main()
local oBar
DEFINE WINDOW oWnd TITLE "Server socket"
DEFINE BUTTONBAR oBar OF oWnd _3D
DEFINE BUTTON OF oBar ACTION (Server()) TOOLTIP "Listen"
ACTIVATE WINDOW oWnd
return nil
//------------------------------------------------------------------------//
function Server()
oSocket = TSocket():New( 1001 )
oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
oClient:Cargo := ST_COMMAND,;
oClient:bRead := { | oSocket | OnRead( oSocket ) },;
oClient:bClose := { | oSocket | OnClose( oSocket ) }}
oSocket:OnWrite("TESTE")
oSocket:Listen()
return nil
//------------------------------------------------------------------------//
function OnRead( oSocket )
local cData := oSocket:GetData()
local cToken
LogFile( "sockserv.txt", { Len( cData ), cData } )
do case
case oSocket:Cargo == ST_COMMAND
cToken = StrToken( cData, 1 )
do case
case cToken == "SENDFILE"
oSocket:Cargo = ST_SENDFILE
oSocket:hFile = fcreate( StrToken( cData, 2 ) )
case cToken == "MSG"
MsgInfo( SubStr( cData, 5 ) )
endcase
case oSocket:Cargo == ST_SENDFILE
fwrite( oSocket:hFile, cData, Len( cData ) )
LogFile( "sockserv.txt", { "writting..." } )
if Len( cData ) < FILE_BLOCK
// fclose( oSocket:hFile )
// MsgInfo( Len( cData ) )
// oSocket:Cargo = ST_COMMAND
endif
endcase
return nil
//------------------------------------------------------------------------//
function OnClose( oSocket )
MsgInfo( "Client has closed!" )
do case
case oSocket:Cargo == ST_SENDFILE
fclose( oSocket:hFile )
endcase
oSocket:End()
return nil
// Socket server connection sample
#include "FiveWin.ch"
#define ST_COMMAND 1
#define ST_SENDFILE 2
#define FILE_BLOCK 8000
static oWnd, oSocket, oClient
//------------------------------------------------------------------------//
function Main()
local oBar
DEFINE WINDOW oWnd TITLE "Server socket"
DEFINE BUTTONBAR oBar OF oWnd _3D
DEFINE BUTTON OF oBar ACTION (Server()) TOOLTIP "Listen"
ACTIVATE WINDOW oWnd
return nil
//------------------------------------------------------------------------//
function Server()
oSocket = TSocket():New( 1001 )
oSocket:bAccept = { | oSocket | oClient := TSocket():Accept( oSocket:nSocket ),;
oClient:Cargo := ST_COMMAND,;
oClient:bRead := { | oSocket | OnRead( oSocket ) },;
oClient:bClose := { | oSocket | OnClose( oSocket ) }}
oSocket:OnWrite("TESTE")
oSocket:Listen()
return nil
//------------------------------------------------------------------------//
function OnRead( oSocket )
local cData := oSocket:GetData()
local cToken
LogFile( "sockserv.txt", { Len( cData ), cData } )
do case
case oSocket:Cargo == ST_COMMAND
cToken = StrToken( cData, 1 )
do case
case cToken == "SENDFILE"
oSocket:Cargo = ST_SENDFILE
oSocket:hFile = fcreate( StrToken( cData, 2 ) )
case cToken == "MSG"
MsgInfo( SubStr( cData, 5 ) )
endcase
case oSocket:Cargo == ST_SENDFILE
fwrite( oSocket:hFile, cData, Len( cData ) )
LogFile( "sockserv.txt", { "writting..." } )
if Len( cData ) < FILE_BLOCK
// fclose( oSocket:hFile )
// MsgInfo( Len( cData ) )
// oSocket:Cargo = ST_COMMAND
endif
endcase
return nil
//------------------------------------------------------------------------//
function OnClose( oSocket )
MsgInfo( "Client has closed!" )
do case
case oSocket:Cargo == ST_SENDFILE
fclose( oSocket:hFile )
endcase
oSocket:End()
return nil