Delphi:PJL 命令发送到打印机并读回响应
将 PJL 发送到打印机时,我很难从打印机获取信息。该打印机支持 PJL,并且是 USB 打印机。现在获取信息/示例似乎是一个问题,或者我看错了地方。我知道 MSDN 上有很多信息,但我已经尝试了从 docinfo 到写入/读取打印机的所有信息,但似乎没有任何效果。
有人说你可以使用 writeprinter 和 readprinter 。我试过这个,当我写打印机时,打印机似乎“做了”一些事情,但读打印机返回或错误或空白。现在我认为这可能是因为打印驱动程序“锁定”端口,因此您无法从中读回信息?
我在某处看到的另一个选项是使用 writefile 和 readfile。在这里,您将获得打印机的物理端口部分,例如“\?\USB#VID_05CA&PID_0403#S5208603411#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}”。然后将端口更改为“FILE”。使用 writefile 和 readfile,路径为上面的物理路径。获取您需要的信息,然后将端口设置回原始端口。这个也试过了,也出现错误。
我只是想做一个简单的@PJL INFO PAGECOUNT(我遗漏了转义字符等)。该字符串是正确的,因为在联网打印机上使用该字符串,它 100% 工作,我可以获取信息。但本地打印机是一个问题。
有没有人有这个工作或工作示例?任何帮助将不胜感激。
PS:下面是我尝试过的 100 个示例中的一个。这是 writeprinter 示例:
procedure TForm1.Button5Click(Sender: TObject);
Const
Defaults: TPrinterDefaults = (
pDatatype : Nil;
pDevMode : nil;
DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER ) ;
Var
Device : array[0..255] of char;
FPrinterHandle:THandle;
DocInfo1: TDocInfo1;
Buffer, Buffer2:PChar;
Written, Len:Cardinal;
i: Integer;
sPath: String;
Begin
StrCopy(Device,PChar('RICOH Aficio SP 4210N PCL 6'));
OpenPrinter(@Device, FPrinterHandle, @Defaults);
DocInfo1.pDocName := 'test';
DocInfo1.pOutputFile := Nil;
DocInfo1.pDatatype := 'RAW';
StartDocPrinter(FPrinterHandle, 1, @DocInfo1);
StartPagePrinter(FPrinterHandle);
Buffer := #27+'%-12345X@PJL COMMENT'+#13+#10+'@PJL INFO PAGECOUNT'+#13+#10+#27+'%-12345X';
WritePrinter(FPrinterHandle,@Buffer,Length(Buffer), Written);
EndPagePrinter(FPrinterHandle);
EndDocPrinter(FPrinterHandle);
// everithing is OK here, BUT
ReadPrinter(FPrinterHandle, @Buffer2, Length(Buffer2), len );
end;
Im struggling with getting information back from the printer when sending PJL to the printer. The printer supports PJL and it is a USB printer. Now getting information / examples seems to be a problem or I'm looking at the wrong places. I know on MSDN there are a lot of information, but I've tried everything I got there, from docinfo's to write/read printers and nothing seems to work.
Some people say you can use writeprinter and readprinter. I've tried this, when I writeprinter, the printer seems to "do" something, but readprinter returns or errors or blanks. Now I think this could be because the print driver is "locking" the port, so you cant read information back from it?
The other option I saw somewhere is to use writefile and readfile. Here you would get the physical port part for the printer, for example '\?\USB#VID_05CA&PID_0403#S5208603411#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}'. Then you change the port to "FILE". Use the writefile and readfile with the path as the physical path from above. Get the information you need and then set the port back to the original port. Tried this as well, also getting errors.
Im just trying to do a simple @PJL INFO PAGECOUNT (I left out the escape chars etc, etc). The string is correct, because using the string on networked printers, its working 100% and I can get the information. But local printers is a problem.
Is there anyone that has this working or a working example? Any help would be much appreciated.
PS: Below is 1 of the 100's of examples I've tried. This is the writeprinter example:
procedure TForm1.Button5Click(Sender: TObject);
Const
Defaults: TPrinterDefaults = (
pDatatype : Nil;
pDevMode : nil;
DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER ) ;
Var
Device : array[0..255] of char;
FPrinterHandle:THandle;
DocInfo1: TDocInfo1;
Buffer, Buffer2:PChar;
Written, Len:Cardinal;
i: Integer;
sPath: String;
Begin
StrCopy(Device,PChar('RICOH Aficio SP 4210N PCL 6'));
OpenPrinter(@Device, FPrinterHandle, @Defaults);
DocInfo1.pDocName := 'test';
DocInfo1.pOutputFile := Nil;
DocInfo1.pDatatype := 'RAW';
StartDocPrinter(FPrinterHandle, 1, @DocInfo1);
StartPagePrinter(FPrinterHandle);
Buffer := #27+'%-12345X@PJL COMMENT'+#13+#10+'@PJL INFO PAGECOUNT'+#13+#10+#27+'%-12345X';
WritePrinter(FPrinterHandle,@Buffer,Length(Buffer), Written);
EndPagePrinter(FPrinterHandle);
EndDocPrinter(FPrinterHandle);
// everithing is OK here, BUT
ReadPrinter(FPrinterHandle, @Buffer2, Length(Buffer2), len );
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 http://www.undocprint.org/winspool/tips_and_tricks 了解操作说明,以及一些示例 C 代码。
即使使用此代码,对您有用的机会也很小。为了能够从打印机读回,端口监视器必须支持双向模式,而标准 USB 端口监视器则不支持。
另外,在上面的代码中,传递给 ReadPrinter() 的 Buffer2 不正确。您需要预先分配缓冲区空间,然后传递缓冲区的地址,而不是指向缓冲区的指针的地址...
Check http://www.undocprint.org/winspool/tips_and_tricks for an explanation of what to do, and some sample C code.
Even with this code the chances of this working for you are minimal. To be able to read back from the printer, the port monitor must support bidirectional mode, and the standard USB port monitor does not.
Also, in your code above, Buffer2 passed to ReadPrinter() is not correct. You need to pre-allocate buffer space, and then pass the address of the buffer, not the address of the pointer to the buffer...