/////////////////////////////////////////////
//
// Funciones de comunicacion por Rs232
//
////////////////////////////////////////////
#include "Fwce.ch"
#define BUFF_IN 1024
#define BUFF_OUT 1024
#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
#define ONESTOPBIT 0
#define ONE5STOPBITS 1
#define TWOSTOPBITS 2
///////////////////////////////////////////////
//
// OpenCom
// Abre el puerto especificado y lo configura
//////////////////////////////////////////////
Function OpenCom( nCom, nBauds, nParity, nDataByt, nStopBits, lIRmode )
LOCAL hComm, cComm
DEFAULT nCom := 1, nBauds := 2400, nParity := NOPARITY, nDataByt := 8, lIRmode := .f.
DEFAULT nStopBits := ONESTOPBIT
cComm := "Com"+ AllTrim( Str( nCom, 2 ) )+ ":"
hComm := CreateFile( cComm, nOr(GENERIC_WRITE, GENERIC_READ) , 0, 0, OPEN_EXISTING, 0,0 )
IF hComm ==-1
Msginfo("Fallo al abrir "+ cComm)
Return -1
ENDIF
IF lIRMode
IF !RAWIRON( hComm )
Msginfo( "Fallo al iniciar RAWIR")
ComClose( hComm )
RETURN -1
ENDIF
ENDIF
IF !SetComm( hComm, nBauds, nParity, nDataByt, nStopBits )
MsgInfo("Fallo al configurar "+ cComm)
ComClose( hComm )
RETURN -1
ENDIF
RETURN hComm
///////////////////////////////////
//
// ComSend( nHdlCom, cString )
//
////////////////////////////////////////////
Function ComSend( hComm, cString )
LOCAL nBytesend, nBytes:= Len( cString )
nByteSend := WriteComm( hComm, @cString, nBytes )
Return ( nByteSend == nBytes )
////////////////////////////////////
//
// ComRead( nHdlCom, nChars )
//
////////////////////////////////////////////
Function ComRead( nHdlCom, nChars )
LOCAL nBytes, cBuffIn := Space(BUFF_IN)
nBytes := ReadComm( nHdlCom, @cBuffIn, nChars )
RETURN if( nBytes >0, Left( cBuffIn, nBytes ),"" )
////////////////////////////////////
//
// ComClose( nHdlCom)
//
////////////////////////////////////////////
FUNCTION ComClose( hComm )
RAWIROFF( hComm )
CloseHandle( hComm )
RETURN .t.
#pragma BEGINDUMP
#include <windows.h>
#include <winbase.h>
#include <hbapi.h>
#include <aygshell.h>
HB_FUNC( SETCOMM )
{
DCB dcb;
COMMTIMEOUTS timeouts;
SetupComm(( HANDLE) hb_parnl( 1 ), BUFF_IN, BUFF_OUT ) ;
GetCommState( ( HANDLE ) hb_parnl( 1 ), &dcb );
dcb.BaudRate = hb_parnl( 2 );
dcb.Parity = hb_parni( 3 );
dcb.ByteSize = hb_parni( 4 );
dcb.StopBits = hb_parni( 5 );
dcb.fBinary = TRUE;
dcb.fParity = TRUE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
GetCommTimeouts(( HANDLE) hb_parnl, &timeouts);
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(( HANDLE) hb_parnl( 1 ), &timeouts );
hb_retl(SetCommState( ( HANDLE ) hb_parnl( 1 ), &dcb ));
}
HB_FUNC( READCOMM )
{
DWORD dw = 0;
ReadFile( ( HANDLE ) hb_parnl( 1 ), ( LPVOID ) hb_parc( 2 ), ( DWORD ) hb_parni( 3 ), &dw, NULL );
hb_retni( ( int ) dw ? dw : -1 );
}
HB_FUNC( WRITECOMM )
{
DWORD dw = 0;
WriteFile( (HANDLE ) hb_parnl( 1 ), ( LPVOID ) hb_parc( 2 ), ( DWORD ) hb_parni( 3 ), &dw, NULL );
hb_retni( ( int ) dw ? dw : -1);
}
HB_FUNC( RAWIRON )
{
hb_retl( EscapeCommFunction( ( HANDLE ) hb_parnl( 1 ), SETIR ) );
}
HB_FUNC( RAWIROFF )
{
hb_retl( EscapeCommFunction( ( HANDLE ) hb_parnl( 1 ), CLRIR ) );
}
#pragma ENDDUMP