You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过C#代码加载预定义.zpl文件配置Zebra ZD220打印机参数

Zebra ZD220 C# 加载ZPL文件配置打印机属性实现方案

前置验证

  • 先确认你的预定义.zpl文件指令有效性:将配置指令(比如标签尺寸^LL、浓度^MD、打印模式^MM等)写入.zpl文件后,可先通过U盘拷入打印机或手动发送指令验证配置生效,排除指令本身错误的问题。
  • 若需要配置永久生效,需在ZPL文件末尾添加^JUS指令,将配置保存到打印机非易失性存储,避免重启后配置丢失。

实现方案

方案1:Windows原生打印API发送(适用USB/共享打印机场景,无需额外依赖)

通过调用Win32打印API将ZPL内容作为原始打印数据发送给打印机,兼容所有已安装Windows驱动的ZD220机型:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

public class ZebraPrinterHelper
{
    // 导入Win32打印API
    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool OpenPrinter(string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool StartDocPrinter(IntPtr hPrinter, int level, IntPtr pDocInfo);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, out int dwWritten);

    // 发送ZPL内容到打印机
    public static bool SendZplToPrinter(string printerName, string zplContent)
    {
        IntPtr hPrinter = IntPtr.Zero;
        int dwWritten = 0;
        bool success = false;

        try
        {
            if (OpenPrinter(printerName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                var di = new DOCINFOA { pDocName = "ZPL Config", pDataType = "RAW" };
                IntPtr pDi = Marshal.AllocHGlobal(Marshal.SizeOf(di));
                Marshal.StructureToPtr(di, pDi, false);

                if (StartDocPrinter(hPrinter, 1, pDi))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        byte[] bytes = Encoding.ASCII.GetBytes(zplContent);
                        IntPtr pBytes = Marshal.AllocHGlobal(bytes.Length);
                        Marshal.Copy(bytes, 0, pBytes, bytes.Length);
                        success = WritePrinter(hPrinter, pBytes, bytes.Length, out dwWritten);
                        EndPagePrinter(hPrinter);
                        Marshal.FreeHGlobal(pBytes);
                    }
                    EndDocPrinter(hPrinter);
                }
                Marshal.FreeHGlobal(pDi);
                ClosePrinter(hPrinter);
            }
            return success;
        }
        catch
        {
            return false;
        }
    }

    // 读取本地ZPL文件并发送
    public static bool SendZplFileToPrinter(string printerName, string zplFilePath)
    {
        if (!File.Exists(zplFilePath)) return false;
        string zplContent = File.ReadAllText(zplFilePath, Encoding.ASCII);
        return SendZplToPrinter(printerName, zplContent);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    private class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }
}

调用示例:

// 替换为你的打印机名称(Windows设备和打印机中显示的完整名称)、ZPL文件路径
bool result = ZebraPrinterHelper.SendZplFileToPrinter("Zebra ZD220", @"C:\config\printer_config.zpl");

方案2:网口直连发送(适用带网络模块的ZD220,无需驱动)

如果打印机配置了网卡,可直接通过Socket连接Zebra默认的9100端口发送ZPL内容:

using System.IO;
using System.Net.Sockets;
using System.Text;

public static bool SendZplFileToNetworkPrinter(string printerIp, int port = 9100, string zplFilePath = "")
{
    if (!File.Exists(zplFilePath)) return false;
    try
    {
        using (TcpClient client = new TcpClient(printerIp, port))
        {
            using (NetworkStream stream = client.GetStream())
            {
                byte[] zplBytes = Encoding.ASCII.GetBytes(File.ReadAllText(zplFilePath, Encoding.ASCII));
                stream.Write(zplBytes, 0, zplBytes.Length);
            }
        }
        return true;
    }
    catch
    {
        return false;
    }
}

常见问题排查

  • 指令发送后不生效:检查ZPL文件编码是否为ASCII,不要使用带BOM的UTF-8编码;确认打印机名称/IP地址填写正确,网络连通性正常。
  • 配置重启后丢失:确认ZPL文件末尾添加了^JUS永久保存指令。

内容的提问来源于stack exchange,提问作者Guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 21:24:03