I'm using LibUsbDotNet to communicate with my GC420t Zebra printer.
It works great when it comes to printing:
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);
But I can't find a way to make my reader code work... Always returns 0 bytes read. I put it right at the end of the above code, having my printer's lid opened (which should surely give me some error code).
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));
}
If you know how to make that work... Or if you know a better/faster/easier way to do that, I'll take it, thanks.
EDIT: So I tried a few more things, did some more research.
Accessing printer status using winspool
-> Returned 0 even though I removed the media from my printer, neat. Well, I guess it's just initialized to 0, and receives no value. This code is using the OpenPrinter/GetPrinter/ClosePrinter
pattern.
LibUsbDotNet -> Tried every listed way to read status, always 0 bytes read.
RawPrinterHelper -> Works for printing, didn't find a way to get my printer's status though.
Then I read something (can't remember on which site) saying you had to read the status WHILE the printer was printing. How can that be done?
EDIT: For the sake of completeness, here's how I generate my commands for the printer (which will probably not help, since, once more, it works flawlessly when it comes to printing):
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));
EDIT: Just for the record, the reason why I'm quite sure you can get this GC420t's status... is because you just can, with Zebra Setup Utilities. If you send ^ee with the provided tool (Open Communication With Printer), you get the error code properly. I just need to know how it does it.
Try to use the above class:
And the call:
fileIn is the escape sequence code.
fileIn should be something like this. N I8,D Q120,24 q245 S4 D12 ZB A100,10,0,3,2,2,N,"A" A140,10,0,3,2,2,N,"B" P1
1
This code works successful 5 years with zebra label printer 2844.
Hope that helps. :)