Unable to connect USB string in Zebra barcode printer

// Expert user has replied.
E Efkan YILMAZ 1 year 11 months ago
26 1 0

Dear Zebra Team,
I had found my USB string by using the following code

public static void Main(string[] args) {
try {
foreach (DiscoveredPrinterDriver printer in UsbDiscoverer.GetZebraDriverPrinters()) {
Console.WriteLine(printer);
}

foreach (DiscoveredUsbPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter())) {
Console.WriteLine(usbPrinter);
}
} catch (ConnectionException e) {
Console.WriteLine($"Error discovering local printers: {e.Message}");
}

Console.WriteLine("Done discovering local printers.");
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
My USB String is "\\?\usb#vid_0a5f&pid_0174#99j210604359#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
private void SendZplOverUSB()
{
Connection thePrinterConn = new UsbConnection(@"\\?\usb#vid_0a5f&pid_0174#99j210604359#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}");
try
{
Console.WriteLine("Finding Zebra printer...!!!");
Console.ReadLine();

ZebraPrinter printer = ZebraPrinterFactory.GetInstance(thePrinterConn);
PrinterStatus printerStatus = printer.GetCurrentStatus();
if (printerStatus.isReadyToPrint)
{
Console.WriteLine("Ready To Print");
Console.ReadLine();
}
else if (printerStatus.isPaused)
{
Console.WriteLine("Cannot Print because the printer is paused.");
Console.ReadLine();
}
else if (printerStatus.isHeadOpen)
{
Console.WriteLine("Cannot Print because the printer head is open.");
Console.ReadLine();
}
else if (printerStatus.isPaperOut)
{
Console.WriteLine("Cannot Print because the paper is out.");
Console.ReadLine();
}
else
{
Console.WriteLine("Cannot Print.");
Console.ReadLine();
}

Console.WriteLine("USB Connection Open...!!!");
Console.ReadLine();
// Open the connection - physical connection is established here.
thePrinterConn.Open();

// This example prints "This is a ZPL test." near the top of the label.
string zplData = @"^XA
^ILR:SAMPLE.GRF^FS
^XZ";

// Send the data to printer as a byte array.
thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));

Console.WriteLine("Write ZPL data to printer");
Console.ReadLine();
}
catch (ConnectionException e)
{
// Handle communications error here.
Console.WriteLine(e.ToString());
Console.ReadLine();
}
finally
{
// Close the connection to release resources.
thePrinterConn.Close();
}
}

Still the above code throws "Connection is not open"
Kindly support me what wrong with this..

Thanks in advance

Please Register or Login to post a reply

1 Replies

S Steven Si

Escape the back slashes in the USB name string when passing it to the UsbConnection(), i.e., with the following code.

Connection thePrinterConn = new UsbConnection(@"\\\\?\\usb#vid_0a5f&pid_0174#99j210604359#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}");

 

CONTACT
Can’t find what you’re looking for?