Pascal
Pascal

Reputation: 2214

Can't find `UsbConnection()` class from Zebra SDK

I installed the Zebra.Printer.SDK via NuGet and included using Zebra.Sdk.Comm; and using Zebra.Sdk.Printer;. I am trying to connect to my printer via USB. In the example they do the following:

} else {
   printerConnection = new UsbConnection(selectedItem.Address);
}

I tried the same in my application. But I can't find the UsbConnection class. Do I need to add additional dependencies?

The SKD reference

SDK Installation which contains the example

Upvotes: 1

Views: 3198

Answers (1)

MAC_ZISV
MAC_ZISV

Reputation: 51

If you installed the Nuget library correctly, you should have the library dependencies needed to run the application. For usb communication, you need the usb dll that allows the interface communication. Please, download the Multiplatform SDK from this Link, it has a folder called PC -.NET, click on it, and then go to “C:\Program Files\Zebra Technologies\link_os_sdk\PC-.NET\v2.15.2634\demos-desktop\Source”, it has a full Visual Studio project that you can install and run immediately. This sample code has all you need, included the dll for usb communication.

For the USB class API documentation please, follow the links below.

Zebra.Sdk.Comm Namespace

https://techdocs.zebra.com/link-os/2-14/pc_net/content/html/85823b27-9fa5-7681-c212-8e536f601bbe.htm

UsbConnection Class

https://techdocs.zebra.com/link-os/2-14/pc_net/content/html/ab837158-704b-90f5-f754-c05091f89421.htm

public UsbConnection(string symbolicName)

Parameters

symbolicName Type: System.String

The USB symbolic name for the device returned by the UsbDiscoverer.GetZebraUsbPrinters() member function.

Example of symbolicName: \?\usb#vid_0a5f&pid_016e#zq520r#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}

private void SendZplOverUsb(string symbolicName) {
 // Instantiate connection for ZPL USB port at given address/symbolicName
    Connection thePrinterConn = new UsbConnection(symbolicName);

    try {
        // 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^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";

        // Send the data to printer as a byte array.
        thePrinterConn.Write(Encoding.UTF8.GetBytes(zplData));
    } catch (ConnectionException e) {
        // Handle communications error here.
        Console.WriteLine(e.ToString());
    } finally {
        // Close the connection to release resources.
        thePrinterConn.Close();
    }
}

Zebra Link-OS - C# view Sample Code

If you do not know your machine's symbolic name, you can attempt to find it using the following code.

using System;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer.Discovery;

public class UsbDiscovererExample {

    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.");
    }
}

code from here.

Upvotes: 5

Related Questions