Author Topic: AdvancedHMI not correct .NET framework for nugget.  (Read 4732 times)

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
AdvancedHMI not correct .NET framework for nugget.
« on: May 08, 2024, 04:41:45 PM »
Is there a way to add AdvancedHMI to a project or does it have to be started using the blank project? Reason being, I am trying to add a Zebra nugget package to a project and keep getting these errors:
Quote
  CACHE https://api.nuget.org/v3/registration5-gz-semver2/zebra.printer.sdk/index.json
Resolved actions to install package 'Zebra.Printer.SDK.3.0.3271'
Install failed. Rolling back...
Package 'Zebra.Printer.SDK.3.0.3271' does not exist in project 'AdvancedHMI'
Package 'Zebra.Printer.SDK.3.0.3271' does not exist in folder 'C:\Users\ashquinn\Desktop\PrintTest\packages'
Executing nuget actions took 1.18 sec
Could not install package 'Zebra.Printer.SDK 3.0.3271'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.8', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
Time Elapsed: 00:00:01.8759248
========== Finished ==========

I am not sure how to get around this, or if there is even a way to. I am trying to print a tag element from my PLC to a Zebra zt410/411 without using the PLC. I was opting for VB to be in control. Has anyone one worked on something similiar to this?

*Edited title to be more accurate*

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #1 on: May 08, 2024, 05:29:36 PM »
Several years ago I did a project that needed to print to a Zebra label printer. I installed the printer driver into Windows and set the zebra as the default printer. From there I send a raw string of data directly to the printer that was in the ZPL format.

Attached is a helper class I used for sending the data to the printer.

The code to send a string to the printer using the helper class:
Code: [Select]
        RawPrinterHelper.SendStringToPrinter(RawPrinterHelper.DefaultPrinterName(), s)

I created a label using the Zebra software and saved it to be used as a template file. I then edited the label file to insert markers where I wanted to substitute data from the PLC. For example, [BatchNumber]

I wrote code to read that template, searched for the markers, then substituted the real data. And finally sending the string directly to the printer.

I can share more of the code, if you are interested
« Last Edit: May 10, 2024, 01:56:32 PM by Archie »

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #2 on: May 09, 2024, 09:34:02 AM »
I think I found a thread on plctalk refering to the project you mentioned. It said you used brackets [] as the container to look for in the zpl, but how did you get the bracket in to the file? Did you just prefill the text with [Variable] and then just replaced the inner string, or you just altered the actual zpl file?

I am very interested on seeing the code for this, I am not well versed on printer communications nor how I can link it to AHMI

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #3 on: May 09, 2024, 10:01:40 AM »
After creating the file using the Zebra designer software, I manually edited with Notepad. Attached is the file.

The first thing my code does is to read data from the PLC and put it in a local variable:
Code: [Select]
        Dim RecipeName As String = ""
        Try
            RecipeName = EthernetIPforCLXCom1.Read("Batch1RecipeName")
        Catch ex As Exception
            MsgBox("Failed to read Batch1RecipeName from PLC. " & ex.Message)
            Exit Sub
        End Try

It then reads the file and stores it in a string:
Code: [Select]
        Dim s As String
        Using sw As New System.IO.StreamReader("BatchTicketLabelFormat.prn")
            s = sw.ReadToEnd()
        End Using

Using Regular Expression, the string is parsed looking for the brackets
Code: [Select]
        Dim x As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(s, "\[(.*?)\]")
        Dim Substitue As String
        While Not String.IsNullOrEmpty(x.Value)
            If x.Value.IndexOf("Batch1RecipeName") >= 0 Then
                Substitue = RecipeName
             Else
                Substitue = "ZZZZZZ"
            End If
            s = s.Substring(0, x.Index) & Substitue & s.Substring(x.Index + x.Length)
            x = System.Text.RegularExpressions.Regex.Match(s, "\[(.*?)\]")
        End While

And finally send the string directly to the printr:
Code: [Select]
        RawPrinterHelper.SendStringToPrinter(RawPrinterHelper.DefaultPrinterName(), s)

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #4 on: May 09, 2024, 02:24:29 PM »
Okay that seems fairly simple. The rawprinterhelper is a tad confusing for me.

Is the rawprinterhelper just placed in a separate module and called when needed? I am looking at the class and trying to understand some segments of the code. 
Would I just change this to what ever I name the ZPL file as, and the data type I assume would remain the same since its send a direct ZPL file?
Code: [Select]
        ' Set up the DOCINFO structure.
        With di
            .pDocName = "Mix Room Label Printer"
            .pDataType = "RAW"
        End With

And as far as the printer name goes, I figure it wouldn't really matter as long as it is the default.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #5 on: May 09, 2024, 06:39:53 PM »
The RawPrinterHelper.vb file is added to the project by right clicking the AdancedHMI project and selecting Add->Existing Item.

You are correct, the printer name does not matter as long as it is default.

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #6 on: May 10, 2024, 08:59:40 AM »
I have plugged everything in, but I am getting an error for the NativeMethods portion.

It is showing that the method isn't accessible because it is a "Friend" though the whole sub is public and its namespace is being imported.

And is the .pDocName suppose to be the name of the format file?

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #8 on: May 10, 2024, 12:55:58 PM »
So I tried using the NativeMethods.VB from the link that was given which fixed the "Friend" issue. However now its throwing an exception saying:
Code: [Select]
MfgControl.AdvancedHMI.NativeMethods::OpenPrinter' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'
Which I have no idea what that means...

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #9 on: May 10, 2024, 01:57:32 PM »
I did not realize I left off the NativeMethods.vb

I edited the post above to add the file. Try to use that one because it came from a working project

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #10 on: May 10, 2024, 02:16:12 PM »
I tried it with the NativeMethods you attached but I am still getting this error:
Quote
Managed Debugging Assistant 'PInvokeStackImbalance'
  Message=Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'AdvancedHMI!MfgControl.AdvancedHMI.NativeMethods::OpenPrinter' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'

Could this just be a formatting issue with the form I am using to create the string that will be placed on the label?

paintman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: AdvancedHMI not correct .NET framework for nugget.
« Reply #11 on: May 10, 2024, 02:46:44 PM »
UPDATE: I had to uncheck the debugger to not break when this exception was thrown 😅 my bad... Thank y'all it works perfectly!!