【问题标题】:print pdf417 barcode in zebra printer using c# and zpl使用 c# 和 zpl 在斑马打印机中打印 pdf417 条码
【发布时间】:2016-05-25 16:59:15
【问题描述】:

我尝试向 Zebra 打印机发送命令。我使用 RawPrinterHelper 类 和 SendStringToPrinter 函数,但问题是没有输入任何内容。我从斑马手册中得到命令这是我的代码:

public class RawPrinterHelper
{
    [DllImport(
        "winspool.Drv",
        EntryPoint = "OpenPrinterA",
        SetLastError = true,
        CharSet = CharSet.Ansi,
        ExactSpelling = true,
        CallingConvention = CallingConvention.StdCall)]
    private static extern bool OpenPrinter(
        [MarshalAs(UnmanagedType.LPStr)] string szPrinter,
        out IntPtr hPrinter,
        Int32 pDefault);


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

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterW", SetLastError = true, CharSet = CharSet.Unicode,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref DOCINFOW pDI);

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

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

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

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

    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount)
    {
        var hPrinter = (IntPtr)(0);
        int dwError; // Last error - in case there was trouble.
        var di = new DOCINFOW();
        int dwWritten = 0;

        // Set up the DOCINFO structure.
        di.pDocName = "My Visual Basic .NET RAW Document";
        di.pDataType = "RAW";
        // Assume failure unless you specifically succeed.
        bool bSuccess = false;
        if (OpenPrinter(szPrinterName, out hPrinter, 0))
        {
            if (StartDocPrinter(hPrinter, 1, ref di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    // Write your printer-specific bytes to the printer.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }

        if (bSuccess == false)
        {
            Marshal.GetLastWin32Error();
        }
        return bSuccess;
    }

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        var fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        var br = new BinaryReader(fs);
        // Dim an array of bytes large enough to hold the file's contents.

        byte[] bytes = br.ReadBytes((int)fs.Length);

        IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem((int)fs.Length);

        Marshal.Copy(bytes, 0, pUnmanagedBytes, (int)fs.Length);

        bool bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, (int)fs.Length);

        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }


    public static object SendStringToPrinter(string szPrinterName, string szString)
    {

        int dwCount = szString.Length;

        IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);

        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        Marshal.FreeCoTaskMem(pBytes);
        return null;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct DOCINFOW
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pOutputFile;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string pDataType;
    }
}

在这里我尝试输入我的命令

        var doc = new PrintDocument();
        doc.PrintPage += (s, e) => ZebraPrinter.PrintCoupon(Points, offerName, Description, Barcode, expDate, e);

        doc.PrinterSettings.PrinterName = "Zebra TTP 2030";
        doc.PrintController = new StandardPrintController();

        if (doc.PrinterSettings.IsValid)
        {
          doc.Print();
           string command = "^XA^BY2,3^FO10,10^B7N,5,5,,83,N^FDYourTextHere^FS^XZ";

            RawPrinterHelper.SendStringToPrinter(doc.PrinterSettings.PrinterName,command);
        }

}

但不打印任何内容。

任何想法;

【问题讨论】:

  • 您的 ZPL 代码看起来不错,您能否确认代码已到达打印机?

标签: c# printing zebra-printers zpl


【解决方案1】:

我在尝试自己连接 TTP-2030 时发现了这篇文章。我开始怀疑 2030 是否真的支持 ZPL。也许我们被 KPL 语言困住了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多