【问题标题】:Sending printer specific commands发送打印机特定命令
【发布时间】:2012-03-22 07:07:01
【问题描述】:

我这里有一个问题,我正在尝试将磁条数据编码到 Fargo DTC400 打印机,在规范中它说我需要从例如记事本、写字板等发送以下字符串命令:

~1%TRACK NUMBER ONE?
~2;123456789?
~3;123456789?

此示例对轨道 1 中的字符串以及轨道 2 和 3 中的数字 123456789 进行编码。这适用于 Notepad.exe。

编辑: 我使用的当前 delphi 代码在另一台打印机上工作:

procedure SendQuote(MyCommand : AnsiString);
var
  PTBlock       : TPassThrough;

begin
  PTBlock.nLen := Length(MyCommand);
  StrPCopy(@PTBlock.SData, MyCommand);
  Escape(printer.handle, PASSTHROUGH, 0, @PTBlock, nil);
end;

当我试图从我自己的应用程序中编码这个字符串时,我遇到了麻烦,似乎打印机完全忽略了我的命令,当我选择打印到文件时,我可以读取二进制数据并在打印的文件中查看我的字符串,当我尝试从示例 notepad.exe 打印到文件时,我得到的只是 rubish 二进制数据,根本找不到我的字符串......

所以我想知道记事本如何发送这个我不知道的字符串命令?

希望有人能阐明这一点,因为我一直渴望在我的应用程序中实现 fargo 支持很长时间。

谢谢

更新。 下面的代码是古老的,但它可以完成这项工作,但是还有其他方法可以与上面的 Passthrough 代码一起使用吗?

var
  POutput: TextFile;
  k: Integer;
begin
  with TPrintDialog.Create(self) do
  try
    if Execute then
    begin
      AssignPrn(POutput);
      Rewrite(POutput);

      Writeln(POutput,'~1%TESTENCODER?');
      Writeln(POutput,'~2;123456789?');
      Writeln(POutput,'~2;987654321?');
      CloseFile(POutput);
    end;
  finally
    free;
  end
end;

【问题讨论】:

  • 这是一个编程问题网站。请告诉我你在 Delphi 代码中做了什么。你怎么打印?不要让我们猜测你做错了什么。向我们展示您的打印方式。我会像这样测试写入原始打印机端口。有关原始打印信息,请参阅此链接。 efg2.com/Lab/Library/Delphi/Printing/index.html
  • 是的,我忘记粘贴我使用的代码了,抱歉 :) 我现在更新了
  • 使用TextFile 输出有什么问题?似乎是一个清晰而简单的解决方案。
  • TPPassthrough的实现到底是干什么的。你正在展示一些高水平的东西,这里没有人可以告诉你任何事情。你有源代码。跟随它(一步),直到你找到一个 win32 api 调用,或者一些足够低的东西,人们可以帮助你更好地理解它。现在,显示您的一个类调用您的另一个类(TPassthrough - 那是什么?)是无法回答的。
  • @LU RD,如果不是为了一件事,我会使用文本文件。我还需要将位图发送到打印机,并且将其组合起来我认为效果不佳,我的应用程序有两个选项,仅编码和与位图一起编码。单一编码使用文本文件没有问题,但与位图一起我必须与 begindoc 和 enddoc 结合

标签: delphi binary-data magnetic-cards


【解决方案1】:

TPassThrough 应该这样声明:

type 
  TPassThrough = packed record 
    nLen  : SmallInt; 
    SData : Array[0..255] of AnsiChar; 
  end; 

您可能正在使用现代 Delphi(2009 或更新版本)或忘记了打包指令。

另请参阅此 SO 问题以获取 correct-way-to-send-commands-directly-to-printer

Torry's 有一个示例 sn-p(由 Fatih Ölçer 编写): 备注:修改后也可用于 Unicode Delphi 版本。

{
  By using the Windows API Escape() function,
  your application can pass data directly to the printer.
  If the printer driver supports the PASSTHROUGH printer escape,
  you can use the Escape() function and the PASSTHROUGH printer escape
  to send native printer language codes to the printer driver.
  If the printer driver does not support the PASSTHROUGH printer escape,
  you must use the DeviceCapabilities() and ExtDevMode() functions instead.


  Mit der Windows API Funktion Escape() kann man Daten direkt zum Drucker schicken.
  Wenn der Drucker Treiber dies nicht unterstützt, müssen die DeviceCapabilities()
  und ExtDevMode() Funktionen verwendet werden.
}

//  DOS like printing using Passthrough command
// you should use "printer.begindoc" and "printer.enddoc"

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

function DirectToPrinter(S: AnsiString; NextLine: Boolean): Boolean;
var 
  Buff: TPrnBuffRec;
  TestInt: Integer;
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;

// this code works if the printer supports escape commands
// you can get special esc codes from printer's manual

//  example:
printer.BeginDoc;
try
  DirectToPrinter('This text ');
finally
  printer.EndDoc;
end;

【讨论】:

  • 是的,我正在尝试将所有内容都设为 unicode,但这部分并不真正需要 unicode,因为磁条仅限于 ascii 字符......尽管我尝试了这个声明,但没有任何改变......它似乎字符串以不同的方式发送
  • 明天我会看看堆栈页面 :) 现在对我来说太晚了,谢谢大家,将继续测试
  • 我今天一直在测试更多,在另一个程序 notepad++ 中,它似乎也能够对我的打印机进行编码,并且在 Fargo 的支持下,我正在做正确的事情,但这可能是一个角色编码问题?
  • 根据 ascii 表,~ 字符是 #126。其他的介于#32 和#95 之间。我不明白为什么这可能是一个编码问题。
  • 是的,这里也一样。很奇怪,我现在不知道从哪里开始 :( 上面的例程不起作用我在 begindoc 之后和 enddoc 之前发送这个
猜你喜欢
  • 2019-01-27
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
相关资源
最近更新 更多