2014-09-04 19 views
5

Tôi là người lập trình C# mới bắt đầu. Tôi có một dự án yêu cầu tôi gửi lệnh thô đến máy in Zebra LP 2844 qua USB và làm cho nó hoạt động. Tôi đã làm rất nhiều nghiên cứu và cố gắng tìm ra cách để làm điều đó. Tôi đang sử dụng mã từ http://support.microsoft.com/kb/322091, nhưng nó không hoạt động. Dựa trên thử nghiệm của tôi, có vẻ như tôi đã gửi lệnh tới máy in, nhưng nó không trả lời và in. Tôi không có ý tưởng về điều này. Ai đó có thể giúp tôi không?Cách gửi máy in ZPL thô đến máy in zebra bằng C# qua USB

Tôi đang sử dụng nút để gửi lệnh trực tiếp

private void button2_Click(object sender, EventArgs e) 
{ 
    string s = "A50,50,0,2,1,1,N,\"9129302\""; 

    // Allow the user to select a printer. 
    PrintDialog pd = new PrintDialog(); 
    pd.PrinterSettings = new PrinterSettings(); 
    if (DialogResult.OK == pd.ShowDialog(this)) 
    { 
     // Send a printer-specific to the printer. 
     RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s); 
     MessageBox.Show("Data sent to printer."); 
    } 
} 
+0

Điều đó không giống như ZPL, có vẻ như EPL. Các lệnh ZPL thường bắt đầu bằng '^' sau đó là một chữ cái. –

Trả lời

12

EDIT: Để giải quyết cập nhật của bạn, Vấn đề bạn gặp phải là bạn đang sử dụng SendStringToPrinter mà sẽ gửi một chuỗi ANSI (một null chấm dứt) chuỗi đến máy in mà không phải là những gì máy in đang mong đợi. Theo chính thức EPL2 programming guide trang 23 (Đó là những gì bạn đang thực sự làm, không ZPL theo ví dụ của bạn).

Mỗi dòng lệnh phải được chấm dứt bằng ký tự Dòng nguồn (LF) (ngày 10 tháng 12). Hầu hết các hệ thống dựa trên PC đều gửi CR/LF khi nhấn phím Enter là . Ký tự trả về (CR) bị bỏ qua bởi máy in và không thể được sử dụng thay cho LF.

Vì vậy, bạn phải hoặc là sửa đổi SendStringToPrinter để gửi một \n ở phần cuối của chuỗi thay vì một \0 hoặc bạn phải xây dựng các mảng ASCII byte bản thân và sử dụng RawPrinterHelper.SendBytesToPrinter mình (như tôi đã làm trong câu trả lời ban đầu của tôi dưới đây).

Vì vậy, để sửa chữa đơn giản ví dụ được đăng của bạn chúng tôi thay đổi ra gọi hàm của bạn, chúng tôi cũng phải nói với máy in để thực sự in bằng cách gửi một P1\n

private void button2_Click(object sender, EventArgs e) 
{ 
    string s = "A50,50,0,2,1,1,N,\"9129302\"\n"; 
    s += "P1\n"; 

    // Allow the user to select a printer. 
    PrintDialog pd = new PrintDialog(); 
    pd.PrinterSettings = new PrinterSettings(); 
    if (DialogResult.OK == pd.ShowDialog(this)) 
    { 
     var bytes = Encoding.ASCII.GetBytes(s); 
     // Send a printer-specific to the printer. 
     RawPrinterHelper.SendBytesToPrinter(pd.PrinterSettings.PrinterName, bytes, bytes.Length); 
     MessageBox.Show("Data sent to printer."); 
    } 
} 

//elsewhere 
public static class RawPrinterHelper 
{ 
    //(Snip) The rest of the code you already have from http://support.microsoft.com/kb/322091 

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


    private static bool SendBytesToPrinter(string szPrinterName, byte[] bytes, Int32 dwCount) 
    { 
     Int32 dwError = 0, dwWritten = 0; 
     IntPtr hPrinter = new IntPtr(0); 
     DOCINFOA di = new DOCINFOA(); 
     bool bSuccess = false; 

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


     if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) 
     { 
      if (StartDocPrinter(hPrinter, 1, di)) 
      { 
       if (StartPagePrinter(hPrinter)) 
       { 
        bSuccess = WritePrinter(hPrinter, bytes, dwCount, out dwWritten); 
        EndPagePrinter(hPrinter); 
       } 
       EndDocPrinter(hPrinter); 
      } 
      ClosePrinter(hPrinter); 
     } 
     if (bSuccess == false) 
     { 
      dwError = Marshal.GetLastWin32Error(); 
      throw new Win32Exception(dwError); 
     } 
     return bSuccess; 
    } 
} 

Tôi đã làm điều này với ngôn ngữ cũ EPL2 Zebra của, nhưng nó sẽ rất giống với những gì bạn cần làm với ZPL. Có lẽ nó sẽ giúp bạn bắt đầu đi đúng hướng.

public class Label 
{ 
    #region Print logic. Taken from http://support.microsoft.com/kb/322091 

    //Snip stuff unchanged from the KB example. 

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

    private static bool SendBytesToPrinter(string szPrinterName, byte[] Bytes, Int32 dwCount) 
    { 
     Int32 dwError = 0, dwWritten = 0; 
     IntPtr hPrinter = new IntPtr(0); 
     DOCINFOA di = new DOCINFOA(); 
     bool bSuccess = false; 

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


     if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) 
     { 
      if (StartDocPrinter(hPrinter, 1, di)) 
      { 
       if (StartPagePrinter(hPrinter)) 
       { 
        bSuccess = WritePrinter(hPrinter, Bytes, dwCount, out dwWritten); 
        EndPagePrinter(hPrinter); 
       } 
       EndDocPrinter(hPrinter); 
      } 
      ClosePrinter(hPrinter); 
     } 
     if (bSuccess == false) 
     { 
      dwError = Marshal.GetLastWin32Error(); 
      throw new Win32Exception(dwError); 
     } 
     return bSuccess; 
    } 
    #endregion 

    public byte[] CreateCompleteCommand(bool headerAndFooter) 
    { 
     List<byte> byteCollection = new List<byte>(); 

     //Static header content describing the label. 
     if (headerAndFooter) 
     { 
      byteCollection.AddRange(Encoding.ASCII.GetBytes("\nN\n")); 
      byteCollection.AddRange(Encoding.ASCII.GetBytes(String.Format("S{0}\n", this.Speed))); 
      byteCollection.AddRange(Encoding.ASCII.GetBytes(String.Format("D{0}\n", this.Density))); 
      byteCollection.AddRange(Encoding.ASCII.GetBytes(String.Format("q{0}\n", this.LabelHeight))); 
      if (this.AdvancedLabelSizing) 
      { 
       byteCollection.AddRange(Encoding.ASCII.GetBytes(String.Format("Q{0},{1}\n", this.LableLength, this.GapLength))); 
      } 
     } 

     //The content of the label. 
     foreach (var command in this.Commands) 
     { 
      byteCollection.AddRange(command.GenerateByteCommand()); 
     } 

     //The footer content of the label. 
     if(headerAndFooter) 
      byteCollection.AddRange(Encoding.ASCII.GetBytes(String.Format("P{0}\n", this.Pages))); 

     return byteCollection.ToArray(); 
    } 

    public bool PrintLabel(string printer) 
    { 
     byte[] command = this.CreateCompleteCommand(true); 
     return SendBytesToPrinter(printer, command, command.Length); 
    } 

    public List<Epl2CommandBase> Commands { get; private set; } 

    //Snip rest of the code. 
} 

public abstract partial class Epl2CommandBase 
{ 
    protected Epl2CommandBase() { } 

    public virtual byte[] GenerateByteCommand() 
    { 
     return Encoding.ASCII.GetBytes(CommandString + '\n'); 
    } 
    public abstract string CommandString { get; set; } 
} 


public class Text : Epl2CommandBase 
{ 
    public override string CommandString 
    { 
     get 
     { 
      string printText = TextValue; 
      if (Font == Fonts.Pts24) 
       printText = TextValue.ToUpperInvariant(); 
      printText = printText.Replace("\\", "\\\\"); // Replace \ with \\ 
      printText = printText.Replace("\"", "\\\""); // replace " with \" 
      return String.Format("A{0},{1},{2},{3},{4},{5},{6},\"{7}\"", X, Y, (byte)TextRotation, (byte)Font, HorziontalMultiplier, VertricalMultiplier, Reverse, printText); 
     } 
     set 
     { 
      GenerateCommandFromText(value); 
     } 
    } 

    private void GenerateCommandFromText(string command) 
    { 
     if (!command.StartsWith(GetFactoryKey())) 
      throw new ArgumentException("Command must begin with " + GetFactoryKey()); 
     string[] commands = command.Substring(1).Split(','); 
     this.X = int.Parse(commands[0]); 
     this.Y = int.Parse(commands[1]); 
     this.TextRotation = (Rotation)byte.Parse(commands[2]); 
     this.Font = (Fonts)byte.Parse(commands[3]); 
     this.HorziontalMultiplier = int.Parse(commands[4]); 
     this.VertricalMultiplier = int.Parse(commands[5]); 
     this.ReverseImageColor = commands[6].Trim().ToUpper() == "R"; 
     string message = String.Join(",", commands, 7, commands.Length - 7); 
     this.TextValue = message.Substring(1, message.Length - 2); // Remove the " at the beginning and end of the string. 
    } 

    //Snip 
} 
+0

Cảm ơn u !! nó rất cụ thể! Tôi đánh giá cao! –

+0

@ShawnLou nếu tôi trả lời câu hỏi của bạn, vui lòng đánh dấu câu trả lời là đã được chấp nhận. –

Các vấn đề liên quan