2

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.

3
  • 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. Commented Jul 26, 2015 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"? Commented Jul 26, 2015 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. Commented Jul 26, 2015 at 15:17

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your help.
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

2 Comments

Better hope the user isn't using Unicode Delphi. Strings don't seem like a good idea here.
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.).
0
  1. I would try to not use any serial component here, as this will only work with serially connected printers
  2. If still going down the serial route I'd add \.\ before the COM3 in the code shown above as that would work with COM port numbers > 10 then.
  3. There is an API in Windows which can be used to send raw commands to a printer. Using that would mean that it won't matter how the printer is connected to the computer.

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.