Como hacer una impresion al puerto serial ?

Post Reply
Erick Almanza
Posts: 75
Joined: Mon Sep 11, 2006 3:52 pm

Como hacer una impresion al puerto serial ?

Post by Erick Almanza »

Hola amigos del foro

Como puedo imprimir, utilizando la clase TDOSPRN directamente, al puerto serial. Aunque la clase contempla este puerto no funciona

Es perfecta para redes o puertos paralelos



Saludos,


Erick
User avatar
pablovidal
Posts: 398
Joined: Thu Oct 06, 2005 10:15 pm
Location: Republica Dominicana
Contact:

Re: Como hacer una impresion al puerto serial ?

Post by pablovidal »

Es simple:

Code: Select all

oPrn := TDosPrn():New( 03, "COM1" )
Saludos,

Pablo Alberto Vidal
/*
------------------------------------------------------
Harbour 3.2.0, Fivewin 17.02, BCC7
------------------------------------------------------
*/
Erick Almanza
Posts: 75
Joined: Mon Sep 11, 2006 3:52 pm

Re: Como hacer una impresion al puerto serial ?

Post by Erick Almanza »

pablovidal wrote:Es simple:

Code: Select all

oPrn := TDosPrn():New( 03, "COM1" )
Hola Pablo

Esto no me funciono, al contrario me envio un error de string

quizas tienes algo modificado en tu classe que no tengo

METHOD New( cPort, lFile, lLaser, nLPP, bNewPage ) CLASS TDosPrn

Local aPorts := { { "LPT1", 888 }, ; // 378h
{ "LPT2", 632 }, ; // 278h
{ "COM1", 1016 }, ; // 3f8h
{ "COM2", 760 } } // 2f8h
Local nPos

DEFAULT cPort := "LPT1" ,;
lFile := .T. ,; // not used; kept here for compatibility
lLaser := .F. ,;
nLPP := 66 --------------------------------------> AQUI ME MARCO EL ERROR
cPort := Upper( cPort )

If lLaser
::cCompress := ::Esc2Cmd( iL_COMPRESS )
::cNormal := iL_NORMAL
::cReset := ::Esc2Cmd( iL_RESET )
Else
::cCompress := iM_COMPRESS
::cNormal := iM_NORMAL
::cReset := ::Esc2Cmd( iM_RESET )
Endif
::cFormFeed := iEJECT
::cBuffer := ""
::nLeftMargin := 0
::nTopMargin := 0
::nRow := 0
::nCol := 0
::lAnsiToOem := .T.
::cPort := cPort + If( ! "." $ cPort .and. ! "\\" $ cPort, ".PRN", "" ) // considers the port can be a network route (\\computername\sharename)
::hDC := -1 // FCreate( ::cPort ) moved down, after port checking is done
::LastError := 0
::lFile := ! ( Left( cPort, 3 ) == "LPT" .or. Left( cPort, 3 ) == "COM" .or. Left( cPort, 3 ) == "PRN" )
::nLPP := nLPP
::bNewPage := bNewPage
::nPage := 1
::nLPTBase := 0

If ! ::lFile
nPos := Max( 1, AScan( aPorts, {|aSub| aSub[1] == cPort } ) ) // LPT1 or PRN considered default
::nLPTBase := aPorts[nPos,2]
::PrinterStatus()
Endif

If ::lFile .or. ( ! ::lPrnBusy .and. ! ::lPaperOut ) /*; // reliable with matrix, not laser printers
If( cPort == "LPT1", IsPrinter(), ! ::lPaperOut )*/
::hDC := FCreate( ::cPort ) // TIP: make sure port name (\sharename part) is 8 chars or less, or you'll get a DOS error 5 (Access denied) if printing across the network
If ::hDC < 0
::LastError := FError()
Endif
Else
If ::lPrnBusy .and. ::lPaperOut .and. ! ::lPrnSelect
::LastError := 28 // "Printer out of paper"
Elseif ::lPrnBusy .and. ! ::lPaperOut
::LastError := 29 // "Write fault" (Printer off line or not powered on)
Else
::LastError := 6 // "Invalid handle"
Endif
Endif

Return Self




Saludos,

Erick
User avatar
pablovidal
Posts: 398
Joined: Thu Oct 06, 2005 10:15 pm
Location: Republica Dominicana
Contact:

Re: Como hacer una impresion al puerto serial ?

Post by pablovidal »

Code: Select all

#include "FiveWin.ch"

#define CR Chr( 13 )

#define cPRNCMD_FF                  "12"
#define cPRNCMD_LASERJET_10CPI      "27,40,115,49,48,72"
#define cPRNCMD_LASERJET_12CPI      "27,40,115,49,50,72"
#define cPRNCMD_LASERJET_18CPI      "27,40,115,49,56,72"
#define cPRNCMD_INKJET_DOUBLE       "27,33,32"
#define cPRNCMD_INKJET_NORMAL       "27,33,00"
#define cPRNCMD_INKJET_COMPRESS     "27,33,04"
#define cPRNCMD_MATRIX_DOUBLE       "14"
#define cPRNCMD_MATRIX_NORMAL       "18"
#define cPRNCMD_MATRIX_COMPRESS     "15"


CLASS TDosPrn

   DATA acDouble, acNormal, acCompress    AS ARRAY
   DATA lAnsiToOem, lAutoFF, lEditFile    AS LOGICAL
   DATA LastError, hDC, nPosFooter, nMAXCOL, nRow, nCol, nLeftMargin, nTopMargin, nPrintType, hEchoFile
   DATA cPrintType, cPort, cEchoFile, cBuffer, cFormFeed, cDouble, cNormal, cCompress, cHeader, cFooter, cName, cDriver

   DATA lNoPrint INIT .F. //.t. No Imprime - > .F. Imprime
   Data lPrintWin Init .F. // Pablo Vidal
   Data cFileTemp Init ""

   METHOD New( nPrintType, cPort, cEchoFile )  CONSTRUCTOR
   METHOD End()

   METHOD StartPage()  VIRTUAL
   METHOD EndPage()

   METHOD Command( cStr1, cStr2, cStr3, cStr4, cStr5, cStr6, cStr7, cStr8, cStr9 )

   METHOD SetCoors( nRow, nCol )

   METHOD NewLine()  INLINE ( ::cBuffer += CRLF,   ;
                              ::nRow++         ,   ; //LF
                              ::nCol := 0 )          //CR

   METHOD Write( cText, lAToO ) ;
            INLINE ( IF( lAtoO == NIL, lAtoO := .T., ),  ;
                     ::cBuffer +=  If( lAtoO, AnsiToOem( cText ), cText ),  ;
                     ::nCol    +=  Len( cText ) )

   METHOD SayDbl( nRow, nCol, cText, lAToO )

   METHOD    Say( nRow, nCol, cText, lAToO )

   METHOD SayCmp( nRow, nCol, cText, lAToO )

ENDCLASS


METHOD New( nPrintType, cPort, cEchoFile ) CLASS TDosPrn

   local l1, lOKType := .t.

   ::nPrintType := 0
   ::cPrintType := ""

   IF     ValType( nPrintType ) != "N" //si en primer parámetro es NIL o no es un nº
      lOKType := .F.
   ELSEIF nPrintType = 1
      ::nPrintType := 1
      ::cPrintType := "LASERJET"
   ELSEIF nPrintType = 2
      ::nPrintType := 2
      ::cPrintType := "INKJET"
   ELSEIF nPrintType = 3
      ::nPrintType := 3
      ::cPrintType := "MATRIX"
   ELSE
      lOKType := .F.
   END

   If .not. lOKType
      MsgStop( 'El parámetro  nPrintType  ha de valer 1 ó 2 ó 3 .-  "' + CR + ;
               '1.- Impresora láser' + CR +                                   ;
               '2.- Impresora de chorro de tinta' + CR +                      ;
               '3.- Impresora matricial.',   ;
               'TDosPrn():New( nPrintType, cPort, cEchoFile )' )
        RETURN Self ///
   End

   If cPort = NIL
      cPort := "LPT1"
   Else
      cPort := Upper( cPort )
   End

   IF ::lNoPrint
    cPort     := ""
    cEchoFile := "print.txt"
   ENDIF

   If de:lPrintTipo
    cPort     := ""
    cEchoFile := StrZero( nRandom(), 08 ) + ".prn"
   EndIf

   ::nLeftMargin := 0
   ::nTopMargin  := 0
   ::nRow        := 0
   ::nCol        := 0
   ::lAnsiToOem  := .T.
   ::lAutoFF     := .T.
   ::cBuffer     := ""

   ::cFormFeed  :=   cPRNCMD_FF
   ::acDouble   := { cPRNCMD_LASERJET_10CPI, cPRNCMD_INKJET_DOUBLE  , cPRNCMD_MATRIX_DOUBLE   }
   ::acNormal   := { cPRNCMD_LASERJET_12CPI, cPRNCMD_INKJET_NORMAL  , cPRNCMD_MATRIX_NORMAL   }
   ::acCompress := { cPRNCMD_LASERJET_18CPI, cPRNCMD_INKJET_COMPRESS, cPRNCMD_MATRIX_COMPRESS }
   ::cDouble    := ::acDouble  [ nPrintType ]
   ::cNormal    := ::acNormal  [ nPrintType ]
   ::cCompress  := ::acCompress[ nPrintType ]

   ::cHeader     := ""
   ::cFooter     := ""
   ::nPosFooter  := 0
   ::nMAXCOL     := 96 // BY PABLO VIDAL

   ::cName   := ""
   ::cDriver := ""
   ::cPort   := cPort

   ::cFileTemp := StrZero( nRandom(), 08 ) + ".prn"

   IF Left( ::cPort, 2 ) = "\\" //impresora en red
      l1 := .F.
   ELSEIF ::cPort = "SELECT"
      PrinterSetup() //dialog box setup: establece como predeterminada una impresora (puede ser en red)
      ::cName   := PrnGetName()
      ::cDriver := PrnGetDrive()
      ::cPort   := PrnGetPort()
      l1 := .t.
   ELSE // "LPTn[:]"
      l1 := .t.
   END

   If l1
      If Right( ::cPort, 1 ) = ":"
       ::cPort := Left( ::cPort, Len( ::cPort ) - 1 )
      End
   End

   ::hDC := fCreate( ::cPort )
   //::hDC := fCreate( ::cFileTemp ) //cPort )

   IF ::hDC < 0
    ::LastError := fError()
   ELSE
    ::LastError := 0
   END

   ::cEchoFile := ""
   IF cEchoFile != NIL
    //cEchoFile += If( "." $ cEchoFile, "", ".PRN" ) //vale cualquier nombre de fichero: también sin extensión
      ::hEchoFile := fCreate( cEchoFile )
      If ::hEchoFile < 0
         MsgStop( 'Error al crear el fichero  cEchoFile , cuyo nombre es:' + CR + ;
                  cValToChar( cEchoFile ), ;
                  '::New( nPrintType, cPort, cEchoFile )' )
      Else
         ::cEchoFile := cEchoFile
         ::lEditFile := .T.
      End
   ELSE
      ::hEchoFile := 0
   END

RETURN Self


METHOD End()  CLASS TDosPrn
LOCAL cPrinter := GetDefaultPrinter()
Local nResult  := 0

   IF !Empty( ::nRow + ::nCol ) .And. ::lAutoFF
      ::EndPage()
   END

   IF !fClose( ::hDC )
      ::LastError := fError()
   //ELSE
    //::LastError := 0

    //IF WaitRun( "command.com /c copy /b " + ::cFileTemp + " " + ::cPort , 0 ) > 31
    // MsgStop("No se pudo imprimir")
    //Else
    // FERASE( ::cFileTemp )
    //ENDIF

   EndIf


    IF ::hEchoFile <> 0
      fClose( ::hEchoFile )
      If ::lEditFile

       //edita en ANSI, maximizadamente. No imprime bien
       // (los comandos salen impresos y no se comprime la letra).-
       //WinExec( 'WRITE ' + ::cEchoFile, 3 )

       //Edita, maximizadamente pero en ventana MSDOS, el texto OEM. Imprime bien.-
       //WaitRun( "command.com /c EDIT " + ::cEchoFile, 3 )

          //WaitRun( "command.com /c copy /b " + ::cEchoFile + " lpt1", 0 )

        If ::lPrintWin
         // Impresion asistida por windows
         nResult := PrintFileRaw( cPrinter, ::cEchofile, "Impresion..." )
         FERASE( ::cEchofile ) // by pablo vidal
        Else
         WinExec( "COMMAND.COM /C PABLO.BAT" , 0 )
        EndIf
       //FERASE( ::cEchofile ) // by pablo vidal

      End
   END

RETURN NIL


METHOD EndPage() CLASS TDosPrn

   If !Empty( ::cHeader )
      ::cBuffer := If( ::lAnsiToOem, AnsiToOem( ::cHeader ), ::cHeader ) + ;
                   CRLF + ::cBuffer
   End
   If !Empty( ::cFooter )
      if     ::nPosFooter = 0 //es el valor predeterminado
             ::nPosFooter := ::nRow + 1
      end
      ::Say( ::nPosFooter, 0, ::cFooter + CRLF ) //añade  ::cFooter + CRLF  al final de  ::cBuffer
   End
   ::Command( ::cFormFeed ) //añade el comando ::cFormFeed  al final de  ::cBuffer

   If fWrite( ::hDC, ::cBuffer ) < Len( ::cBuffer ) ///////
      ::LastError := fError()
   Else
      ::LastError := 0
   End

   IF ::hEchoFile <> 0
    fWrite( ::hEchoFile, ::cBuffer )
   END

   ::cBuffer := ""
   ::nRow    := 0
   ::nCol    := 0

RETURN NIL


METHOD Command( cStr1, cStr2, cStr3, cStr4, cStr5, ;
                cStr6, cStr7, cStr8, cStr9 )  CLASS TDosPrn

   local nToken := 1
   local cToken, cCommand := "", ;
         cString := cStr1

   IF cStr2 != NIL
      cString += "," + cStr2
   END
   IF cStr3 != NIL
      cString += "," + cStr3
   END
   IF cStr4 != NIL
      cString += "," + cStr4
   END
   IF cStr5 != NIL
      cString += "," + cStr5
   END
   IF cStr6 != NIL
      cString += "," + cStr6
   END
   IF cStr7 != NIL
      cString += "," + cStr7
   END
   IF cStr8 != NIL
      cString += "," + cStr8
   END
   IF cStr9 != NIL
      cString += "," + cStr9
   END

   DO WHILE !Empty( cToken := StrToken( cString, nToken++, "," ) )
      cCommand += Chr( Val( cToken ) )
   ENDDO

   ::cBuffer += cCommand

RETURN NIL


METHOD SetCoors( nRow, nCol ) CLASS TDosPrn

   nRow += ::nTopMargin
   nCol += ::nLeftMargin

   IF ::nRow > nRow
      ::EndPage()
      ::StartPage()
   END

   //IF nRow == ::nRow  .AND. nCol < ::nCol
   //   ::EndPage()
   //   ::StartPage()
   //END

   DO WHILE ::nRow < nRow
      ::NewLine()
   ENDDO
   IF nCol > ::nCol
      ::Write( Space( nCol - ::nCol ) )
   END


RETURN NIL


METHOD SayDbl( nRow, nCol, cText, lAToO ) CLASS TDosPrn

   DEFAULT lAToO := ::lAnsiToOem

   ::SetCoors( nRow, nCol )
   ::Command( ::cDouble )
   ::cBuffer += If( lAToO, AnsiToOem( cText ), cText )
 //::nCol    += Int( Len( cText ) * 2.3 + .5 ) ///
   ::nCol    += Len( cText )
   ::Command( ::cNormal )

RETURN NIL


METHOD Say( nRow, nCol, cText, lAToO ) CLASS TDosPrn
LOCAL cBUFFERNEW := ""
LOCAL cSTRING := ""

DEFAULT lAToO := ::lAnsiToOem

   nRow += ::nTopMargin
   nCol += ::nLeftMargin

   IF ::nRow > nRow
      ::EndPage()
      ::StartPage()
   END

   DO WHILE ::nRow < nRow
      ::NewLine()
   ENDDO

   IF nCol > ::nCol
      ::Write( Space( nCol - ::nCol ) )
   END

   IF nRow == ::nRow  .AND. nCol < ::nCol
     ::SetCoors( nRow, nCol )

     cBUFFERNEW := SUBSTR( ::cBuffer, 1, RAT( CRLF, ::cBuffer ) )
     cSTRING    := ALLTRIM( SUBSTR( ::cBuffer, RAT( CRLF, ::cBuffer ) ) )
     cSTRING    := SUBSTR( cSTRING, 00, nCOL  ) + cTEXT + SUBSTR( cSTRING, nCOL + 01 )
     cSTRING    := SUBSTR( cSTRING, 00, ::nMAXCOL )
     ::cBUFFER  := cBUFFERNEW + cSTRING + CRLF
     ::nCOL     := LEN( cSTRING )

   ELSE
     ::cBuffer +=  cText
     ::nCol    +=  Len( cText )
   ENDIF

   //ELSE
   //  ::SetCoors( nRow, nCol )
   //  ::Write( cText, lAToO )
   //ENDIF

RETURN NIL


METHOD SayCmp( nRow, nCol, cText, lAToO ) CLASS TDosPrn

   DEFAULT lAToO := ::lAnsiToOem

   ::SetCoors( nRow, nCol )
   ::Command( ::cCompress )
   ::cBuffer += If( lAToO, AnsiToOem( cText ), cText )
 //::nCol    += Int( Len( cText ) / 1.7 + .5 ) ///
   ::nCol    += Len( cText )
   ::Command( ::cNormal )

RETURN NIL

 
Saludos,

Pablo Alberto Vidal
/*
------------------------------------------------------
Harbour 3.2.0, Fivewin 17.02, BCC7
------------------------------------------------------
*/
Post Reply