Author Topic: Printing to a Zebra Printer from AdvancedHMI  (Read 11422 times)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Printing to a Zebra Printer from AdvancedHMI
« on: September 30, 2018, 05:42:21 AM »
Attached are some code bits from a project I did to print to a Zebra printer. I used Zebra Designer software to create a label, then exported it to a "prn" file. Within the label, I specified variable fields by closing them in brackets. The code would look for those fields and substitute it with the data.

The code does use native methods so it will only work on Windows. It sends information to the printer that has been installed using the Zebra printer driver.

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #1 on: September 30, 2018, 09:18:53 AM »
Thanks for the code , Archie.
It looks like I need to make 2 classes: NativeMethods & RawPrinterHelpers
When I need to print, I would need to call:
RawPrinterHelper.SendStringToPrinter(RawPrinterHelper.DefaultPrinterName(), s)

so on the target PC, I need to setup a default printer by running setup ZebraDesigner software?
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #2 on: September 30, 2018, 09:42:20 AM »
The Zebra Designer software is only used to create a ZPL/PRN file. It is not necessary to use that software, but I tend to favor visual designers over manually writing zpl code.

The target PC only needs the Zebra printer driver installed. If I remember correctly, the name given to the printer in Windows is what the code uses to determine where to send the print output.

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #3 on: October 01, 2018, 09:47:25 PM »
I dig a little further into Zebra's LinkOS SDK. Their SDK can be downloaded directly or use Nuget Package.
I currently have a very good working CSharp solution. It use .NET 4.7 framework.
Basically, I added 3 DLL references (SdkApi_Core, SdkApi_Desktop, SdkApi_Desktop_Usb) and a PrintHelper.cs class

Now, I need to convert the PrintHelper cs class to VB class to use with AAHMI, the online converter converted all of them except one liner. See below



Attached is working CSharp sln and non-working vb class. TIA
« Last Edit: October 01, 2018, 09:57:36 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #4 on: October 01, 2018, 11:59:25 PM »
I haven't looked at your files yet but error like that is usually resolved by replacing the operator "<>" with "IsNot".

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #5 on: October 02, 2018, 07:17:12 AM »
Thanks Godra, it worked. I will do further testing  with VB version. 
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #6 on: October 02, 2018, 08:10:23 AM »
Attached is a complete working code with VB version.
it allows to print over TCP, USB as well as Async methods. it also has error checking status, like out of paper, etc.

You need to grab the DLL files and put it in manually, because of upload file size limit.
« Last Edit: October 02, 2018, 08:13:09 AM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #7 on: October 02, 2018, 01:55:24 PM »
This regular expression is brilliant!

Code: [Select]
       '* Look for fields closed in "[" "]"
        Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(ZPL_STRING, "\[(.*?)\]")

For simpler case, a replace also works:
Code: [Select]
ZPL_STRING = ZPL_STRING.Replace("[SerialNum]", ZPLSubtitute)
The Labelary website is also brilliant and worth mentioning, save a lot of trees.
http://labelary.com/viewer.html
« Last Edit: October 02, 2018, 03:32:48 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
AOI for string.replace
« Reply #8 on: February 09, 2019, 10:15:01 AM »
Attached is an AOI for string.replace.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

aquilmustafa

  • Full Member
  • ***
  • Posts: 121
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #9 on: July 18, 2019, 10:50:06 PM »
Hi,

We did a project in 2015 where we were printing data to Toshiba and Zebra Label Printers and with there *.prn files it was a lot easy

Herein attached is the code for the same

            Dim ToshibaStr As String = "{D0281,0301,0251|}" &
            "{AY;+05,0|}" &
            "{C|}" &
            "{PC000;0023,0056,05,1,J,00,B=EN: " & RFID & "|}" &
            "{PC001;0025,0093,05,1,J,00,B=LV:  " & LeakValueASCII & "|}" &
            "{PC002;0057,0137,1,1,J,00,B=STS: OK|}" &
            "{PC003;0024,0175,05,05,I,00,B=" & DateAndTime.DateString & "|}" &
            "{PC004;0023,0202,05,05,I,00,B=" & DateAndTime.TimeOfDay & "|}" &
            "{PC005;0025,0231,05,05,I,00,B=" & shift & "|}" &
            "{XB00;0168,0155,T,L,04,A,0,M2=" & RFID & ">M" & LeakValueASCII & "|}" &
            "{XS;I,0002,0002C4201|}"


            Dim ip As String = Lip
            Dim port As Integer = CInt(LPort)
            Try
                'Open Connection
                Dim client As New System.Net.Sockets.TcpClient

                client.Connect(ip, port)

                'Write ZPL String to Connection
                Dim writer As New System.IO.StreamWriter(client.GetStream())
                writer.Write(ToshibaStr)
                writer.Flush()

                'Close Connection
                writer.Close()
                client.Close()

This will work with raw ethernet without installing drivers..
The port was "9100"
Hope this helps....
« Last Edit: July 18, 2019, 10:53:10 PM by aquilmustafa »

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #10 on: July 19, 2019, 02:59:49 PM »
In case some one wants the mentioned DLLs but does not want to install the whole SDK, see attached files.

I dig a little further into Zebra's LinkOS SDK. Their SDK can be downloaded directly or use Nuget Package.
I currently have a very good working CSharp solution. It use .NET 4.7 framework.
Basically, I added 3 DLL references (SdkApi_Core, SdkApi_Desktop, SdkApi_Desktop_Usb) and a PrintHelper.cs class

Now, I need to convert the PrintHelper cs class to VB class to use with AAHMI, the online converter converted all of them except one liner. See below



Attached is working CSharp sln and non-working vb class. TIA

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #11 on: August 09, 2019, 10:01:30 AM »
Bachphi, what VS version did you used?

Attached is a complete working code with VB version.
it allows to print over TCP, USB as well as Async methods. it also has error checking status, like out of paper, etc.

You need to grab the DLL files and put it in manually, because of upload file size limit.

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #12 on: August 09, 2019, 04:14:25 PM »
If you download the zip, open the sln w/ notepad, you can read the version from there.
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #13 on: August 12, 2019, 01:44:46 PM »
If you download the zip, open the sln w/ notepad, you can read the version from there.

I see you used 2015. I was trying to use it with 2013 with no sucess, it throws stack errors. I am going to install 2015 and see what happens. Thanks!

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Printing to a Zebra Printer from AdvancedHMI
« Reply #14 on: August 12, 2019, 05:07:49 PM »
It's not 2015. I believe it's 2017
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================