2010-01-11 25 views
36

क्या .NET में प्रिंटर में ZPL (Zebra प्रोग्रामिंग भाषा) भेजने का कोई तरीका है?जेईपीआर प्रिंटर को ZPL भेजने के लिए .NET कोड

मेरे पास डेल्फी में ऐसा करने का कोड है, लेकिन यह सुंदर नहीं है और मैं इसे .NET में पुन: बनाने की कोशिश नहीं करता।

+3

30k विचार? लोग मेरे जैसे होना चाहिए, और आश्चर्य हुआ कि जेपीएल क्या था :)। – kristianp

+3

@ क्रिस्टियनप कोई भी जिसे कभी भी खुदरा सॉफ्टवेयर पर काम करना पड़ता है या बाद में जेपीएल के साथ लड़ाई में समाप्त होता है;) – Vedran

+0

खुदरा या औद्योगिक/विनिर्माण मुद्रण – StingyJack

उत्तर

11

इस धागे को देखें: Print ZPL codes to ZEBRA printer using PrintDocument class

विशेष रूप से ओ पी धागा के जवाब से इस समारोह लेने:

[DllImport("kernel32.dll", SetLastError = true)] 
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, 
uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, 
uint dwFlagsAndAttributes, IntPtr hTemplateFile); 

private void Print() 
{ 
    // Command to be sent to the printer 
    string command = "^XA^FO10,10,^AO,30,20^FDFDTesting^FS^FO10,30^BY3^BCN,100,Y,N,N^FDTesting^FS^XZ"; 

    // Create a buffer with the command 
    Byte[] buffer = new byte[command.Length]; 
    buffer = System.Text.Encoding.ASCII.GetBytes(command); 
    // Use the CreateFile external func to connect to the LPT1 port 
    SafeFileHandle printer = CreateFile("LPT1:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); 
    // Aqui verifico se a impressora é válida 
    if (printer.IsInvalid == true) 
    { 
     return; 
    } 

    // Open the filestream to the lpt1 port and send the command 
    FileStream lpt1 = new FileStream(printer, FileAccess.ReadWrite); 
    lpt1.Write(buffer, 0, buffer.Length); 
    // Close the FileStream connection 
    lpt1.Close(); 

} 
+1

समर्थन के लिए गैर Eng प्रतीक 'कमांड' को यूटीएफ 16 बीई में एन्कोड किया जाना चाहिए। – Alexufo

+0

क्या इस विधि के लिए प्रिंटर ड्राइवर स्थापित करना आवश्यक है? –

9

मैं एक परियोजना है कि साल के लिए सॉकेट के साथ इस करता है प्रबंधित किया है। ज़ेबरा के आम तौर पर बंदरगाह 6101 का उपयोग मैं कोड के माध्यम से देखने के लिए और पोस्ट है कि मैं क्या कर सकते हैं करेंगे।

public void SendData(string zpl) 
{ 
    NetworkStream ns = null; 
    Socket socket = null; 

    try 
    { 
     if (printerIP == null) 
     { 
      /* IP is a string property for the printer's IP address. */ 
      /* 6101 is the common port of all our Zebra printers. */ 
      printerIP = new IPEndPoint(IPAddress.Parse(IP), 6101); 
     } 

     socket = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, 
      ProtocolType.Tcp); 
     socket.Connect(printerIP); 

     ns = new NetworkStream(socket); 

     byte[] toSend = Encoding.ASCII.GetBytes(zpl); 
     ns.Write(toSend, 0, toSend.Length); 
    } 
    finally 
    { 
     if (ns != null) 
      ns.Close(); 

     if (socket != null && socket.Connected) 
      socket.Close(); 
    } 
} 
+4

ज़ेबरा मोबाइल प्रिंटर पोर्ट 6101 (क्यूएल, आरडब्ल्यू, एमजेड, आदि) का उपयोग करते हैं। बड़े प्रिंटर आमतौर पर पोर्ट 9100 का उपयोग करते हैं। –

+1

मैंने ज़ेबरा एलपी 2844 जेड पर प्रिंट करने के लिए एक सरल कंसोल ऐप में इस कोड का उपयोग किया। मुझे पोर्ट को 9100 में बदलना पड़ा। – Monroecheeseman

6

तब से यह पता चला है कि यह अभी भी सी # और जेपीएल के खोज परिणामों में उच्च दिख रहा है, मुझे शार्पजेब्रा का जिक्र करना चाहिए। यह केवल EPL2 है, लेकिन मैं एक update कि सॉकेट के माध्यम से मुद्रण के साथ ZPL समर्थन जोड़ता सबमिट करने के बाद विंडोज स्पूल सेवा और प्रत्यक्ष यूएसबी।

2

वीबी संस्करण (पोर्ट 9100 का उपयोग कर - ज़ेबरा ZM400 पर परीक्षण)

Sub PrintZPL(ByVal pIP As String, ByVal psZPL As String) 
    Dim lAddress As Net.IPEndPoint 
    Dim lSocket As System.Net.Sockets.Socket = Nothing 
    Dim lNetStream As System.Net.Sockets.NetworkStream = Nothing 
    Dim lBytes As Byte() 

    Try 
     lAddress = New Net.IPEndPoint(Net.IPAddress.Parse(pIP), 9100) 
     lSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, _      ProtocolType.Tcp) 
     lSocket.Connect(lAddress) 
     lNetStream = New NetworkStream(lSocket) 

     lBytes = System.Text.Encoding.ASCII.GetBytes(psZPL) 
     lNetStream.Write(lBytes, 0, lBytes.Length) 
    Catch ex As Exception When Not App.Debugging 
     Msgbox ex.message & vbnewline & ex.tostring 
    Finally 
     If Not lNetStream Is Nothing Then 
      lNetStream.Close() 
     End If 
     If Not lSocket Is Nothing Then 
      lSocket.Close() 
     End If 
    End Try 
End Sub 
0

मैं इन दो

Private Sub sendData(ByVal zpl As String) 
    Dim ns As System.Net.Sockets.NetworkStream = Nothing 
    Dim socket As System.Net.Sockets.Socket = Nothing 
    Dim printerIP As Net.IPEndPoint = Nothing 
    Dim toSend As Byte() 

    Try 
     If printerIP Is Nothing Then 
      'set the IP address 
      printerIP = New Net.IPEndPoint(IPAddress.Parse(IP_ADDRESS), 9100) 
     End If 

     'Create a TCP socket 
     socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 
     'Connect to the printer based on the IP address 
     socket.Connect(printerIP) 
     'create a new network stream based on the socket connection 
     ns = New NetworkStream(socket) 

     'convert the zpl command to a byte array 
     toSend = System.Text.Encoding.ASCII.GetBytes(zpl) 

     'send the zpl byte array over the networkstream to the connected printer 
     ns.Write(toSend, 0, toSend.Length) 

    Catch ex As Exception 
     MessageBox.Show(ex.Message, "Cable Printer", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) 
    Finally 
     'close the networkstream and then the socket 
     If Not ns Is Nothing Then 
      ns.Close() 
     End If 

     If Not socket Is Nothing Then 
      socket.Close() 
     End If 
    End Try 
End Sub 


Private Function createString() As String 
    Dim command As String 

    command = "^XA" 
    command += "^LH20,25" 

    If rdoSmall.Checked = True Then 
     command += "^FO1,30^A0,N,25,25^FD" 
    ElseIf rdoNormal.Checked = True Then 
     command += "^FO1,30^A0,N,35,35^FD" 
    Else 
     command += "^FO1,30^A0,N,50,50^FD" 
    End If 

    command += txtInput.Text 
    command += "^FS" 
    command += "^XZ" 

    Return command 

End Function 
32

इस तरह के कॉम्बो का उपयोग आप कोई एक प्रिंटर से ZPL भेजने के लिए सक्षम हो जाएगा बात यह कैसे जुड़ा हुआ है (एलपीटी, यूएसबी, नेटवर्क शेयर ...)

(How to send raw data to a printer by using Visual C# .NET पर Microsoft लेख से) RawPrinterHelper वर्ग बनाएँ:

using System; 
using System.Drawing; 
using System.Drawing.Printing; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

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, IntPtr 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, IntPtr 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 = "My C#.NET RAW Document"; 
     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(); 
     } 
     return bSuccess; 
    } 

    public static bool SendFileToPrinter(string szPrinterName, string szFileName) 
    { 
     // Open the file. 
     FileStream fs = new FileStream(szFileName, FileMode.Open); 
     // Create a BinaryReader on the file. 
     BinaryReader br = new BinaryReader(fs); 
     // Dim an array of bytes big enough to hold the file's contents. 
     Byte []bytes = new Byte[fs.Length]; 
     bool bSuccess = false; 
     // Your unmanaged pointer. 
     IntPtr pUnmanagedBytes = new IntPtr(0); 
     int nLength; 

     nLength = Convert.ToInt32(fs.Length); 
     // Read the contents of the file into the array. 
     bytes = br.ReadBytes(nLength); 
     // Allocate some unmanaged memory for those bytes. 
     pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength); 
     // Copy the managed byte array into the unmanaged array. 
     Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength); 
     // Send the unmanaged bytes to the printer. 
     bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength); 
     // Free the unmanaged memory that you allocated earlier. 
     Marshal.FreeCoTaskMem(pUnmanagedBytes); 
     return bSuccess; 
    } 
    public static bool SendStringToPrinter(string szPrinterName, string szString) 
    { 
     IntPtr pBytes; 
     Int32 dwCount; 
     // How many characters are in the string? 
     dwCount = szString.Length; 
     // Assume that the printer is expecting ANSI text, and then convert 
     // the string to ANSI text. 
     pBytes = Marshal.StringToCoTaskMemAnsi(szString); 
     // Send the converted ANSI string to the printer. 
     SendBytesToPrinter(szPrinterName, pBytes, dwCount); 
     Marshal.FreeCoTaskMem(pBytes); 
     return true; 
    } 
} 

कॉल प्रिंट विधि:

private void BtnPrint_Click(object sender, System.EventArgs e) 
{ 
    string s = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ"; 

    PrintDialog pd = new PrintDialog(); 
    pd.PrinterSettings = new PrinterSettings(); 
    if(DialogResult.OK == pd.ShowDialog(this)) 
    { 
     RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s); 
    } 
} 

हो सकता है जब आप 2 gotchas मैं उस का सामना करना पड़ा हैं प्रिंटर को ZPL कोड के साथ txt फ़ाइलों को भेज रहा है:

  1. फ़ाइल हे रों एक नई लाइन चरित्र
  2. एन्कोडिंग के साथ समाप्त करने के लिए जब विशेष वर्ण

    public static bool SendTextFileToPrinter(string szFileName, string printerName) 
    { 
        var sb = new StringBuilder(); 
    
        using (var sr = new StreamReader(szFileName, Encoding.Default)) 
        { 
         while (!sr.EndOfStream) 
         { 
          sb.AppendLine(sr.ReadLine()); 
         } 
        } 
    
        return RawPrinterHelper.SendStringToPrinter(printerName, sb.ToString()); 
    } 
    
+0

सीधे लूप से SendStringToPrinter प्रति पंक्ति को कॉल करना संभव होना चाहिए स्मृति में पूरी इनपुट फ़ाइल को पढ़ने की आवश्यकता के बिना। (यदि ज़ेबरा प्रिंटर वाला कोई व्यक्ति यह पुष्टि कर सकता है कि यह काम करता है, तो कृपया उत्तर संपादित करें या मुझे बताएं) – Vedran

+0

मैं इसे यूएसबी पर आज़माता हूं, यह ठीक है लेकिन जब मैं एलपीटी में कोशिश करता हूं तो यह प्रिंट नहीं करता था लेकिन किसी भी त्रुटि को फेंक नहीं देता था। कोई भी संभावित त्रुटि? – Vic

+0

हाय, मैंने आपका कोड इस्तेमाल किया लेकिन प्रिंटर कुछ भी प्रिंट नहीं करता है, प्रिंटर स्पूल को अनुरोध भेजता है लेकिन प्रिंट नहीं करता है, क्या आप मदद कर सकते हैं? – Esmigol

2

साथ एएनएसआई txt फ़ाइलें पढ़ने ज़ेबरा समर्थन वेबसाइट पर एक जवाब नहीं है Encoding.Default करने के लिए सेट किया जाना है:

https://km.zebra.com/kb/index?page=content&id=SA301&cat=ZISV_PL_ZPL&actp=LIST

+0

के साथ ऐसा करना संभव नहीं है इस पृष्ठ में कॉपीराइट हैं जो वहां पाए गए कोड की प्रतिलिपि बनाने के लिए अवैध बनाते हैं । (या कम से कम ऐसा लगता है।) – Vaccano

11

सरल समाधान साझा प्रिंटर में फ़ाइलों की प्रतिलिपि के साथ है।सी # में
उदाहरण:

System.IO.File.Copy(inputFilePath, printerPath); 

जहां:

  • inputFilePath - ZPL फ़ाइल का पथ (विशेष विस्तार की आवश्यकता नहीं है);
  • printerPath - साझा प्रिंटर के लिए पथ, उदाहरण के लिए (!): \ 127.0.0.1 \ zebraGX
+0

यह मेरे लिए काम करता था, मैं पहले से ही अपने लेबल के लिए फाइलें बना रहा था। –

+0

यह मेरे लिए vb.net में काम किया। अच्छा और सरल 1 लाइनर। मेरे पास .zpl फ़ाइलें हैं जो मुझे प्रदान की जाती हैं और मुझे उन्हें ज़ेबरा प्रिंटर पर भेजने की आवश्यकता होती है, – Zyren

+0

यह आश्चर्यजनक है, अन्य समाधानों की तुलना में बहुत साफ है। यह EasyPost जैसी सेवाओं के साथ पूरी तरह से काम करता है जो प्रिंटिंग के लिए पहले से ही एक जेपीएल फ़ाइल उत्पन्न करता है। मैं बस "\\ मशीन-नाम \\ प्रिंटर-नाम" के साथ नेटवर्क पर किसी अन्य मशीन से साझा प्रिंटर पर प्रिंट करने में सक्षम था। – blackorchid

8

यहाँ कैसे टीसीपी आईपी प्रोटोकॉल का उपयोग कर यह करने के लिए है:

// Printer IP Address and communication port 
    string ipAddress = "10.3.14.42"; 
    int port = 9100; 

// ZPL Command(s) 
    string ZPLString = 
    "^XA" + 
    "^FO50,50" + 
    "^A0N50,50" + 
    "^FDHello, World!^FS" + 
    "^XZ"; 

    try 
    { 
    // Open connection 
    System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); 
    client.Connect(ipAddress, port); 

    // Write ZPL String to connection 
    System.IO.StreamWriter writer = 
    new System.IO.StreamWriter(client.GetStream()); 
    writer.Write(ZPLString); 
    writer.Flush(); 

    // Close Connection 
    writer.Close(); 
    client.Close(); 
} 
catch (Exception ex) 
{ 
    // Catch Exception 
} 

स्रोत: ZEBRA WEBSITE