AdvancedHMI Software
General Category => Tips & Tricks => Topic started 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.
-
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?
-
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.
-
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
-
I haven't looked at your files yet but error like that is usually resolved by replacing the operator "<>" with "IsNot".
-
Thanks Godra, it worked. I will do further testing with VB version.
-
Attached is a complete working VB code solution for printing to a Zebra printer. It supports printing over TCP, USB, and asynchronous methods. Additionally, it includes error checking for statuses like 'out of paper' and other printer issues.
To use the solution, unzip both ZIP files and move the SDK DLL files into the solution folder. Due to upload file size limitations, the SDK had to be split into two ZIP files.
-
This regular expression is brilliant!
'* 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:
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)
-
Attached is an AOI for string.replace.
-
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....
-
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
-
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.
-
If you download the zip, open the sln w/ notepad, you can read the version from there.
-
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!
-
It's not 2015. I believe it's 2017
-
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).
-
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.
-
an improved version of Search & Replace:
-
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)
/********************************************************************
• .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;
-
I tried this but I'm getting an error in RawPrinterHelper... any ideas?
(http://)
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.
-
Changing from OpenPrinterW DLL call to OpenPrinter2W call seems to have worked... I do not understand what i'm doing here. but i printed something!
I tried this but I'm getting an error in RawPrinterHelper... any ideas?
(http://)
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.
-
NATIVE METHODS > OPENPRINTER>
ByVal pd as Long »»»» ByVal pd as IntPtr
Then, can use OpenPrinterW instead of OpenPrinter2w
Had to be an int p'tr.
Changing from OpenPrinterW DLL call to OpenPrinter2W call seems to have worked... I do not understand what i'm doing here. but i printed something!
I tried this but I'm getting an error in RawPrinterHelper... any ideas?
(http://)
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.