【发布时间】:2014-05-23 13:56:49
【问题描述】:
我正在使用LibUsbDotNet 与我的 GC420t Zebra 打印机通信。
在打印方面效果很好:
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUsbDevice == null) throw new Exception("Device Not Found.");
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
int bytesWritten;
if (writer.Write(Encoding.Default.GetBytes(someString), 2000, out bytesWritten) != ErrorCode.None)
throw new Exception(UsbDevice.LastErrorString);
但我找不到让我的阅读器代码工作的方法...总是返回读取的 0 字节。我把它放在上面代码的末尾,打开了我的打印机盖(这肯定会给我一些错误代码)。
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
// above code...
ErrorCode ec = ErrorCode.None;
byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;
ec = reader.Read(readBuffer, 5000, out bytesRead);
Console.WriteLine("{0} bytes read", bytesRead);
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
}
如果您知道如何进行这项工作...或者如果您知道更好/更快/更简单的方法来做到这一点,我会接受的,谢谢。
编辑: 所以我尝试了更多的东西,做了更多的研究。
Accessing printer status using winspool
-> 即使我从打印机中取出介质,也返回 0,整洁。好吧,我猜它只是初始化为 0,并且没有收到任何值。此代码使用OpenPrinter/GetPrinter/ClosePrinter 模式。
LibUsbDotNet -> 尝试了所有列出的读取状态的方法,总是读取 0 字节。
RawPrinterHelper -> 适用于打印,但没有找到获取打印机状态的方法。
然后我读到一些东西(不记得在哪个网站上)说您必须在打印机正在打印时读取状态。如何做到这一点?
编辑: 为了完整起见,以下是我为打印机生成命令的方式(这可能无济于事,因为它在打印时再次完美运行):
StringBuilder sb = new StringBuilder().AppendLine()
.AppendLine("N")
.AppendLine("^ee") // The "give me an answer" code, also tested at the end of the commands, or as the only command (with newline and N)
// more appending...
.AppendLine(String.Format("P{0},{1}", 1, 1));
编辑: 只是为了记录,我非常确定您可以获得此 GC420t 状态的原因......是因为您可以使用 Zebra Setup Utilities。如果您使用提供的工具(Open Communication With Printer)发送 ^ee,您将正确获得错误代码。我只需要知道它是如何做到的。
【问题讨论】:
-
什么是ec?什么是读者? 编辑: 通常您可以通过 com 端口进行通信。或者您可以使用 winspool.Drv 将字节发送到打印机
-
糟糕,复制/粘贴太快了。
-
没关系:) 但通常你可以使用 dllimport 和 winspool.Drv 来做到这一点。它工作完美。我已经用斑马标签打印机2844成功了。
-
关于我应该在哪里搜索示例的任何提示?在google D上找不到可以读写的:
-
EPL 编程手册对此提出警告,便携式打印机不支持 ^ee。这个看起来很容易随身携带。打印机手册也没有将 ZPL 命令列为受支持的命令。如果您想确定,请联系供应商。
标签: c# usb polling zebra-printers epl