AdvancedHMI Software

General Category => Tips & Tricks => Topic started by: Archie on September 30, 2018, 05:42:21 AM

Title: Printing to a Zebra Printer from AdvancedHMI
Post by: Archie 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.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi 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?
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Archie 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.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi 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

(https://i.postimg.cc/85Lb0y1z/Printer_Languague.png)

Attached is working CSharp sln and non-working vb class. TIA
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Godra 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".
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi on October 02, 2018, 07:17:12 AM
Thanks Godra, it worked. I will do further testing  with VB version. 
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi 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.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi 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 (http://labelary.com/viewer.html)
Title: AOI for string.replace
Post by: bachphi on February 09, 2019, 10:15:01 AM
Attached is an AOI for string.replace.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: aquilmustafa 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....
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Noe 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

(https://i.postimg.cc/85Lb0y1z/Printer_Languague.png)

Attached is working CSharp sln and non-working vb class. TIA
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Noe 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.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi 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.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Noe 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!
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi on August 12, 2019, 05:07:49 PM
It's not 2015. I believe it's 2017
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Godra on August 12, 2019, 10:10:35 PM
Sprungmonkey,

Try the attached solution in VS 2013.
Make sure to install Net Framework 4.7.1 Developer Pack first (if you don't have it already installed).
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: Noe on August 15, 2019, 09:31:39 AM
Sprungmonkey,

Try the attached solution in VS 2013.
Make sure to install Net Framework 4.7.1 Developer Pack first (if you don't have it already installed).

Thanks Godra! I will give a try as soon as I can.
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi on January 05, 2020, 01:08:09 PM
an improved version of Search & Replace:
Title: Re: Printing to a Zebra Printer from AdvancedHMI
Post by: bachphi on February 20, 2020, 06:09:59 PM
Another improved Search & Replace version (? may be not),
But it does give the flexibility of using different string size.

(https://i.postimg.cc/RVcHncqs/Search-And-Replace.png)


Code: [Select]
/********************************************************************
.NET equivalent of STRING.REPLACE
Search a string for a specified string and replace it
Added looping capability to replace more than one
Added passing different custom source string size
********************************************************************/

//Determine the size of the passed in array
SIZE(SourceArray,0,array_size);

FOR index:= 0 TO array_size-1 DO
COP(SourceArray[index],sSource.DATA[index],1);
END_FOR;
//
sSource.LEN:= array_size;

WHILE sSource.LEN > sSearch.LEN DO
FIND(sSource,sSearch,1,PosFound);
IF PosFound > 0 then
DELETE(sSource,sSearch.LEN,PosFound,sSource);
INSERT(sSource,sReplace,PosFound,sSource);
ELSE
EXIT;
END_IF;
END_WHILE;

FOR index:= 0 TO sSource.LEN-1 DO
COP(sSource.DATA[index],SourceArray[index],1);
END_FOR;