delphi - 如何从Delphi向POS打印机发送控制命令

标签 delphi printing posprinter

我使用此代码将文本文件打印到 POS 打印机 (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);

文本已打印,但我需要剪一张收据。 我有 EPSON 命令代码,但我不知道如何将它们发送到打印机。 谁能写个例子吗?

谢谢。

最佳答案

我尝试了很多,最后我编写了这段有效的代码:

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;

所以解决办法是:

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;

无论如何,我的建议是使用 tComPort 组件而不是直接使用 Writeln。使用 tComPort,您可以在出现“纸张用完”、“打印机脱机”等错误时处理打印机的返回值。

关于delphi - 如何从Delphi向POS打印机发送控制命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31638043/

相关文章:

delphi - URLOpenBlockingStream 给出相同的页面

delphi - 我应该如何为 TTreeNode 实现 GetLastNode?

delphi - 如何让 TAnimate 的通用 AVI 在 Vista 和 Win7 上运行?

delphi - TDirectoryWatch 第一次未触发

python - 显示金额 - Python

python - 是否可以在不使用 Python 中的打印功能的情况下进行打印?

internet-explorer - IE PrintTemplate 混合上下文警告

C# POS 打印机 API : can't find printer