Puerto Serial

Post Reply
User avatar
Vikthor
Posts: 271
Joined: Fri Oct 07, 2005 5:20 am
Location: México

Puerto Serial

Post by Vikthor »

Hola amigos :

Alguien me podría mostrar un ejemplo de envío de datos al puerto serial de una Pocket ?

Saludos y gracias por adelantado.
Vikthor
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
luis.santeliz
Posts: 47
Joined: Mon May 29, 2006 9:04 pm

Post by luis.santeliz »

Yo tengo por aqui un ejemplito que podria servirte, lo utilize para imprimir en una lx300 epson con un adaptador bluetooth, es uno de los q estan en la carpeta sample q modifique, incluye las instrucciones para configurar el puerto...

Code: Select all

// BlueTooth use sample

#include "FWCE.ch"

#define GENERIC_READ                     0x80000000
#define GENERIC_WRITE                    0x40000000
#define GENERIC_REWRITE 0xC0000000
#define OPEN_EXISTING   3
#define FILE_ATTRIBUTE_NORMAL 0x00000080

function Main()


   local hCom:=NIL
   local aCom:={"COM1:","COM2:","COM3:","COM4:","COM5:","COM6:","COM7:","COM8:"}

   PUBLIC cCom:=aCom[8]

   PUBLIC oWnd


 //  local hIn := CreateFile( "COM3:",; // change the number as needed
 //                           GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL )

   DEFINE WINDOW oWnd TITLE "Comunicaciones COMx"


#define nSEP 5
#define nMARGEN_DER 10
#define nANCHO 20
#define oBCONECT oWnd:aControls[1]
#define oCPUERTO oWnd:aControls[2]
#define oBIMPRIM oWnd:aControls[3]
#define oBSALIR  oWnd:aControls[4]

   @ 10, nMARGEN_DER BUTTON "Conectar" SIZE 80, 20 PIXEL OF oWnd

   oBCONECT:bAction:={|| hCom:=Conectar(cCOM) }
 //  oBCONECT:bWhen:={|| hCom==NIL }


   @ oBCONECT:nTop, oBCONECT:nRight+nSEP COMBOBOX cCom ITEMS aCom SIZE 70,nANCHO PIXEL OF oWnd

   @ oBCONECT:nBottom+nSEP, nMARGEN_DER BUTTON "Imprimir" ACTION Imprime(hCom) SIZE 80, 20 PIXEL OF oWnd

   oBIMPRIM:bWhen:={|| hCom<>NIL }

   @ oBIMPRIM:nBottom+nSEP , nMARGEN_DER BUTTON "Salir" SIZE 80, 20 PIXEL OF oWnd

   oBSALIR:bAction:={|| CloseHandle(hCom), oWnd:End() }

//   ACTION MsgInfo(ReadText(hCom)) SIZE 80, 20

   ACTIVATE WINDOW oWnd

   CloseHandle( hCom )
//   CloseHandle( hIn )

return nil


FUNCTION Conectar(cPTO)
local hCom
local err

	IF cPTO=NIL
		RETURN .F.
	ENDIF

	SetWindowText(oBCONECT:hWnd,"Conectando "+cPTO) // Cambia el caption del boton para saber que se esta conectando

	hCom := CreateFile( cPTO, nor( GENERIC_WRITE, GENERIC_READ ), 0,   0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ) // aqui crea la conexion con el puerto

	SetComm(hCom, 9600, .F., 8, 1) // Configura el puerto
	
   	err:=GetLastError()
	IF err=-1
		MsgInfo(err,"Error!")
	ELSE
		SetWindowText(oBCONECT:hWnd,"Desconectar")
		oBCONECT:bAction:={||CloseHandle(hCom), hCom:=NIL, SetWindowText(oBCONECT:hWnd,"Conectar"), oBCONECT:bAction:={|| hCom:=Conectar(cCOM)} }

	ENDIF

RETURN hCom

#DEFINE CRLF Chr(13)+Chr(10)

// 
// ESC/P CODES 

#DEFINE ESC       Chr(27)
#DEFINE EXPAND    Chr(14)
#DEFINE _EXPAND   Chr(20)
#DEFINE NEGR      Chr(15)
#DEFINE _NEGR     Chr(18)
#DEFINE SANSSER   ESC+"k1"
#DEFINE ROMAN     ESC+"k0"
#DEFINE DRAFT     ESC+"x0"
#DEFINE FORMFEED  Chr(12)
#DEFINE ULTIMAPAG Chr(26)


FUNCTION Imprime(hCom)

 //   SendText(hCom,ESC+"iS") // Inicialización Obligatoria
 //  MsgInfo(ReadText(hCom,32)) // Recibe información de statu

   SendText(hCom,ESC+"@")
//   SendText(hCom,ESC+"ia"+Chr(0))
//   SendText(hCom,ESC+"k1")
   SendText(hCom,EXPAND+"LA EMPRESA MODELO"+_EXPAND)
   SendText(hCom,SANSSER+"esto es una prueba SANSERIF")
   SendText(hCom,ROMAN+"esto es una prueba ROMAN")
   SendText(hCom,DRAFT+"esto es una prueba DRAFT")

   SendText(hCom,ULTIMAPAG)

RETURN

function SendText( hOut, cText, lCr )

   local n

   DEFAULT lCr:=.T.   



   for n = 1 to Len( cText )
      WriteByte( hOut, Asc( SubStr( cText, n, 1 ) ) )
   next

   IF lCr
	WriteByte( hOut, 13 )
	WriteByte( hOut, 10 )
   ENDIF   

return nil

FUNCTION ReadText(hSer,nBytes)
LOCAL nChr, cTxt:=""
DEFAULT nBytes:=32

WHILE nChr != 236 .AND. nChr != 10
        nChr = ( READBYTE( hSer ) )
        cTxt += ALLTRIM(STR( nChr,3,0 ))+" "
   IF (nBytes--)=0
      exit
   ENDIF
ENDDO

RETURN cTxt

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

LPSTR WideToAnsi( LPWSTR );

HB_FUNC( GETWINDOWTEXT )
{
   HWND hWnd = ( HWND ) hb_parnl( 1 );
   int iLen = GetWindowTextLength( hWnd );
   LPWSTR pText = ( LPWSTR ) hb_xgrab( ( iLen * 2 ) + 2 );
   LPSTR pAnsi;

   GetWindowText( hWnd, pText, iLen + 1 );

   pAnsi = WideToAnsi( pText );
   hb_retc( pAnsi );
   hb_xfree( pAnsi );
   hb_xfree( pText );
}

/* 
   Configurar el puerto COMx: 

       SetComm(hCom, nBaudRate, nParidad, nDataBits, lStopBits) 
   ej: SetComm(hCom, 9600, .F., 9, 1)

*/


HB_FUNC( SETCOMM )
{
   DCB dcb;
   
   GetCommState( ( HANDLE ) hb_parnl( 1 ), &dcb );
   
   dcb.BaudRate = hb_parnl( 2 );
   dcb.Parity = hb_parl( 3 );
   dcb.ByteSize = hb_parnl( 4 );
   dcb.StopBits = hb_parnl( 5 );
   
   hb_retl( SetCommState( ( HANDLE ) hb_parnl( 1 ), &dcb ) );
}


HB_FUNC( MGETREPLACE ){}
HB_FUNC( GETWINTXTLENGHT ){}
HB_FUNC( MGETLINE ){}

#pragma ENDDUMP


Luis Santeliz
Móvil SoftWare, C.A.
---------------------------------------------
MySQL DBA,
MySQL Server Performance Tunning,
DPxBase, FiveWin, Java,
Linux Redhat, Windows

Caracas Venezuela
luissanteliz@cantv.net
Post Reply