2015-06-25 91 views
1

我尝试向Zebra打印机发送命令。我使用RawPrinterHelper类 和SendStringToPrinter函数,但是proplem是没有输入。我从斑马手册得到了命令,这里是我的代码:使用c#和zpl在斑马打印机中打印pdf417条码

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); 
     } 

}

但不打印什么。

任何想法;

+0

您的ZPL代码看起来不错,您可以确认代码是否已到达打印机? –

回答

0

我发现这篇文章,因为我试图自己连接TTP-2030。而且我开始怀疑2030实际上是否支持ZPL。也许我们坚持使用KPL语言。