Board index » delphi » Check printer status - urgent

Check printer status - urgent

Quote
J Preou <jpr...@ihug.co.nz> wrote in message

news:rbeg6sc9iqjcad4h5p51mutp9ecis6nrru@4ax.com...

Quote
> I need to be able to check that a printer is attached to LPT1 and
> ready to print (ie online, paper loaded, etc).

Try the code below. The error codes you get back from the port may not
accurately reflect the state of your printer, so you might need to
experiment a bit....

uses Printers;

const
  Offline      = $00;
  TimeOut      = $01;
  IOError      = $08;
  Online       = $10;
  PaperOut     = $20;
  PaperOut2    = $30;
  SwitchedOff  = $80;
  PrnReady     = $90;
  NoConnect    = $B0;

function PrinterReady(LPT: Word; var StatusMsg: string): Boolean;
var
  PrnDevice, PrnPort, PrnDriver: array[0..79] of Char;
  PrnHandle: THandle;
  Port: Word;
  ErrorCode, E: Byte;
  Code: integer;

  function GetPrinterStatus(LPT: Word): Byte;
  begin
    asm
      mov ah, 2;
      mov dx, LPT;
      dec dx;
      int $17;
      mov @Result, ah;
    end;
  end;

begin
  Result := False;
  Port := LPT;
  if Port = 0 then
  begin
    // no port number passed, so get the port of the default printer
    Printer.GetPrinter(PrnDevice, PrnDriver, PrnPort, PrnHandle);
    if Pos('LPT', PrnPort) > 0 then
    begin
      Val(PrnPort[Length('LPT')], Port, Code);
      if Code > 0 then
        Port := 1;
    end
    else    // default to LPT1
      Port := 1;
  end;

  if (Port > 4) or (Port < 1) then
  begin
   raise ERangeError.CreateFmt('LPT%d is not within the valid range of
%d..%d',
     [Port, 1, 4]);
    Exit;
  end;

  ErrorCode := GetPrinterStatus(Port);
  ErrorCode := ErrorCode and NoConnect;
  E := ErrorCode shl 6;
  if E > 0 then
    ErrorCode := NoConnect;

  case ErrorCode of
    PrnReady: begin
                StatusMsg := 'Printer ready';
                Result := True;
              end;
    Timeout     : StatusMsg := 'Time out';
    IOError     : StatusMsg := 'I/O error';
    NoConnect   : StatusMsg := 'Printer not connected';
    Offline     : StatusMsg := 'Printer off line';
    Online      : StatusMsg := 'Printer on line';
    PaperOut    : StatusMsg := 'Printer out of paper';
    PaperOut2   : StatusMsg := 'Printer out of paper 2';
    SwitchedOff : StatusMsg := 'Printer not switched on';
  else
    StatusMsg := 'Printer error code: ' + IntToStr(ErrorCode);
  end;
end;

Call the function like this...

var
  StatusMsg: string;
begin
 PrinterReady(1, StatusMsg);    // passing LPT1 as the port
 ShowMessage(StatusMsg);
end;

--
Stephen Brown

 

Re:Check printer status - urgent


Quote
J Preou <jpr...@ihug.co.nz> wrote in message

news:kufj6s8liuo5hcbq1dh7v9olhvc5dfq811@4ax.com...

Quote
> using.  Do you know if the 'printer ready' code is at least the same
> for all printers - after all, I really need to know whether its ready
> or not, I don't care too much what the problem is - just that it is or
> isn't ok to print !

I can't really help you on that score Jeff. I've only tried the code on my
own rather ancient HP LaserJet II. For the record, most of the responses
were sensible - it only had a problem differentiating between 'switched
off', 'not connected' and 'off line', reporting all three as 'off line'. I'd
be surprised if you didn't get a pretty clear go/no-go response from most
printers.

--
Stephen Brown

Other Threads