0

this is a test code for printing on EPSON Thermal Printer. All is working fine except the QR code part. Printer just get stuck and I need to reset it manually.

I am following the documentation from this page: https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=143

I am using Delphi 10.3.

What am I doing wrong?

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls ,
  Vcl.Printers,
 WinProcs, WinTypes;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure setupPrinter(const printerName: string);
    procedure PrintTest;
    function DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TPrnBuffRec = packed record
  bufflength: Word;
  Buff_1: array[0..255] of AnsiChar;
end;

function tform1.DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
var
  Buff: TPrnBuffRec;
  TestInt: Integer;
  i: integer;
   Device: PChar;
  Driver: PChar;
  Port: PChar;
begin
  TestInt := PassThrough;
  if Escape(Printer.Handle, QUERYESCSUPPORT, SizeOf(TESTINT), @testint, nil) > 0 then
  begin
    if NextLine then  S := S + #13 + #10;
    StrPCopy(Buff.Buff_1, S);
    Buff.bufflength := StrLen(Buff.Buff_1);
    Escape(Printer.Canvas.Handle, Passthrough, 0, @buff, nil);
    Result := True;
  end
  else
    Result := False;
end;

Procedure tform1.PrintTest;
var
   store_len: integer;
   store_pl: Byte;
   store_ph : Byte;
   qrData: string;
Begin

        setupPrinter('EPSON TM-T88V Receipt');
        Printer.BeginDoc;
        DirectToPrinter(Chr(27)+Chr(64), false);           //init
        DirectToPrinter(Chr(27)+chr(97)+chr(49), false);
//        DirectToPrinter(Chr(27)+chr(33)+Chr(8), false);   //font select
        DirectToPrinter(Chr(27)+chr(45)+Chr(49), false);   //underline
        DirectToPrinter('Test!', true);
        DirectToPrinter(Chr(27)+chr(100)+Chr(5), false);   //feed 5 lines
        DirectToPrinter(Chr(27)+chr(45)+Chr(48), false);   //underline
        DirectToPrinter(Chr(27)+chr(97)+chr(48), false);   //align-center
        DirectToPrinter('Hello world!', true);

        DirectToPrinter(
          chr(29)+chr(72)+chr(49),
          False

        );                   //show content aboce barcode
//
        DirectToPrinter(
          chr(29)+chr(102)+chr(48),
          False

        );                 //font  A

//        DirectToPrinter(
//          chr(29)+chr(104)+chr(50),
//          False
//
//        );        //height

//        DirectToPrinter(
//          chr(29)+chr(119)+chr(50),
//          False
//
//        );        //width

        DirectToPrinter(
          chr(29)+chr(107)+chr(4)+'*0001443AB*',
          true
        );   //ean 39


        DirectToPrinter(
          chr(29)+chr(107)+chr(73)+chr(13)+chr(123)+Chr(65)+'8600123456789',
          true
        );          // ean 128


        DirectToPrinter(
          chr(29)+chr(107)+chr(67)+Chr(12)+'860012345678',
          true
        ); // ean 13

        //*************************QR CODE ****************/

        qrData := 'https://www.stackoverflow.com';
        store_len:= Length(qrData)+3;
        store_pl := store_len mod 256;
        store_ph := Trunc(store_len / 256);

        DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+char(3)+chr(0)+Chr(49)+Chr(65)+
          Chr(49)+Chr(0),false
        ); //QR Code: Select the model

        DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(67)+
          Chr(2),false
        ); //QR Code: Set the size of module

        //QR Code: Select the error correction level
        DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(69)+
          Chr(48),false
        );

        //QR Code: Store the data in the symbol storage area
        DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+
          chr(store_pl)+Chr(store_ph)+
          Chr(49)+Chr(80)+Chr(48)+qrData,
          false
        );

//        QR Code: Print the symbol data in the symbol storage area
         DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+chr(store_pl)+chr(store_ph)+Chr(49)+Chr(81)+Chr(48),true
        );

        DirectToPrinter(Chr(27)+Chr(112)+Chr(48)+Chr(60)+Chr(120), false); //drawer pulse
        Printer.EndDoc;
End;

procedure TForm1.setupPrinter(const printerName: string);
var
  Buff: TPrnBuffRec;
  TestInt: Integer;
  i: integer;
   Device: PChar;
  Driver: PChar;
  Port: PChar;
  HDeviceMode: THandle;
begin
  Printer.PrinterIndex := -1;
  GetMem(Device, 255);
  GetMem(Driver, 255);
  GetMem(Port, 255);
  for I := 0 to Printer.Printers.Count - 1 do
  begin
    if Printer.Printers[I] = printername then
    begin
      printer.PrinterIndex := I;
      printer.getprinter(Device, Driver, Port, HdeviceMode);
      StrCat(Device, ',');
      StrCat(Device, Driver);
      StrCat(Device, Port);
      WriteProfileString('windows', 'device', Device);
      StrCopy(Device, 'windows');
      SendMessage(HWND_BROADCAST, WM_WININICHANGE,
        0, Longint(@Device));
      break;
    end;
  end;
  FreeMem(Device, 255);
  FreeMem(Driver, 255);
  FreeMem(Port, 255);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintTest;
end;
end.

3 Answers 3

0

The length is specified incorrectly in the command to print the QR code.
The same data length as the command that stores the QR code, which is the previous command, is specified, but this is a fixed value of 3 bytes.

GS ( k <Function 181>

This part:

//        QR Code: Print the symbol data in the symbol storage area
         DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+chr(store_pl)+chr(store_ph)+Chr(49)+Chr(81)+Chr(48),true
        );

Please change to here:

//        QR Code: Print the symbol data in the symbol storage area
         DirectToPrinter(
          Chr(29)+chr(40)+Chr(107)+chr(3)+chr(0)+Chr(49)+Chr(81)+Chr(48),true
        );
Sign up to request clarification or add additional context in comments.

5 Comments

I changed that part (like you have mentioned), but still nothing happens. QR code doesn't get printed out, the printer get stuck, so I need to reset it. I see the document in the printer queue, marked as "Sent to printer", and it disappears after a few minutes). Codes above the QR code are printed out correctly. Also, I changed the "Select model part" with DirectToPrinter( Chr(29)+chr(40)+Chr(107)+char(3)+chr(0)+Chr(49)+Chr(65)+ Chr(49)+Chr(0),false ); //QR Code: Select the model
Is it possible to redirect the data sent to the printer to a file? If you can do that, If you can do that, try parsing the resulting file with the EscPosDecorde program in this tool. kunif/EscPosUtils
Is there a possibility to get this executable file (EscPosUtils) instead of installing Visual Studio and compiling it?
There is no place to archive the executable file. However, although it is convenient to have that tool, you can analyze it manually without it. The file that can be created with the source code of the question will be small, so if you have an editor that can edit binary data in hexadecimal mode, you will be able to work.
You both keep in mind that you could also write it in a more compact style: #29#40#107#3#0#49#81#48.
0

You have a problem with function 165: QR Code: Select the model

You put: Chr(29)+chr(40)+Chr(107)+char(3)+chr(0)+Chr(49).....

And is: Chr(29)+chr(40)+Chr(107)+chr(4)+chr(0)+Chr(49).....

Comments

0

here's how it works for me.

    store_len:= Length(qrData)+3;
    store_pl := store_len mod 256;
    store_ph := Trunc(store_len / 256);
                                                                       //49
    alCMD.Add(Chr(29)+chr(40)+Chr(107)+chr(4)+chr(0)+Chr(49)+Chr(65)+Chr(50)+Chr(0));  // QR Code: Select the model
                                                                       // 2
    alCMD.Add(Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(67)+Chr(8));          // QR Code: Set the size of module
    alCMD.Add(Chr(29)+chr(40)+Chr(107)+chr(3)+Chr(0)+Chr(49)+Chr(69)+Chr(48));         // QR Code: Select the error correction level
    // QR Code: Store the data in the symbol storage area
    alCMD.Add(Chr(29)+chr(40)+Chr(107)+chr(store_pl)+Chr(store_ph)+Chr(49)+Chr(80)+Chr(48)+qrData);
    // QR Code: Print the symbol data in the symbol storage area
    alCMD.Add(Chr(29)+chr(40)+Chr(107)+chr(3)+chr(0)+Chr(49)+Chr(81)+Chr(48));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.