Abstraction package for TscZebra.Plugin nuget
$ dotnet add package TscZebra.Plugin.AbstractionsThis NuGet package simplifies printer connectivity via TCP connection, enabling querying of printer status and sending print commands 🖨️
It exclusively supports ZPL labels for thermal printers 🔥
This code creates IZplPrinter Instance without connecting
using TscZebra.Plugin;
using TscZebra.Plugin.Abstractions;
IZplPrinter Printer =
PrinterFactory.Create(IPAddress.Parse("127.0.0.1"), 9100, PrinterTypes.Tsc);
try
{
await Printer.ConnectAsync();
} catch (PrinterConnectionException)
{
// connection cannot be established
}
This method request printer for status, every interval seconds. Invokes event OnStatusChanged.
Method auto disabled when printer disabled (always when IZplPrinter throws PrinterConnectionException). Use after ConnectAsync method
Printer.StartStatusPolling(5);
Printer.OnStatusChanged += Receive;
private void Receive(object? sender, PrinterStatus status)
{
// Your logic here
}
Printer.StopStatusPolling();
Printer.OnStatusChanged -= Receive;
This method also invokes event OnStatusChanged
PrinterStatuses StatusByHand = await Printer.RequestStatusAsync();
Before printing, the printer requests its status and triggers all status events.
try {
Printer.PrintZplAsync(string zpl);
} catch {
}
This method validates the ZPL code for printing. If the ZPL code is not valid, it throws a PrinterCommandBodyException
public async Task PrintZplAsync(string zpl)
{
if (!(zpl.StartsWith("^XA") && zpl.EndsWith("^XZ")))
throw new PrinterCommandBodyException();
}
If the printer is successfully connected but cannot print a label (e.g., the head is open), it throws a PrinterStatusException
public async Task PrintZplAsync(string zpl)
{
if (Status is not (PrinterStatuses.Ready or PrinterStatuses.Busy))
throw new PrinterStatusException();
}
Printer.Disconnect();
or
Printer.Dispose();
You can find comprehensive documentation for IZplPrinter in the code.