Board index » delphi » How should I print ASCII Text in Delphi 2.

How should I print ASCII Text in Delphi 2.

Quote
Simon Grunbauer wrote:
> I am writing a Label printing program in Delphi 2, attached to a label

> printer via LPT1, which requires its instructions in the form of ASCII
> text
> to describe the label.  I could not use the TPrinter object because it
> sent
> the text to Print Manager which printed the text in a graphic format,
> and
> not as ASCII text.

> Now I am using the following code:

> procedure TForm1.Button2Click(Sender: TObject);
> var
>   Prn_Port: TextFile;
> begin
>   AssignFile(Prn_Port, 'LPT1');
>   Rewrite(Prn_Port);
>   Writeln(Prn_Port, '! 0 100 1150 1');
>   Writeln(Prn_Port, 'PITCH 200');
>   Writeln(Prn_Port, 'FILL_BOX 90 10 710 1140');
>   Writeln(Prn_Port, 'TEXT 1(0,90,1,1) 255 850 CONSIGNMENT NO:');
>   Writeln(Prn_Port, 'TEXT 3(0,90,1,1) 205 850 2423748');
>   Writeln(Prn_Port, 'TEXT 4(0,90,1,1) 110 30 ITEM 1 OF 1');
>   Writeln(Prn_Port, 'END');
>   CloseFile(Prn_Port);
> end;

> This code works fine, while the printer is connected and online.  If
> the
> printer is not connected, the first Writeln command causes the program
> to
> hang.

> How do I overcome this problem, and am I doing this correctly or is
> there a
> better approach.
> --
> Simon Grunbauer
> Sij...@Ozemail.com.au

I think the reason your program hang is it is waiting for your printer
to confirm on the print.  Have you try the Try...Finally...End; command?

I don't understand why you can't use TPrinter, it's much easer.

Richard Tan

 

Re:How should I print ASCII Text in Delphi 2.


Richard Tan <Richard_...@workmail.com> wrote in article
<339437E9.7780E...@workmail.com>...

Quote
> Simon Grunbauer wrote:

> > I am writing a Label printing program in Delphi 2, attached to a label

> > printer via LPT1, which requires its instructions in the form of ASCII
> > text
> > to describe the label.  I could not use the TPrinter object because it
> > sent
> > the text to Print Manager which printed the text in a graphic format,
> > and
> > not as ASCII text.
> > This code works fine, while the printer is connected and online.  

[deletions]

Quote
> > printer is not connected, the first Writeln command causes the program
> > to
> > hang.

> > How do I overcome this problem, and am I doing this correctly or is
> > there a
> > better approach.
> > --
> > Simon Grunbauer
> > Sij...@Ozemail.com.au

> I think the reason your program hang is it is waiting for your printer
> to confirm on the print.  Have you try the Try...Finally...End; command?

> I don't understand why you can't use TPrinter, it's much easer.

> Richard Tan

No, TRY ... FINALLY ... END does not work.  Nor does the use of
IORESULT.  I thought this problem had an easy answer, but after
30 minutes I had to move on.  

TPrinter doesn't really work if you want to control the printer without
all the Windows overhead.  For example, if for some reason you want
to send HPGL code to a HP LaserJet, you would need to follow this
same general approach.

In Delphi 1 BIOS interrupt 0x17 could have been used (see C code
below), but I don't think this will work in Delphi 2.

/* chkprt, EFG, 10/2/90, Turbo C++ */
#include <dos.h>
int main (void)
{
  union REGS regs;

  regs.h.ah = 0x02;               /* request printer port status */
  regs.x.dx = 0x0000;             /* printer number 0..2         */
  int86 (0x17, &regs, &regs);     /* BIOS interrupt x17          */

  printf ("printer status:  dec %03d  hex %02X\n",regs.h.ah,regs.h.ah);

  return (regs.h.ah);

Quote
}                    

Simon, you still have a very interesting problem.

--
_________________________________________________

Earl F. Glynn          EarlGl...@WorldNet.att.net
EFG Software              913/859-9557  Voice/Fax
   Scientific/Engineering/Medical Applications
             Overland Park, KS  USA

Re:How should I print ASCII Text in Delphi 2.


"Earl F. Glynn" <EarlGl...@worldnet.att.net> writes:

Quote
>No, TRY ... FINALLY ... END does not work.  Nor does the use of
>IORESULT.  I thought this problem had an easy answer, but after
>30 minutes I had to move on.  
>TPrinter doesn't really work if you want to control the printer without
>all the Windows overhead.  For example, if for some reason you want
>to send HPGL code to a HP LaserJet, you would need to follow this
>same general approach.

I've had some success in direct printing via the PASSTHROUGH printer
escape (see the Win API Escape function). The upside is that it works for
network printers as well as local printers. The downside is that not all
printer drivers support it, even though it has to be *the* simplest
function to support. Don't make the mistake of sending the data one byte
at a time, though. Send biggish buffers or it'll be S-L-O-W.

Oh, and there is also the SpoolFile API, but that _doesn't_ work for
network printers. It's a {*word*76}y mess.

--
Luke Webber

* Note: The opinions expressed by Luke Webber are in no way supported *
*       by his employers, Luke Webber Consulting Services             *

Re:How should I print ASCII Text in Delphi 2.


Quote
l...@oberon.sub.net.au (Luke Webber) wrote:
>>TPrinter doesn't really work if you want to control the printer without
>>all the Windows overhead.  For example, if for some reason you want
>>to send HPGL code to a HP LaserJet, you would need to follow this
>>same general approach.

>I've had some success in direct printing via the PASSTHROUGH printer
>escape (see the Win API Escape function). The upside is that it works for
>network printers as well as local printers. The downside is that not all
>printer drivers support it, even though it has to be *the* simplest
>function to support. Don't make the mistake of sending the data one byte
>at a time, though. Send biggish buffers or it'll be S-L-O-W.

As far as I know, Windows 32bit does not support any more the
PASSTHROUGH Escape, it's there for compatibility, but does nothing, as
I found out.
The easiest way to print ASCII text using any type of printers and
printing it at highest throuput is to set the "Courier Bew" font with
a supported font size: in this way it works almost at the same speed
as with PASSTHROUGH, but you can use all the printer control you need.
Norbert Braito
bra...@tecsiel.it

Re:How should I print ASCII Text in Delphi 2.


Quote
Simon Grunbauer wrote:

> I am writing a Label printing program in Delphi 2, attached to a label
> printer via LPT1, which requires its instructions in the form of ASCII text
> to describe the label.  I could not use the TPrinter object because it sent
> the text to Print Manager which printed the text in a graphic format, and
> not as ASCII text.

> Now I am using the following code:

> procedure TForm1.Button2Click(Sender: TObject);
> var
>   Prn_Port: TextFile;
> begin
>   AssignFile(Prn_Port, 'LPT1');
>   Rewrite(Prn_Port);
>   Writeln(Prn_Port, '! 0 100 1150 1');
>   Writeln(Prn_Port, 'PITCH 200');
>   Writeln(Prn_Port, 'FILL_BOX 90 10 710 1140');
>   Writeln(Prn_Port, 'TEXT 1(0,90,1,1) 255 850 CONSIGNMENT NO:');
>   Writeln(Prn_Port, 'TEXT 3(0,90,1,1) 205 850 2423748');
>   Writeln(Prn_Port, 'TEXT 4(0,90,1,1) 110 30 ITEM 1 OF 1');
>   Writeln(Prn_Port, 'END');
>   CloseFile(Prn_Port);
> end;

> This code works fine, while the printer is connected and online.  If the
> printer is not connected, the first Writeln command causes the program to
> hang.

> How do I overcome this problem, and am I doing this correctly or is there a
> better approach.
> --
> Simon Grunbauer
> Sij...@Ozemail.com.au

I don't have a solution for you, but I know what you are going through.
In my application, I would like to know if the printer (local or
network) is on line and ready to print.  Unbelievably, their is no way
to determine the *current* status of the printer.  It is possible to
get the last known status, but that's not any good if the status has
changed.

For such a sophisticated operating system (NT or 95), you would think
I could figure out if my printer is out of paper.

Lance
---------------------------------------------------------------------
"...Bond reflected that good Americans were fine people and that most
of them seemed to come from Texas."
{*word*2} Royale. Chapter VII

Re:How should I print ASCII Text in Delphi 2.


Quote
lhirsch wrote:
> I don't have a solution for you, but I know what you are going through.
> In my application, I would like to know if the printer (local or
> network) is on line and ready to print.  Unbelievably, their is no way
> to determine the *current* status of the printer.  It is possible to
> get the last known status, but that's not any good if the status has
> changed.

Some code from SWAG:

const
  pnLPT1    = 0;
  pnLPT2    = 1;
  pnLPT3    = 2;

function PrinterReady(PN: word): boolean; assembler;
asm
    mov     dx, PN              {printer number goes in DX}
    mov     ah, 02h
    int     17h                 {int. 17h service 02h}
    xor     al, al              {assume false}
    and     ah, 10101000b       {clear all other bits}
    cmp     ah, 10000000b       {ready & not out of paper or error?}
    jne     @Done               {no -- leave result false}
    inc     ax                  {yes -- change to true}
@Done:
end;

--
+--------------------------------+
|        The Messiah             |
+--------------------------------+
|  Ou' sont les neiges d' antan  |
|         Villon                 |
+________________________________+
|    There is a man...           |
|   playing a violin...          |
|   and the strings...           |
| are the nerves in his own arm. |
| A twisted soul- the mortar...  |
|  despair- the bricks...        |
| to build a temple of sadness.  |
|  The Crow, J. O'Barr           |
+--------------------------------+

Re:How should I print ASCII Text in Delphi 2.


Quote
lhirsch wrote:
> I don't have a solution for you, but I know what you are going through.
> In my application, I would like to know if the printer (local or
> network) is on line and ready to print.  Unbelievably, their is no way
> to determine the *current* status of the printer.  It is possible to
> get the last known status, but that's not any good if the status has
> changed.

Some code from SWAG:

const
  pnLPT1    = 0;
  pnLPT2    = 1;
  pnLPT3    = 2;

function PrinterReady(PN: word): boolean; assembler;
asm
    mov     dx, PN              {printer number goes in DX}
    mov     ah, 02h
    int     17h                 {int. 17h service 02h}
    xor     al, al              {assume false}
    and     ah, 10101000b       {clear all other bits}
    cmp     ah, 10000000b       {ready & not out of paper or error?}
    jne     @Done               {no -- leave result false}
    inc     ax                  {yes -- change to true}
@Done:
end;

--
+--------------------------------+
|        The Messiah             |
+--------------------------------+
|  Ou' sont les neiges d' antan  |
|         Villon                 |
+________________________________+
|    There is a man...           |
|   playing a violin...          |
|   and the strings...           |
| are the nerves in his own arm. |
| A twisted soul- the mortar...  |
|  despair- the bricks...        |
| to build a temple of sadness.  |
|  The Crow, J. O'Barr           |
+--------------------------------+

Re:How should I print ASCII Text in Delphi 2.


Well how about some error codes???
try
 (print whatever)
except
 (crash dialog and exception)
end;

It there is a chance that a procedure might crash depending on the user's
input or when other apps/devices are being used then make an error code.

Quote
> > This code works fine, while the printer is connected and online.  If
the
> > printer is not connected, the first Writeln command causes the program
to
> > hang.

> > How do I overcome this problem, and am I doing this correctly or is
there a
> > better approach.

--
|_o_
   |  |         This message was brought to you by Fredrik Larsson.
_/ \_            mail2fred @ hotmail.com (space spam protect)

===================================================================
* Home          http://home3.swipnet.se/~w-33398/
* World of links        http://home3.swipnet.se/~w-33398/world/world.html
* Developers
* corner:       http://home3.swipnet.se/~w-33398/developer/
===================================================================

Re:How should I print ASCII Text in Delphi 2.


Your question was about the printer status...

Try Ralf Brown's Interrupt List for everything you ever wanted to know
about interrupts. Even printer status - and I mean everything about the
printer status !!!

Mail me if you think I need to be more specific. Glad to help.

evas...@shisas.co.za

Other Threads