Diagraph-NGTAPI 1.0.5

dotnet add package Diagraph-NGTAPI --version 1.0.5
                    
NuGet\Install-Package Diagraph-NGTAPI -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Diagraph-NGTAPI" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Diagraph-NGTAPI" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Diagraph-NGTAPI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Diagraph-NGTAPI --version 1.0.5
                    
#r "nuget: Diagraph-NGTAPI, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Diagraph-NGTAPI@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Diagraph-NGTAPI&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Diagraph-NGTAPI&version=1.0.5
                    
Install as a Cake Tool

NGT Demo

This is a simple C# program for interfacing with a Diagraph NGT thermal transfer printer using the NGT library. It demonstrates how to connect to an NGT printer, retrieve information, handle errors, and print or parse labels.

Prerequisites

Before running this program, ensure you have the following prerequisites:

  • An NGT Printer with a reachable IP address.
  • The .NET SDK installed.
  • The NGT Library properly installed and referenced in your project.

Getting Started

  1. Clone this repository to your local machine.

  2. Open the NGT_Demo project in your preferred C# development environment.

  3. Make sure you have the NGT library properly installed and referenced in your project.

  4. Build and run the program.

Usage

This program connects to an NGT printer, retrieves printer information, and manages label data from a specified slot. It provides the following functionalities:

  • Search for available NGT printers on the network and connect to the first one found.
  • Fetch information about the connected printer, including density, speed, air pressure, and voltage.
  • Handle errors on the printer and reset them.
  • Continuously retrieve label data from a specified slot and display information about the label's components, such as barcode type, graphic dimensions, line box dimensions, and text fields.

Code Explanation

  • The program utilizes the NGTPrinterDriver class to interact with the NGT printer.

  • It searches for available printers on the network and connects to the first one found.

  • It fetches information about the connected printer, such as density, speed, air pressure, and voltage.

  • It handles errors on the printer, displaying error details and providing the option to reset them.

  • It continuously retrieves label data from a specified slot, displaying information about the label's components.

using System;
using System.Globalization;
using System.Linq;
using Diagraph.Labelparser.NGT.PrintableElements;
using Diagraph.NGLibrary;
using Diagraph.NGLibrary.NGTModels;

namespace NGT_Demo
{
    public class Program
    {
        private const int Port = 4001;

        public static void Main(string[] args)
        {
            PrintersDemo();
        }

        private static void PrintersDemo()
        {
            Console.WriteLine("Searching printers ...");
            foreach (var printerIp in NGTPrinterDriver.FindPrinterIPs())
            {
                Console.WriteLine(new string('-', 80));
                PrintPrinterInfo(printerIp.Key);
            }

            Console.WriteLine("\nPress any key to continue");
            Console.ReadLine();
        }

        private static void PrintPrinterInfo(string ip)
        {
            var connectionString = $"{ip}:{Port}";
            var driver = ConnectToPrinter(connectionString);

            if (!driver.IsConnected)
            {
                Console.WriteLine($"Not connected to printer: {connectionString}");
                Console.WriteLine($"Reason: {driver.Connection.ConnectionError}");
                return;
            }

            Console.WriteLine($"Connected to printer: {driver.ConnectionString}"); 

            PrintPrinterDetails(driver);

            if (driver.ErrorStatus > 0)
            {
                PrintErrorDetails(driver);
                ResetErrors(driver);
            }
            else
            {
                PrintLabels(driver);
            }

            driver.Disconnect();
        }

        private static NGTPrinterDriver ConnectToPrinter(string connectionString)
        {
            var driver = new NGTPrinterDriver();
            driver.UpdateLanguage(CultureInfo.CurrentCulture);
            driver.Connect(connectionString);
            return driver;
        }

        private static void PrintPrinterDetails(NGTPrinterDriver driver)
        {
            Console.WriteLine($"Density: {driver.Density}");
            Console.WriteLine($"Speed: {driver.Speed}");
            Console.WriteLine($"Air pressure: {driver.AirPressure} bar");
            Console.WriteLine($"Voltage: {driver.Voltage} V");
        }

        private static void PrintErrorDetails(NGTPrinterDriver driver)
        {
            Console.WriteLine("Printer has errors. Error details:");
            foreach (var error in driver.ErrorDetails) Console.WriteLine($"Error: {error}");
        }

        private static void ResetErrors(NGTPrinterDriver driver)
        {
            Console.WriteLine("Resetting errors...");
            driver.ResetError();
        }

        private static void PrintLabels(NGTPrinterDriver driver)
        {
            Console.WriteLine("\nGetting labels...");
            var labelName = "";
            foreach (var label in driver.LabelNames.Where(label => label != PrinterDriver.NOFORMAT))
            {
                labelName = label;
                Console.WriteLine(label);
            }

            var slot = driver.GetSlot(labelName);

            Console.WriteLine();
            while (slot < 1000)
            {
                Console.WriteLine($"Searching label at slot {slot}...");
                var parsedLabel = driver.GetLabel(slot);

                if (!string.IsNullOrEmpty(parsedLabel.LabelName))
                {
                    Console.WriteLine($"Found label {parsedLabel.LabelName}");
                    Console.WriteLine($"Content of label  {parsedLabel.LabelName}");
                    foreach (var element in parsedLabel.GetPrintableElements()) HandlePrintableElement(element);
                    break;
                }

                slot++;
            }
        }

        private static void HandlePrintableElement(BasePrintableElement element)
        {
            if (element is PrintableBarcode barcode)
                Console.WriteLine($"BarcodeType: {barcode.BarcodeType}");
            else if (element is PrintableGraphic graphic)
                Console.WriteLine($"Graphic width x height: {graphic.Height} x {graphic.Width}");
            else if (element is PrintableLineBox lineBox)
                Console.WriteLine($"Line: {lineBox.Height} x {lineBox.Width}");
            else if (element is PrintableText text) Console.WriteLine($"Textfield: {text.Value}");
        }
    }
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

NuGet Package

You can find the NGT library on NuGet: Diagraph-NGTAPI

Issues

If you encounter any issues or have questions, please open an issue.

Enjoy working with your NGT printer!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net is compatible.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios is compatible. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 186 10/13/2023

API update