Files
FATrace/FATrace.App/ZebraPrint.cs
2025-11-11 17:26:31 +08:00

145 lines
8.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using FATrace.App.UniCodeToZPL;
using System.Text;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
namespace FATrace.App
{
/// <summary>
/// 斑马打印
/// </summary>
public class ZebraPrint
{
// 打印机连接参数
private readonly string _ip;
private readonly int _port;
private readonly int _readTimeoutMs = 10000;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">打印机 IP 地址</param>
/// <param name="port">打印机端口(通常为 9100</param>
public ZebraPrint(string ip, int port)
{
if (string.IsNullOrWhiteSpace(ip))
{
throw new ArgumentException("打印机 IP 不能为空", nameof(ip));
}
if (port <= 0 || port > 65535)
{
throw new ArgumentOutOfRangeException(nameof(port), "端口范围必须在 1-65535 之间");
}
_ip = ip;
_port = port;
}
// 使用按需连接模式,每次打印时建立并关闭连接。
/// <summary>
/// 发送Zpl指令进行打印
/// </summary>
/// <param name="zplStr">ZPL 指令字符串</param>
public void PrintZpl(string zplStr)
{
if (string.IsNullOrWhiteSpace(zplStr))
{
throw new ArgumentException("ZPL 指令不能为空", nameof(zplStr));
}
TcpConnection connection = null;
try
{
connection = new TcpConnection(_ip, TcpConnection.DEFAULT_ZPL_TCP_PORT);
connection.MaxTimeoutForRead = _readTimeoutMs;
connection.Open();
if (!connection.Connected)
{
throw new InvalidOperationException($"无法连接到打印机 {_ip}:{TcpConnection.DEFAULT_ZPL_TCP_PORT}");
}
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(PrinterLanguage.ZPL, connection);
printer.SendCommand(zplStr);
}
catch (ConnectionException ex)
{
throw new InvalidOperationException($"与打印机 {_ip}:{_port} 通信失败:{ex.Message}", ex);
}
catch (Exception ex)
{
throw new InvalidOperationException($"发送 ZPL 指令到打印机失败:{ex.Message}", ex);
}
finally
{
try
{
connection?.Close();
}
catch
{
// 忽略关闭异常
}
}
}
/// <summary>
/// 打印称重信息
/// </summary>
public void PrintWeight(string Code, string RawName, double Weight, string Batch, int ShelfLife)
{
////二维码数据
//var Code = new StringBuilder();
//Code.Append(Batch);
//Code.Append(',');
//Code.Append(Weight.ToString().Replace(".", ""));
//Code.Append(',');
//Code.Append(ShelfLife.ToString());
//Code.Append(',');
//Code.Append(GetSourceInf(Source));
//Code.Append(',');
//Code.Append(DayCount.ToString());
var RawNameUnicodeZplStr = UnicodeToZPLHelper.CompressZPL(RawName, "RawName", new Font("宋体", 22f, FontStyle.Bold), TextDirection.Degree0);
var PrintZpl = new StringBuilder();
PrintZpl.AppendLine("~DGRawNameCN.GRF,1647,27,k0Y01K08K08M08R01g038J0EK0FL01CP0601CR0EL07CJ0FK0FL01FO01F01ER0OFJ0EK0EL03CO0HF83CR07R0EK0EL038N0HF803CR07I03N0E1J0EL078I0CI0HFCH038R07I07CK040E186H0EL07I01EH0101CH078R07I07L060E3E380EL0LF8I01CH07S07I06L070E381E0EL0EI01EJ01CH07I02O07I0CH02I038E3H0F0EK01CI03CJ01CH0EI07O070608H07I038E7H0F0EK038I078J01CH0KF8N0707KF8H03CE6H070EK07J078J01CH0EI0F8N0707J07I018E4H070EK07J0FK01CE1CI0EO0707J07I018ECH020EK0FCH01EK01DE18181CO0707J07K0E8J0EJ01CFH03CI03JFB81E18O0707J07K0E18I0EJ03078078K03C0301C3P0707J07K0E3CI0EJ0603C0FL03C0601C2P0707KFH01KFI0EJ0C03C1EL03C0601CQ0707J07J01EH06H0EI01801C3CL07C0C01CQ0707J07J03EH0380EI02H01878L07F0801CQ0707J07J03EH03C0EN0FM07D8061CQ0707J07J03FH01E0EM01EM0FDE079D8P0707J07J07FCH0F0EM07CM0FCF0F1HCP0607J07J07EFH0E0EM0FM01DC70F1C6P0607KFJ07E78060EL0DEI03I01DC70E1C7P060701C07J0HE3C060E3K0F8I038H039C31E1C38O0E0601C06J0CE1CI0E38I01LFCH039C31C1C38O0EI01CK01CE1CI0E7CI03EJ038H071C01C1C1CO0EI01CK018E08I0FCJ0FEJ038H061C0381C1EO0EH0C1CK030EI03FEJ03CEJ038H0E1C0301C0FO0EH0E1HCJ070EH07F0EJ0F0EJ038H0C1C0701C0FO0C01F1C78I060E3FCH0EI03C0EJ0380181C0601C078N0C03C1C1EI0C0E0CI0EI0EH0EJ0380301C0C01C078M01C0781C0F80180EK0EH03I0EJ0380201C1801C038M01C0F01C07E01H0EK0EL0EJ038I01C1801C038M0181C01C03F02H0EK0EL0EJ038I01C3H01C03N0383801CH0FJ0EK0EL0EJ038I01C6H01CP0307H01CH0FJ0EK0EL0EJ038I01CI01CP060C063CH07J0EK0EL0EJ038I01CH0E1CP061803FCH03J0EK0EL0LF8I01CH03FCP0C2I0F8M0EK0EL0EJ038I01CH01F8P08J078M0EK0EL0EJ03J01CI0FP01K02N0CK0EL0CO01J06g0K08K08z0z0h0");
PrintZpl.AppendLine("~DGWeightCN.GRF,407,11,h0P06L01S07FI0JF8O03HF8J0CH01R01K0JFR01H0CH0CH01P0KFEH0CH01R01K0JFR0102I0CH01P01JFM018N01810201LFC06L018102P0FL018102I0JF8H06L01IFEI0C0818O018102I0C0818O018103I0C0818O01JFI0JF8O018103I0C0818Q01K0JF8Q01018H0C081P07JFCJ08S01M080CH06N01J03JFEH0FN01H07J08J06K03LF8I08018U0LFCq0L0");
PrintZpl.AppendLine("~DGBatchCN.GRF,407,11,g0J01g0G01CO01P018608I07IF8O01870EI06H03P01860CI06H03P01B60CI06H03O03HFE0CI06H03P01860CI06H03P01860C3H07IFP018H6C7806H03P0187FC6O06L01BE0HCL03H0FL01E60FH0LF806L03860EJ0CQ01F860CJ0CQ039860CI018018N0H1860CI03IFCO01860CI01H018O01860CL038O01860C1K03P01861C1K03P018H6C1K03I06L0186HC1K03I0FL01878C1I0106I06K01F870E38I0FEP070207FJ03CP02O03p0M0");
PrintZpl.AppendLine("~DGShelfLifeCN.GRF,814,22,h0M04O018g0R07I08K0FEH0C1M08K0806R0E7HFC0107F8I0E18H08H01801IFC03R0C601801F8K0C18HFCH03H018018018Q0C601801807J0C18C0CH06H018018H08P018601801806I07IFC0CH04H018018H0CP01860180180606H0C18C0CH0CH018018H06P030601801KFH0C18C0CH08H018018H06P038601801806J0C18C0C018H01IF8H03P0707HF801806J0HF8HFC018H018018H03P07063180180608H0C18C0C03I018018H03I06L0DH03I019IFCH0C18C0C03I018018H03I0FL09H03I0198H08H0C18C0C03I018018H03I06K0H1H03030198408H0HF8C0C03I018018H03O0217JF8198708H0C18C0C03I018018H03P01H07I0198608H0C18HFC03I01IF8H03P01H0F4H0H18608H0C1HC0C03I018018H03P01H0F6H031860C07IFC0C018H03H018H03P0101B2H031860CK0C0C018H03H018H03P010I3H0318C0CH0620C0C018H03H018H06P010H3180218C08H073980CH0CH03H018H06P010631C0601F8I0E1D80CH04H06H018H0CI06L010C30E0401878H0C0DH0CH06H06H018H08I0FL0H18307H80701E0180BH0CH03H0CH018018I06L0130302080CH0E02H060F8H0181803F803Q01403H0103I0604H0C038I083I07806Q03H02J0CI02I01H01K04I02z0N0");
PrintZpl.AppendLine("^XA\r\n~TA000\r\n~JSN\r\n^LT0\r\n^MNW\r\n^MTT\r\n^PON\r\n^PMN\r\n^LH0,0\r\n^JMA\r\n^PR4,4\r\n~SD15\r\n^JUS\r\n^LRN\r\n^CI27\r\n^PA0,1,1,0\r\n^XZ\r\n^XA\r\n^MMT\r\n^PW472\r\n^LL354\r\n^LS0\r\n^FT275,366^BQN,2,7\r\n^FH\\^FDLA,");
PrintZpl.AppendLine(Code.ToString());
PrintZpl.AppendLine("^FS\r\n^FO150,10^XG");
PrintZpl.AppendLine("RawNameCN");
PrintZpl.AppendLine("^Fs");
PrintZpl.AppendLine("^FO110,65^GFA,");
PrintZpl.AppendLine(RawNameUnicodeZplStr);
PrintZpl.AppendLine("^FS\r\n^FO8,166^XG");
PrintZpl.AppendLine("WeightCN");
PrintZpl.AppendLine("^Fs\r\n^FT89,191^A0N,29,48^FH\\^CI28^FD");
PrintZpl.AppendLine(Weight.ToString() + "g");
PrintZpl.AppendLine("^FS^CI27\r\n^FO8,215^XG");
PrintZpl.AppendLine("BatchCN");
PrintZpl.AppendLine("^Fs\r\n^FT92,241^A0N,29,41^FH\\^CI28^FD");
PrintZpl.AppendLine(Batch);
PrintZpl.AppendLine("^FS^CI27\r\n^FO8,267^XG");
PrintZpl.AppendLine("ShelfLifeCN");
PrintZpl.AppendLine("^Fs\r\n^FO4,3^GB465,0,4^FS\r\n^FO2,3^GB0,349,4^FS\r\n^FO5,348^GB464,0,4^FS\r\n^FO467,3^GB0,349,4^FS\r\n^FT25,125^A0N,28,30^FH\\^CI28^FD");
PrintZpl.AppendLine(Code.ToString());
PrintZpl.AppendLine("^FS^CI27\r\n^FT175,293^A0N,28,48^FH\\^CI28^FD");
PrintZpl.AppendLine(ShelfLife.ToString());
PrintZpl.AppendLine("^FS^CI27\r\n^PQ1,0,1,Y\r\n^XZ");
//var dd= PrintZpl.ToString();
this.PrintZpl(PrintZpl.ToString());
}
}
}