发送打印机特定的命令 [英] Sending printer specific commands

查看:339
本文介绍了发送打印机特定的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an issue here, which I am trying to encode magnetic stripe data to an Fargo DTC400 printer, in the specifications it says I need to send the following string commands from example notepad, wordpad etc etc :

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

此示例对音轨1中的字符串进行编码,并在音轨2和3中对数字进行编码.这在Notepad.exe中有效.

this example encodes the string in track one, and the numbers 123456789 in both track 2 and 3.. this works from Notepad.exe.

我使用的当前delphi代码可在另一台打印机上使用:

Current delphi code I use works on another printer:

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打印到文件时,我得到的只是二进制数据,根本找不到我的字符串...

when I am trying to encode this string from my own application I get trouble, it seems the printer is totally ignoring my commands, when I choose print to file, I can read the binary data and see my string in the printed file, when I try to print to file from example notepad.exe I get just rubish binary data and cannot find my strings at all...

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

so I wonder what does notepad do to send this string command which I dont ?

希望有人可以阐明这一点,因为我一直渴望在我的应用程序中实现Fargo支持.

hope someone can shed light on this because I have been eager to implement fargo support in my application for a longer period of time .

谢谢

更新. 下面的代码很古老,但是可以完成工作,但是还有另外一种方法可以与上面的Passthrough代码一起使用吗?

Update. the following code is ancient but it does the job, however is there another way I can use this with the Passthrough code above?

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;

推荐答案

TPassThrough应该这样声明:

TPassThrough should be declared like this :

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

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

You might be using a modern Delphi (2009 or newer) or forgotten the packed directive.

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

See also this SO question for a correct-way-to-send-commands-directly-to-printer.

Torry的中有一个示例片段(由Fatih撰写) Ölçer): 备注:修改后也可用于Unicode Delphi版本.

At Torry's there is an example snippet (written by Fatih Ölçer): Remark : Modified for use with Unicode Delphi versions as well.

{
  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;

这篇关于发送打印机特定的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆