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

时间:2015-02-16 17:00:43

标签: c# qr-code zebra-printers

  

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

场景:我有一台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 :(得分:0)

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

我知道这会使你的谈话方向截然不同,但是尝试过假脱机/ Win32方法,我可以告诉你使用套接字更加可靠。如果您需要一些示例代码,请告诉我。

答案 1 :(得分: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文件。然后我拿了那个文件   把它分成几个部分并创建了一个使用a的辅助类   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