FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Test to see if USB Printer is ready
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Test to see if USB Printer is ready
Posted: Thu May 21, 2015 04:46 PM

To All

Is there a way to test if the Default USB printer is turned on, Ready and plugged in ?

Thanks
Rick Lipkin

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 05:37 PM
   cPorta := PrnGetPort()

   cPrinter := PrinterPortToName( cPorta )

   if empty(cPrinter)

      cPrinter := PrinterPortToName( "USB002" )

      if empty(cPrinter)

         cPrinter := PrinterPortToName( "USB001" )

         if empty(cPrinter)

            MsgStop("No printers Installed")

            return nil

         endif

      endif

   endif
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 06:33 PM

João

Thank you for your code, unfortunately the example you provided tells me the port and printer name, but not if it is Ready or On-Line :cry:

Thanks
Rick Lipkin

Posts: 8515
Joined: Tue Dec 20, 2005 07:36 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 06:53 PM

Rick,

#include "Fivewin.ch"

FUNCTION Main()

   LOCAL oPrn, oPrint,oFnt, cPrinter := ""
   LOCAL oDlg, oBrw
   LOCAL aPrn := {}
   LOCAL cDef, i := 1

   aPrn := GetPrinters()

   IF Empty (aPrn) // Empty( oPrn:hDC )
     MsgAlert ("No Printers found" )
     RETURN NIL
   ENDIF

   cDef := GetDefaultPrinter()

   DEFINE DIALOG oDlg FROM 2, 2 TO 21, 50 TITLE "Available Printers"

   @ 1, 2 LISTBOX oBrw FIELDS aPrn[ i ] ;
          HEADERS "Printer Array" ;
          FIELDSIZES 200 ;
          OF oDlg ;
          SIZE 100, 100

   oBrw:bGoTop    = { || i := 1 }
   oBrw:bGoBottom = { || i := Eval( oBrw:bLogicLen )}
   oBrw:bSkip     = { | nWant, nOld | nOld := i, i += nWant, ;
                        i := Max( 1, Min( i, Eval( oBrw:bLogicLen ) ) ),;
                        i - nOld }
   oBrw:bLogicLen = { || Len( aPrn ) }
   oBrw:cAlias    = "Array"                // Just put something

   @ 7.8,2 SAY "DEFAULT PRINTER: " + cDef

   @ 1,22 BUTTON "&Print" OF oDlg ACTION Testprint (aPrn[i])
   @ 2,22 BUTTON "&End  " OF oDlg ACTION oDlg:End()

   ACTIVATE DIALOG oDlg CENTERED

RETURN NIL
//--------------------------------------------------------------
FUNCTION TestPrint(cPrn)

   LOCAL oFnt, oPrint
   LOCAL cText := "Printer Test - Default Printer"

   IF MsgYesNo ( "Print to " + cPrn)

     PRINT oPrint NAME "Formular" TO (cPrn)

     DEFINE FONT oFnt NAME "ARIAL" SIZE 0,12 OF oPrint

     oPrint:SetCopies (2)
     oPrint:SetLandscape()

     oPrint:Setup ()  // check the settings

     PAGE
       oPrint:CmSay (1,1, cText,oFnt)
     ENDPAGE

     ENDPRINT

     oFnt:End()

   ENDIF

RETURN NIL
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 07:46 PM

João

Thank you again for your kind response and code .. unfortunately, your example shows me the printers loaded on the computer, but not the specific attributes of the printer such as 'being ready'

What I am really looking for is to be able to interrogate the default printer attributes and determine if the printer is 'on line' and 'ready'

Thanks
Rick Lipkin

Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 08:05 PM

Rick

You can test the function hb_printerIsReady ("USB001")?

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 08:10 PM

IsPrinter()
Determines if print output can be processed.
Syntax
IsPrinter( [<cPrinterName>] ) --> lIsPrinter

Arguments
<cPrinterName>
An optional character string holding the name of a particular printer driver. Return
The function returns .T. (true) when the operating system can process print output, otherwise .F. (false) is returned.
Description
The IsPrinter() function is used to test if an application can pass print output to the operating system. This is usually the case when a print spooler is active. IsPrinter() cannot detect, if a physical printer is ready to print, since this is monitored by the operating system.
If a character string holding a particular printer name is passed, IsPrinter() returns .T. (true) if this printer is installed.
Note: under CA-Clipper (DOS), IsPrinter() checked the readiness of a physical printer. This is not possible with xHarbour's IsPrinter() function due to differences between DOS and modern operating systems.
Info
See also: GetDefaultPrinter(), GetPrinters(), PCol(), PRow(), SET DEVICE, SET PRINTER, SetPrc()
Category: Printer functions
Source: rtl\isprint.c
LIB: xhb.lib
DLL: xhbdll.dll

Example
// The example outlines possibilities of getting printer information
// different from CA-Clipper (DOS).

PROCEDURE Main
LOCAL aPrinter := GetPrinters()

  ? ValToPrg( aPrinter )

  ? GetDefaultPrinter()

  ? IsPrinter()

  ? IsPrinter( aPrinter[1] )

  ? IsPrinter( &quot;HP LaserJet&quot; )

RETURN

Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 08:10 PM

Christobal

I get a compile error using xHarbour unresolved external.

Rick

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 08:19 PM

Samir

The result for IsPrinter() is always .t. when I test it even though the USB cable is un-plugged.

Rick

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Thu May 21, 2015 08:27 PM

To All

This code does not trap a printer that has the USB plug pulled and is not ready :cry:

Rick Lipkin

PRINTER oPRINT FROM USER                ;
PREVIEW MODAL                           ;
NAME "Gross Sales Report for "+xLOGIN

IF EMPTY( oPRINT:hDC )
    MsgStop ( "Printer not Ready !" )
    CLOSE DATABASES
    oDlg:END()
    RETURN(NIL)
ENDIF
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Re: Test to see if USB Printer is ready
Posted: Fri May 22, 2015 02:00 AM

Dear Rick,

FWH Function is PrnStatus( cPrinterName ), it works for me.

PrnStatus() 

Retrieves the status of a printer. 

Syntax: 

PrnStatus( <cPrinter> ) --> nStatus  

Parameters: 
 <cPrinter> The printer of which the status is to be retrieved.  
Returns: 
 <nStatus> One of the following values:  
 
 #define PRINTER_STATUS_OK            0  
 #define PRINTER_STATUS_PAUSED         1  
 #define PRINTER_STATUS_ERROR         2  
 #define PRINTER_STATUS_PENDING_DELETION   4  
 #define PRINTER_STATUS_PAPER_JAM      8  
 #define PRINTER_STATUS_PAPER_OUT      16  
 #define PRINTER_STATUS_MANUAL_FEED      32  
 #define PRINTER_STATUS_PAPER_PROBLEM      64  
 #define PRINTER_STATUS_OFFLINE         128  
 #define PRINTER_STATUS_IO_ACTIVE         256  
 #define PRINTER_STATUS_BUSY         512  
 #define PRINTER_STATUS_PRINTING         1024  
 #define PRINTER_STATUS_OUTPUT_BIN_FULL      2048  
 #define PRINTER_STATUS_NOT_AVAILABLE      4096  
 #define PRINTER_STATUS_WAITING         8192  
 #define PRINTER_STATUS_PROCESSING      16384  
 #define PRINTER_STATUS_INITIALIZING         32768  
 #define PRINTER_STATUS_WARMING_UP      65536  
 #define PRINTER_STATUS_TONER_LOW      131072  
 #define PRINTER_STATUS_NO_TONER         262144  
 #define PRINTER_STATUS_PAGE_PUNT      524288  
 #define PRINTER_STATUS_USER_INTERVENTION   1048576  
 #define PRINTER_STATUS_OUT_OF_MEMORY      2097152  
 #define PRINTER_STATUS_DOOR_OPEN      4194304  
 #define PRINTER_STATUS_SERVER_UNKNOWN   8388608  
 #define PRINTER_STATUS_POWER_SAVE      16777216
Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Fri May 22, 2015 12:53 PM

Dutch

Thank you for your code .. I did see something similar in the \Samples.. Here is my code,.. I query for the port and the name .. I run both parameters through PrnStatus() .. and the USB printer cable has been removed:

#include "Fivewin.ch"

#define PRINTER_STATUS_OK                       0
#define PRINTER_STATUS_PAUSED                   1
#define PRINTER_STATUS_ERROR                    2
#define PRINTER_STATUS_PENDING_DELETION         4
#define PRINTER_STATUS_PAPER_JAM                8
#define PRINTER_STATUS_PAPER_OUT               16
#define PRINTER_STATUS_MANUAL_FEED             32
#define PRINTER_STATUS_PAPER_PROBLEM           64
#define PRINTER_STATUS_OFFLINE                128
#define PRINTER_STATUS_IO_ACTIVE              256
#define PRINTER_STATUS_BUSY                   512
#define PRINTER_STATUS_PRINTING              1024
#define PRINTER_STATUS_OUTPUT_BIN_FULL       2048
#define PRINTER_STATUS_NOT_AVAILABLE         4096
#define PRINTER_STATUS_WAITING               8192
#define PRINTER_STATUS_PROCESSING           16384
#define PRINTER_STATUS_INITIALIZING         32768
#define PRINTER_STATUS_WARMING_UP           65536
#define PRINTER_STATUS_TONER_LOW           131072
#define PRINTER_STATUS_NO_TONER            262144
#define PRINTER_STATUS_PAGE_PUNT           524288
#define PRINTER_STATUS_USER_INTERVENTION  1048576
#define PRINTER_STATUS_OUT_OF_MEMORY      2097152
#define PRINTER_STATUS_DOOR_OPEN          4194304
#define PRINTER_STATUS_SERVER_UNKNOWN     8388608
#define PRINTER_STATUS_POWER_SAVE        16777216


//------------------------
FUNCTION Main()

Local cPort,aPrn,nStatus,aPrinter

cPort := PrnGetPort()
Msginfo( cPort )

cPrinter := GetDefaultPrinter()
msginfo( "cPrinter" )
msginfo( cPrinter )

nStatus := PrnStatus( cPrinter )
msginfo( nStatus )

nStatus := PrnStatus( cPort )
msginfo( nStatus)

RETURN

cPort = "USB001"
cPrinter = "Brother HL-1440 series"

nStatus passing cPrinter = 0 ( status ok )
nStatus passing cPort = 4096 ( status un-available )

Perhaps running this on XP has some bearing ?? .. the above results retrieved the correct port and printer name .. but the status seemed to conflict each other interrogating the Port vs the printer name ..

Rick Lipkin

Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: Test to see if USB Printer is ready
Posted: Fri May 22, 2015 02:37 PM

Do you have spooler service running?

Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: Test to see if USB Printer is ready
Posted: Fri May 22, 2015 03:33 PM
Samir

Yes .. the reason this thread started is that I use the same printer as our Counter computer, all I do is un-plug the Counter computers USB and insert mine.

Sometimes I forget to take my USB cable out and when we cash out customers, the Counter computer will not print because I have my cable plugged in instead of the Counter computer.

I just want to be able to test the 'ready' state of the default printer so the printer cable issue will be tested and resolved at run-time.. just to be able to throw up a MsgAlert to the user to be able to check if the cable is plugged in.

As you can see from the picture .. my default printer is faded out and is 'not ready' .. I just need some way to test for the 'ready state' before I print.

Rick Lipkin
Posts: 883
Joined: Thu Dec 24, 2009 12:46 AM
Re: Test to see if USB Printer is ready
Posted: Fri May 22, 2015 09:14 PM

=====>

Bayron Landaverry
xBasePHP.com
(215)2226600 Philadelphia,PA, USA
MayaBuilders@gMail.com
Guatemala

FWH25.06--Harbour 3.0.0--BCC7.7--UEstudio 10.10
Windows 10

FiveWin, One line of code and it's done...