?
Solved

Print a Text File for the Serial Port

Posted on 2001-07-19
4
Medium Priority
?
573 Views
Last Modified: 2010-04-06
How can I print a text file in a printer that is connected to a serial port?
0
Comment
Question by:Kefren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6301096
i would guess this works as on any other printer,
if a windows printer-device-driver is installed

look at the tprinter-object
0
 
LVL 3

Accepted Solution

by:
VSF earned 200 total points
ID: 6301885
title               : Print to a Serial Printer without Author              : Joe C. Hecht
authoremail         : joehecht@gte.net
description         : How to print to a serial printer

> I have a Serial Printer conected to COM1 but I don't have the NT driver
> for it (it is an EPSON TM-U295), I need to print from my delphi 4
> application with a series of ESC commands, how can I do this.

You can either work dirrectly with the comm port functions,
or install any print driver (TTY/Generic recommended) and
use the following code:

The following unit "PrtRaw.pas" demonstrates a 16/32 bit
raw printing unit for Delphi and Borland C Builder.

Following the unit, is an exmaple on using the
PrtRaw unit.

Notes:

1) You are responsible for embedding all necessary
control codes that the printer may require (including
form feed codes).

2) You must specify a valid printer and port name.

unit PrtRaw;

{Copyright (c) 1998 by Joe C. Hecht - All rights Reserved}
{joehecht@gte.net You may use and share this unit}
{providing my name and the copyright notice stays intact.}

interface

uses
  WinTypes;

{$IFDEF WIN32}
 type SpoolInt = DWORD;
{$ELSE}
 type SpoolInt = integer;
{$ENDIF}

function StartRawPrintJob(PrinterName : pChar;
                          Port : pChar;
                          DocName : pChar) : THandle;

function StartRawPrintPage(hPrn : THandle) : integer;

function PrintRawData(hPrn : THandle;
                      Buffer : pointer;
                      NumBytes : SpoolInt) : integer;

function EndRawPrintPage(hPrn : THandle) : integer;

function EndRawPrintJob(hPrn : THandle) : integer;

implementation
 uses
   WinProcs,
{$IFDEF WIN32}
 WinSpool;
{$ELSE}
 Print;
{$ENDIF}

function StartRawPrintJob(PrinterName : pChar;
                          Port : pChar;
                          DocName : pChar) : THandle;
{$IFDEF WIN32}
 var
   hPrn : THandle;
   DocInfo1 : TDocInfo1;
{$ENDIF}
begin
 {$IFDEF WIN32}
   if (OpenPrinter(PChar(PrinterName),
                   hPrn,
                   nil) = FALSE)  then begin
     Result := THandle(-1);
     Exit;
   end;
   DocInfo1.pDocName := DocName;
   DocInfo1.pOutputFile := Port;
   DocInfo1.pDataType := 'RAW';
   if (StartDocPrinter(hPrn,
                       1,
                       @DocInfo1) = 0) then begin
     Result := THandle(-1);
     exit;
   end;
   Result := hPrn;
 {$ELSE}
  result := OpenJob(Port,
                    DocName,
                    0);
 {$ENDIF}
end;

function StartRawPrintPage(hPrn : THandle) : integer;
begin
 {$IFDEF WIN32}
   if (StartPagePrinter(hPrn) = FALSE) then begin
     Result := -1;
     exit;
   end;
   result := 1;
 {$ELSE}
  result := StartSpoolPage(hPrn);
 {$ENDIF}
end;

function PrintRawData(hPrn : THandle;
                      Buffer : pointer;
                      NumBytes : SpoolInt) : integer;
{$IFDEF WIN32}
var
  BytesWritten : DWORD;
 {$ENDIF}
begin
  if (NumBytes = 0) then begin
    Result := 1;
    exit;
  end;
 {$IFDEF WIN32}
   if (WritePrinter(hPrn,
                    Buffer,
                    NumBytes,
                    BytesWritten) = FALSE) then begin
     Result := -1;
     exit;
   end;
   if (NumBytes <> BytesWritten) then begin
     Result := -1;
     exit;
   end;
   Result := 1;
 {$ELSE}
  result := WriteSpool(hPrn,
                       Buffer,
                       NumBytes);
 {$ENDIF}
end;

function EndRawPrintPage(hPrn : THandle) : integer;
begin
 {$IFDEF WIN32}
   if (EndPagePrinter(hPrn) = FALSE) then begin
     Result := -1;
     exit;
   end;
   Result := 1;
 {$ELSE}
  result := EndSpoolPage(hPrn);
 {$ENDIF}
end;

function EndRawPrintJob(hPrn : THandle) : integer;
begin
 {$IFDEF WIN32}
   if (EndDocPrinter(hPrn) = FALSE) then begin
     Result := -1;
     exit;
   end;
   if (ClosePrinter(hPrn) = FALSE) then begin
     Result := -1;
     exit;
   end;
   Result := 1;
 {$ELSE}
  result := CloseJob(hPrn);
 {$ENDIF}
end;

end.

Example of use:

uses
  PrtRaw;

procedure TForm1.Button1Click(Sender: TObject);
var
  hPrn : THandle;
  Data : array[0..31] of char;
begin

  hPrn := StartRawPrintJob('Generic / Text Only',
                           'LPT1:',
                           'My Document');
  if (integer(hPrn) < 0) then begin
    ShowMessage('StartRawPrintJob Failed');
    exit;
  end;

  if (StartRawPrintPage(hPrn) < 0) then begin
    ShowMessage('StartRawPrintPage Failed');
    EndRawPrintJob(hPrn);
    exit;
  end;

  Data[0] := 'P';
  Data[1] := 'a';
  Data[2] := 'g';
  Data[3] := 'e';
  Data[4] := #32;
  Data[5] := '1';
  Data[6] := #13;
  Data[7] := #10;

  if (PrintRawData(hPrn,
                   @data,
                   8) < 0) then begin
    ShowMessage('PrintRawData Failed');
    EndRawPrintPage(hPrn);
    EndRawPrintJob(hPrn);
    exit;
  end;

  if (EndRawPrintPage(hPrn) < 0) then begin
    ShowMessage('EndRawPrintPage Failed');
    EndRawPrintJob(hPrn);
    exit;
  end;

  if (StartRawPrintPage(hPrn) < 0) then begin
    ShowMessage('StartRawPrintPage Failed');
    EndRawPrintJob(hPrn);
    exit;
  end;
  Data[0] := 'P';
  Data[1] := 'a';
  Data[2] := 'g';
  Data[3] := 'e';
  Data[4] := #32;
  Data[5] := '2';
  Data[6] := #13;
  Data[7] := #10;

  if (PrintRawData(hPrn,
                   @data,
                   8) < 0) then begin
    ShowMessage('PrintRawData Failed');
    EndRawPrintPage(hPrn);
    EndRawPrintJob(hPrn);
    exit;
  end;

  if (EndRawPrintPage(hPrn) < 0) then begin
    ShowMessage('EndRawPrintPage Failed');
    EndRawPrintJob(hPrn);
    exit;
  end;

  if (EndRawPrintJob(hPrn) < 0) then begin
    ShowMessage('EndRawPrintJob Failed');
    exit;
  end;

end;

end.



0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8703658
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept VSF's comment as answer

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question