直接向打印机发送命令的正确方法!
好的,我是这样做的:
procedure TMainWindow.btnRawPrintClick(Sender: TObject);
begin
BeginPrint;
SendStr(#27#69);
SendStr('MyData');
SendStr(#10);
EndPrint;
end;
procedure TMainWindow.SendStr(Text: String);
var
i: Integer;
data : Array of Char;
begin
for i := 1 to Length(Text) do
begin
SetLength(data,i);
data[Pred(i)] := Text[i];
end;
if (PrintRawData(printHandle,
data,
Length(data)) < 0) then begin
ShowMessage('PrintRawData Failed');
EndRawPrintPage(printHandle);
EndRawPrintJob(printHandle);
exit;
end;
end;
procedure TMainWindow.BeginPrint;
begin
printHandle := StartRawPrintJob('EPSON TM-T70 Receipt','ESDPRT001','Test Document');
if printHandle < 0 then
begin
ShowMessage('StartRawPrintJob Failed!');
exit;
end;
if (StartRawPrintPage(printHandle) < 0) then begin
ShowMessage('StartRawPrintPage Failed!');
EndRawPrintJob(printHandle);
exit;
end;
end;
procedure TMainWindow.EndPrint;
begin
if (EndRawPrintPage(printHandle) < 0) then begin
ShowMessage('EndRawPrintPage Failed');
EndRawPrintJob(printHandle);
exit;
end;
if (EndRawPrintJob(printHandle) < 0) then begin
ShowMessage('EndRawPrintJob Failed');
exit;
end;
end;
我还更改了一些从 此处:
function PrintRawData(hPrn : THandle;
Buffer : pointer;
NumBytes : SpoolInt) : integer;
{$IFDEF WIN32}
var
BytesWritten : DWORD;
{$ENDIF}
begin
NumBytes := NumBytes * 2; //<-- I added this line
...
但是,出现问题,因为某些命令(转义序列)无法按预期工作!
Ok, here is how i do it:
procedure TMainWindow.btnRawPrintClick(Sender: TObject);
begin
BeginPrint;
SendStr(#27#69);
SendStr('MyData');
SendStr(#10);
EndPrint;
end;
procedure TMainWindow.SendStr(Text: String);
var
i: Integer;
data : Array of Char;
begin
for i := 1 to Length(Text) do
begin
SetLength(data,i);
data[Pred(i)] := Text[i];
end;
if (PrintRawData(printHandle,
data,
Length(data)) < 0) then begin
ShowMessage('PrintRawData Failed');
EndRawPrintPage(printHandle);
EndRawPrintJob(printHandle);
exit;
end;
end;
procedure TMainWindow.BeginPrint;
begin
printHandle := StartRawPrintJob('EPSON TM-T70 Receipt','ESDPRT001','Test Document');
if printHandle < 0 then
begin
ShowMessage('StartRawPrintJob Failed!');
exit;
end;
if (StartRawPrintPage(printHandle) < 0) then begin
ShowMessage('StartRawPrintPage Failed!');
EndRawPrintJob(printHandle);
exit;
end;
end;
procedure TMainWindow.EndPrint;
begin
if (EndRawPrintPage(printHandle) < 0) then begin
ShowMessage('EndRawPrintPage Failed');
EndRawPrintJob(printHandle);
exit;
end;
if (EndRawPrintJob(printHandle) < 0) then begin
ShowMessage('EndRawPrintJob Failed');
exit;
end;
end;
Also i changed a little code that i took from here:
function PrintRawData(hPrn : THandle;
Buffer : pointer;
NumBytes : SpoolInt) : integer;
{$IFDEF WIN32}
var
BytesWritten : DWORD;
{$ENDIF}
begin
NumBytes := NumBytes * 2; //<-- I added this line
...
However, something is wrong as some commands (escape sequences) don't work as expected!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用了错误的功能。使用 Escape,将 PASSTHROUGH 标志作为第二个参数。这会将原始的、未处理的转义码直接发送到打印机。
Joe Hecht(Borland 前任成员)多次发布了一个单元,使这变得更容易。我找到了单元PrtRaw 在这里。
You're using the wrong function. Use Escape, passing the PASSTHROUGH flag as the second parameter. This sends the raw, unprocessed escape codes to the printer directly.
Joe Hecht (formerly of Borland) has posted a unit several times that makes this easier. I found unit PrtRaw here.
由于 Ansi 和 Unicode 字符之间的变化,您当前的代码以错误的格式向打印机发送数据。您使用的打印机显然能够容忍一定量的错误,这就是您的某些命令有效的原因,但有一个限制。
在您的 Delphi 版本中,
Char
相当于WideChar
,因此请将您的Char
代码更改为使用AnsiChar
,这样您就可以按照打印机的预期发送一字节字符。您的PrintRawData
函数以前没问题。你的改变是错误的。打印机不期望接收两字节 Unicode 字符,但这就是您所做的更改。恢复原始
PrintRawData
代码后,将SendStr
函数更改为:我对代码进行了以下更改:
Char
数组替换为AnsiString
。PAnsiChar
以传递给PrintRawData
。exit
语句。Your current code is sending data to the printer in the wrong format due to changes between Ansi and Unicode characters. The printer you're using is evidently able to tolerate some amount of error, which is why some of your commands worked, but there's a limit.
In your version of Delphi,
Char
is equivalent toWideChar
, so change yourChar
code to useAnsiChar
instead, so you can send one-byte characters, as the printer expects. YourPrintRawData
function was fine before. Your change is wrong. The printer does not expect to receive two-byte Unicode characters, but that's what your change amounts to.After restoring the original
PrintRawData
code, change yourSendStr
function to this:I made the following changes to the code:
Char
array with anAnsiString
.PAnsiChar
for passing toPrintRawData
.exit
statement when the function is already finished.用法:
Usage: