2012-03-21 137 views
1

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

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

这个例子中编码在轨道之一的字符串,并在这两个轨道2和3 ..这个作品从Notepad.exe的号码123456789。

编辑: 我使用的作品在其他打印机当前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进行打印时,我只会读取二进制数据,根本找不到我的字符串。 ..

所以我想知道记事本做什么发送是字符串命令,我不?

希望有人能够阐明这一点,因为我一直渴望在我的申请中实施更长时间的法戈支持。

感谢

更新。 下面的代码是古老的,但它做的工作,但是有没有另一种方式我可以使用上面的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; 
+3

这是一个编程问题网站。请告诉我你在你的Delphi代码中做了什么。你打印怎么样?不要让我们猜测你做错了什么。向我们展示您的打印方式。我会测试写入这样的原始打印机端口。查看原始打印信息的链接。 http://www.efg2.com/Lab/Library/Delphi/Printing/index.html – 2012-03-21 20:27:30

+1

是的,我忘了粘贴我使用的代码,对不起:)我现在更新了它 – Plastkort 2012-03-21 21:07:32

+0

使用'TextFile'输出有什么问题?看起来像一个清晰简单的解决方案。 – 2012-03-21 21:21:14

回答

4

TPassThrough应声明如下:

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

您可能使用的是现代的德尔福(2009或更新版本)或遗忘的包装指令。

另请参阅这个SO问题correct-way-to-send-commands-directly-to-printer

Torry's有一个示例代码片段(由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; 
+0

是的,我试图让所有的unicode,但unicode不是真的需要这部分,因为磁条仅限于ascii字符......虽然我试过这个声明,但没有改变......它似乎字符串发送不同的方式 – Plastkort 2012-03-21 22:26:12

+1

明天我会看看堆栈页面:)现在对我来说已经太迟了,感谢al现在,将继续测试 – Plastkort 2012-03-21 22:27:06

+0

我今天一直在测试alittlebit今天,在另一个程序中,记事本++它似乎也能够编码我的printern,并从法戈的支持,我做的是正确的事情,但这可能是一个字符编码问题? – Plastkort 2012-03-22 17:24:05