Support with VB.NET SDK

// Expert user has replied.
E Eduardo Romero 1 year ago
1083 6 0

Hi,

I'm currently in the process of developing a desktop application in VB.NET to interface with Zebra Printers, specifically the ZT410 model. So far, I've managed to achieve printing functionality using ZXing and System.Drawing.Printing in a Windows environment. However, I'm now exploring an alternative approach, which involves accessing a predefined format stored in the printer's memory using ZebraDesigner for Developers. This format contains various variables that I need to populate programmatically.

My challenge at the moment is incorporating the Zebra.SDK for this purpose, and I've encountered some roadblocks in the process. I've included my code below for reference.

Imports System
Imports Zebra.Sdk.Comm
Imports Zebra.Sdk.Printer

Imports System
Imports Zebra.Sdk.Comm
Imports Zebra.Sdk.Printer

Public Class Form1

    Private Sub PrintLabel()


        Dim printerFactory As ZebraPrinterFactory = ZebraPrinterFactory.GetInstance()

        Dim printer As ZebraPrinter = printerFactory.CreateZebraPrinter("ZDesigner ZT410-300dpi ZPL")

        ' Abre la conexión con la impresora
        printer.Open()

        ' Carga el formato de etiqueta guardado localmente en la impresora
        Dim labelFormat As LabelFormat = printer.LoadStoredFormat("R:TEST.ZPL")

Public Class Form1

        Private Sub PrintLabel()
            Dim printerFactory As ZebraPrinterFactory = ZebraPrinterFactory.GetInstance()
            Dim printer As ZebraPrinter = printerFactory.CreateZebraPrinter("ZDesigner ZT410-300dpi ZPL")
            printer.Open()

            Dim labelFormat As LabelFormat = printer.LoadStoredFormat("E:FORMAT1.ZPL")

            labelFormat.SetVariableValue("BARCODE", "1234567890")
            labelFormat.SetVariableValue("LOT", "ABCD")
            labelFormat.SetVariableValue("PARTNUMBER", "1234567890")
            labelFormat.SetVariableValue("REV", "A")
            labelFormat.SetVariableValue("SUPPLIER", "Zebra")


            printer.PrintLabel(labelFormat)

        End Sub

    End Class

 

I would greatly appreciate it if someone could provide a VB code example demonstrating how to establish communication with the printer and send the required variables to the stored format.

Thank you in advance for any assistance you can offer!

 

Please Register or Login to post a reply

6 Replies

S Steven Si

The Zebra.Sdk.Printer module in the Zebra.Printer.SDK library contains a few methods to achieve the objective.

1. FormatUtil.RetrieveFormatFromPrinter() - To retrieve the format stored on the printer.

2. FormatUtil.GetVariableFields() - To get the variables from the format.

3. FormatUtil.PrintStoredFormat() - To print the label by passing the variable values to the stored format

E Eduardo Romero

Thank you, Steve, I have followed your reply and used those methods, but I'm stuck trying to retrieve the formats from the printer. Could you please give me a hand looking to my methods? 
 

I used ZebraDesigner for developers and stored the format in the internal flash. As a reference, this was the ZPL generated (which I think should be the same stored in the printer):

 

CT~~CD,~CC^~CT~
^XA
^DFE:SHIPPING.ZPL^FS
~TA000
~JSN
^LT0
^MNW
^MTT
^PON
^PMN
^LH0,0
^JMA
^PR6,6
~SD30
^JUS
^LRN
^CI27
^PA0,1,1,0
^MMT
^PW900
^LL600
^LS0
^FT42,128^A0N,38,38^FH\^CI28^FDSupplier:^FS^CI27
^FT42,215^A0N,38,38^FH\^CI28^FDPart Number:^FS^CI27
^FT42,320^A0N,38,38^FH\^CI28^FDRev:^FS^CI27
^FT42,434^A0N,38,38^FH\^CI28^FDLot:^FS^CI27
^FT293,128^A0N,38,38^FH\^CI28^FN1"SUPPLIER"^FS^CI27
^FT293,215^A0N,38,38^FH\^CI28^FN2"PARTNUMBER"^FS^CI27
^FT293,320^A0N,38,38^FH\^CI28^FN3"REV"^FS^CI27
^FT293,434^A0N,38,38^FH\^CI28^FN4"LOT"^FS^CI27
^BY4,15^FT515,432^B7N,15,2,0,0,N
^FH\^FN5"BARCODE"^FS
^XZ
^XA
^XFE:SHIPPING.ZPL^FS
^CI27^FN1^FH\^FD^FS
^CI27^FN2^FH\^FD^FS
^CI27^FN3^FH\^FD^FS
^CI27^FN4^FH\^FD^FS
^CI27^FN5^FH\^FD^FS
^PQ1,0,1
^XZ



Attached is my method to get the variable fields from the printer. 

 

    Private Sub PrintBarcodeImage()
        Try
            Dim connection As New UsbConnection("\\?\usb#vid_0a5f&pid_00fa#18j152704918#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}")
            connection.Open()

            Dim getZebraUsbPrinters As List(Of DiscoveredUsbPrinter) = UsbDiscoverer.GetZebraUsbPrinters()

            Console.WriteLine(getZebraUsbPrinters)

            Dim printer As ZebraPrinter = ZebraPrinterFactory.GetInstance(connection)

            Dim printerStatus As PrinterStatus = printer.GetCurrentStatus()

            If printerStatus.isReadyToPrint Then
                Console.WriteLine("Ready to print")
            Else
                Console.WriteLine("Not ready to print")
            End If



            Dim formatData As Byte() = printer.RetrieveFormatFromPrinter("E:SHIPPING.ZPL")
            Console.WriteLine("formatdata: " & formatData.ToString)
            Dim formatString = formatData.ToString
            Dim variableFields As FieldDescriptionData() = printer.GetVariableFields(formatString)




            For Each field As FieldDescriptionData In variableFields
                Console.WriteLine("Field number: " & field.FieldNumber)
                Console.Write("Variable name: " & field.FieldName)
            Next


            connection.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub FindPrinters()
        Try
            For Each printer As DiscoveredPrinterDriver In UsbDiscoverer.GetZebraDriverPrinters()
                Console.WriteLine(printer)
            Next

            For Each usbPrinter As DiscoveredUsbPrinter In UsbDiscoverer.GetZebraUsbPrinters(New ZebraPrinterFilter())
                Console.WriteLine(usbPrinter)
            Next
        Catch e As ConnectionException
            Console.WriteLine($"Error discovering local printers: {e.Message}")
        End Try

        Console.WriteLine("Done discovering local printers.")
        Console.WriteLine()
    End Sub

Thank you!! 
Eduardo

S Steven Si

What is the issue with the RetrieveFormatFromPrinter() API call? Does it return the content of the E:SHIPPING.ZPL template?

E Eduardo Romero

I don't see it returns something, or in the worst case I'm not accessing correctly to the return. The printer returns True for printerStatus.isReadyToPrint. For the API call of RetrieveFormatFromPrinter() I have this in console: 


The thread 0x691c has exited with code 0 (0x0).
ZDesigner ZT410-300dpi ZPL
\\?\usb#vid_0a5f&pid_00fa#18j152704918#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}
Done discovering local printers.

System.Collections.Generic.List`1[Zebra.Sdk.Printer.Discovery.DiscoveredUsbPrinter]
Ready to print
formatdata: System.Byte[]                               (RETRIEVE API)
System.Byte[]                    
Zebra.Sdk.Printer.FieldDescriptionData[]
The thread 0x3634 has exited with code 0 (0x0).
The thread 0x4c4 has exited with code 0 (0x0).
 

S Steven Si

There is a developer demo app that comes with the Link-OS Multiplatform SDK for .NET. It's called DeveloperDemo_Windows under the demos-desktop folder. It has the souce code calling the RetrieveFormatFromPrinter() API. Though the code is in .NET, it provides an example on how the RetrieveFormatFromPrinter() is called. You can actually run the demo in Visual Studio and watch the action. Below is the code snippet.

        private void CreateFormatTableDialog() {
            SetButtonState(retrieveFormatsButton, false);
            SetFormatListState(false);

            string formatName = (string)storedFilesList.SelectedValue;
            Connection printerConnection = null;

            Task.Run(() => {
                try {
                    printerConnection = connectionSelector.GetConnection();
                    printerConnection.Open();

                    ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);
                    byte[] formatData = printer.RetrieveFormatFromPrinter(formatName);

                    fieldDescDataVars = printer.GetVariableFields(Encoding.UTF8.GetString(formatData)).ToList();
                    fieldDescDataVars = FormatFieldDescriptionDataVars(fieldDescDataVars);

                    formatVariables = new ObservableCollection<FormatVariable>();
                    for (int i = 0; i < fieldDescDataVars.Count; ++i) {
                        formatVariables.Add(new FormatVariable { FieldName = fieldDescDataVars[i].FieldName, FieldValue = "" });
                    }

                    try {
                        if (printerConnection != null) {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) { }

                    ShowStoredFormatDialog(formatName);
                } catch (Exception e) {
                    MessageBoxCreator.ShowError(e.Message, "Communication Error");
                    SetButtonState(retrieveFormatsButton, true);
                    SetFormatListState(true);
                } finally {
                    try {
                        if (printerConnection != null && printerConnection.Connected) {
                            printerConnection.Close();
                        }
                    } catch (ConnectionException) { }
                }
            });
        }
E Eduardo Romero

Hey Steven, I really appreciate your time following this thread, thank you! I adapted the code from the demo you shared and is now working correctly! The printer works, the label is printed with the variable values that I send, just 1 issue to solve; the last variable is a PDF-417 barcode format which will have these values: Variable1#TABVariable2#TABVariable3TABVariable4#TAB

All the variables are stored into a dictionary and then passed to the PrintStoredFormat function, which looks like this: 
 

            Dim variablesDictionary As New Dictionary(Of Integer, String) From {
    {1, TextBoxSupplierID.Text},
    {2, TextBoxClientNumber.Text},
    {3, TextBoxRevision.Text},
    {4, TextBoxWorkOrder.Text},
    {5, $"^FH^FN1^FD{TextBoxSupplierID.Text}^FS^FH^FN2^FD{TextBoxClientNumber.Text}^FS^FH^FN3^FD{TextBoxRevision.Text}^FS^FH^FN4^FD{TextBoxWorkOrder.Text}^FS^FH^FN5^FD{vbTab}^FS"}
}

The issue is with the 5th variable, which is the concatenation of the other variables. The barcode is printed with the correct values, but no tab is included on the printing. 

 

Once again, thank you for your help! 

 

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