Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening an EPOS Till Draw with control codes - N00b question

Status
Not open for further replies.

RipperRoo

Programmer
Jan 12, 2007
5
GB
Hi Everyone, first of all let me say what a wonderfully useful forum this is, i have spent the last few days looking through the FAQ's and posts and answered loads of questions i never knew i had!

But now onto my question, I'm writing my first Deplhi EPOS and being a total n00b im working stage by stage, i have finished the layout and navigation, its now its time to start adding functionality to the program and the first thing i need to add is a way to open the till draw.

on a button click I need to send the leter 'A' directly to the printer in font size 10 using the font type 'control' (you guessed it - its an epson recipt printer)

the recipt printer will be connected to LPT1 but the application will also use a USB printer for normal A4 document output.

its a shame the person who asked this three years ago didnt give a code example when they worked out how to do this :(

many thanks in advance for your help with this and the thousands of other questions i'm bound to ask before this project is finished.

RipperRoo

all my code is Fully Utilizing Code Key Encrypted Data!
 
Opppps - Forgot to mention

I'm using Delphi 7 Second Edition v7.2

all my code is Fully Utilizing Code Key Encrypted Data!
 
ok, this looks like it.

Code:
procedure TForm64.BitBtn1Click(Sender: TObject);
begin
 // 'Printers' must be added to uses


        Printer.PrinterIndex := 1 //Index of printer which isn’t default
        Printer.BeginDoc;
  Printer.Canvas.Font.Name := 'control';
  Printer.Canvas.Font.Size := 10;
  Printer.Canvas.Font.Style := [];

  Printer.Canvas.TextOut(10, 10 , 'A');

        Printer.EndDoc;
end;

all my code is Fully Utilizing Code Key Encrypted Data!
 
The following code should help. It works on most EPOS printers. More hardware specific code will do more fancy things.

procedure TFormMain.OpenDrawer;
var drawercode : String;
begin
drawercode :=#27+'p0'+#100+#100;
SendControlCode(drawercode);
end;

procedure TFormMain.SendControlCode(S: string);
var
Handle, hDeviceMode: THandle;
N: DWORD;
DocInfo1: TDocInfo1;
Device, Driver, Port: array[0..255] of char;
PrinterName: string;
buf:array[0..255] of char;
lbuf:integer;
begin
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
PrinterName := Format('%s', [Device]);
if not OpenPrinter(PChar(PrinterName), Handle, nil) then
RaiseLastWin32Error;
try
with DocInfo1 do
begin
pDocName := 'Control';
pOutputFile := nil;
pDataType := 'RAW';
end;
StartDocPrinter(Handle, 1, @DocInfo1);
try
lbuf:=length(s);
copymemory(@buf,Pchar(s),lbuf);
if not WritePrinter(Handle, @buf, lbuf, N) then
RaiseLastWin32Error;
finally
EndDocPrinter(Handle);
end;
finally
ClosePrinter(Handle);
end;
end;
 
looks like what i need, but how would i insert it into my form to make the till open on the clck of a button?

i currently have this code working but wasting a couple of inchs of paper and spitting the odd exception if you try running it when the other printer is in use:

Code:
unit Unit61;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Printers, ExtCtrls, StdCtrls, Buttons, Dialogs, ExtCtrls, StdCtrls, Buttons;

type
  TForm61 = class(TForm)
    BitBtn36: TBitBtn;
    BitBtn1: TBitBtn;
    Image1: TImage;
    procedure BitBtn36Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form61: TForm61;

implementation

{$R *.dfm}

procedure TForm61.BitBtn36Click(Sender: TObject);
begin
          Printer.PrinterIndex := 0; //Index of printer which isn’t default, default is 1?
     Printer.BeginDoc;
       Printer.Canvas.Font.Name := 'control';
        Printer.Canvas.Font.Size := 10;
         Printer.Canvas.Font.Style := [];

  Printer.Canvas.TextOut(1, 1, 'A');

        Printer.EndDoc;
             Form9.Show; Form61.Close;
end;

procedure TForm61.BitBtn1Click(Sender: TObject);
begin
     Form9.Show; Form61.Close;
end;

end.

Sorry if this is a stupid question, but as i said, complete n00b, it took me 3 days to work out this basic procedure and it probobly has more leaks than Wales :(

all my code is Fully Utilizing Code Key Encrypted Data!
 
*BUMP**

all my code is Fully Utilizing Code Key Encrypted Data!
 
You can call any procedure or method from a button click event handler, so if you want to use ors' code you could do somethinglike this

Code:
unit Unit61;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Printers, ExtCtrls, StdCtrls, Buttons, Dialogs, ExtCtrls, StdCtrls, Buttons;

type
  TForm61 = class(TForm)
    btnOpenDrawer: TBitBtn;
    procedure btnOpenDrawerClick(Sender: TObject);
  private
    procedure SendControlCode(S: string);
  public
    { Public declarations }
  end;

var
  Form61: TForm61;

implementation

{$R *.dfm}

procedure TForm61.btnOpenDrawerClick(Sender: TObject);
var drawercode : String;
begin
  drawercode :=#27+'p0'+#100+#100;
  SendControlCode(drawercode);
end;

procedure TForm61.SendControlCode(S: string);
 var
  Handle, hDeviceMode: THandle;
  N: DWORD;
  DocInfo1: TDocInfo1;
  Device, Driver, Port: array[0..255] of char;
  PrinterName: string;
  buf:array[0..255] of char;
  lbuf:integer;
 begin
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  PrinterName := Format('%s', [Device]);
  if not OpenPrinter(PChar(PrinterName), Handle, nil) then
   RaiseLastWin32Error;
  try
   with DocInfo1 do
   begin
    pDocName := 'Control';
    pOutputFile := nil;
    pDataType := 'RAW';
   end;
   StartDocPrinter(Handle, 1, @DocInfo1);
   try
    lbuf:=length(s);
    copymemory(@buf,Pchar(s),lbuf);
    if not WritePrinter(Handle, @buf, lbuf, N) then
     RaiseLastWin32Error;
   finally
    EndDocPrinter(Handle);
   end;
  finally
   ClosePrinter(Handle);
  end;
 end;

I have just typed this code straight in here so apologies if it doesn't compile, but any errors will be simple ones.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top