1

I use this code to print text file to POS printer (EPSON):

   AssignFile(prnfile, 'file.txt');
   Reset(prnfile, 1);
   AssignFile(port, 'COM3');
   Rewrite(port, 1);
   repeat
     BlockRead(prnfile, buffer, SizeOf(buffer), Read);
     BlockWrite(port, buffer, Read);
   until EOF(prnfile) or (Read <> SizeOf(buffer));
     CloseFile(prnfile);
     CloseFile(port);

Text is printed, but I need to cut a receipt. I have EPSON command codes, but I don't know how to send them to printer. Can anybody write an example?

Thank You.

Mindaugas
  • 115
  • 2
  • 11
  • Send them in exactly the same way as you are doing here. Although personally I'd prefer not to use legacy Pascal I/O. Actually I get the impression that you've just copied this code and don't understand how it works. I suspect that because you ask us to write the code for you rather than help you understand. Take some time to understand what this code does. Then you'll realise that sending command codes is just the same. – David Heffernan Jul 26 '15 at 14:43
  • Yes, you are right, I do not fully understand this code and I tried to use the same code to send commands to pirnter, but without luck. And what do you recommend to use instead of " legacy Pascal I/O"? – Mindaugas Jul 26 '15 at 14:59
  • I suggest that you endeavour to understand what you are doing. On the face of it it would seem that you don't yet know what you are doing, don't understand this code, and are asking us to write your code for you. That is not what this site is for. – David Heffernan Jul 26 '15 at 15:17

2 Answers2

1

I've tried a lot and finally I've written this code that works:

procedure Cut();
 var epsonprn : System.Text;
begin
 try
   AssignFile(epsonprn,'COM3');// the name of printer port, can be a network share
   Rewrite(epsonprn);
   Write(epsonprn,#29#86#66#0);//cut sequence
 finally
   CloseFile(epsonprn);
 end;
end;

so the solution is:

procedure TForm1.Button1Click(Sender: TObject);
 var prnfile,port:System.Text;
 var buffer:String;
begin
  try
    AssignFile(prnfile, 'c:\file.txt');
    Reset(prnfile);
    AssignFile(port, 'COM3');
    Rewrite(port);

    while not eof(prnfile) do
      begin
        Readln(prnfile, buffer);
        Writeln(port, buffer);
      end;

   finally
     CloseFile(port);
     CloseFile(prnfile);
   end;

   cut();
end;

Anyway, my suggestion is to use a tComPort component instead of a direct use of Writeln. Using a tComPort you can handle the return value from the printer in case of errors like "End Paper", "Printer OffLine" and so on.

Gianluca Colombo
  • 717
  • 17
  • 38
0

You have to send an ESC/POS sequence like this

Definition of cut command:

//ASCII   GS V  m
//Hex     1D 42 m
//Decimal 29 66 m


var cut:String;
begin  
  cut:=Chr(29)+'V'+Chr(66)+Chr(0);
// send this sequence direct to com after the text file
end;

the complete esc/pos code here

Gianluca Colombo
  • 717
  • 17
  • 38
  • 1
    Better hope the user isn't using Unicode Delphi. Strings don't seem like a good idea here. – David Heffernan Jul 26 '15 at 15:18
  • I started to use PrtRaw unit, and can print text or send some commands to printer(#10 or #13), but printer do not accept cut command in any format (#29#86#0, #29#66#0, #29'V'#66#0, etc.). – Mindaugas Jul 27 '15 at 18:02