首页 文章

如何在C#中通过USB使用Zebra RW 420热敏打印机使用命令打印QR码

提问于
浏览
1

在你继续将我的问题标记为重复之前,请相信我,不是 . 我已经完成了这里提供的几乎所有解决方案,但仍然无法让我的应用程序正常工作,所以请你好好花一些时间来帮助我 .

场景:我有一台Zebra RW 420热敏打印机,我想用它来打印带有QR码的代金券 . 我正在使用C#并且已经按照Scott Chamberlain herecode here给出的所有帮助将命令发送到打印机 . 我有EPL2手册以及CPCL和ZPL参考手册,其中包含很多关于如何格式化命令的内容 .

问题:我发送的所有命令都是打印为命令的纯文本副本,或者打印机只是挂起,其显示屏上显示小信息图标 . 我尝试使用Zebra Utilitis发送相同的命令,但仍然得到与我的示例应用程序相同的结果 . 下面是我的代码片段,请告诉我是否有任何参考库我可能需要它才能使其工作 .

private void btnPrint_Click(object sender, RoutedEventArgs e)
    {
        string s = "! 0 200 200 500 1\nB QR 10 100 M 2 U 10\nMA,QR code ABC123\nENDQR\nFORM\nPRINT";

//还尝试了\ r \ n用于具有相同结果的换行符 .

// Allow the user to select a printer.
        PrintDialog pd = new PrintDialog();

        if ((bool)pd.ShowDialog())
        {
            var bytes = Encoding.ASCII.GetBytes(s);
            // Send a printer-specific to the printer.
            RawPrinterHelper.SendBytesToPrinter(pd.PrintQueue.FullName, bytes, bytes.Length);
        }
    }

Scott here修改的PrinterHelper类

public class RawPrinterHelper
{
    // Structure and API declarions:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
    }

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

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

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
        ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level,
        [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

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

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

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

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

    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter(string szPrinterName, byte[] pBytes, Int32 dwCount)
    {
        Int32 dwError = 0, dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.

        di.pDocName = "Zebra Label";
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
            dwError = Marshal.GetLastWin32Error();
            throw new Win32Exception(dwError);
        }
        return bSuccess;
    }

}

2 回答

  • 0

    去年我在Zebra ZT230和GX430t打印机上做了很多工作,而我发现它们是通过端口9100在TCP套接字上使用ZPL指令是非常可靠的 .

    我知道这会让你的谈话朝着一个非常不同的方向发展,但是我尝试使用spool / Win32方法可以告诉你使用套接字更加可靠 . 如果您需要一些示例代码,请告诉我 .

  • 0

    去年使用KR403写了一个信息亭应用程序 . 我能够通过usb使用下面的博客文章成功打印并轮询打印机的状态,以查看是否有卡纸低纸等 .

    http://danielezanoli.blogspot.com/2010/06/usb-communications-with-zebra-printers.html

    使用打印假脱机程序(仅打印)

    https://sharpzebra.codeplex.com/SourceControl/latest#src/Com.SharpZebra/Printing/RawPrinter.cs

    我使用ZebraDesigner进行初始布局 . 在斑马设计器内部的打印屏幕上有一个打印到文件选项,它将您的设计保存为带有ZPL的txt文件 . 然后我把那个文件分成几个部分并创建了一个内部使用StringBuilder的帮助器类,所以我可以专注于zpl的某些部分,因为看起来超过1-2行可能是压倒性的 .

    var kioskTicketBuilder = new KioskTicketBuilder();
    kioskTicketBuilder.SetPrinterDefaults();
    kioskTicketBuilder.DisplayTicketHeader();
    kioskTicketBuilder.DisplayInformationHeading(data.Name, data.todaysDate, data.ClientName, data.ClientCode);
    kioskTicketBuilder.DisplayMoreStuff()
    kioskTicketBuilder.DisplayBarcode(data.TrackingId);
    kioskTicketBuilder.EndOfJob();
    return kioskTicketBuilder.GetPrintJobToArray();
    

    此外,如果您转到打印机属性>打印默认值>工具选项卡有一个选项可以将zpl文件发送到打印机或发送单个命令 . 这非常适合从您的应用程序中测试zpl分离 .

    enter image description here

相关问题