【问题标题】:.NET network socket print on Zebra EPL/ZPLZebra EPL/ZPL 上的 .NET 网络套接字打印
【发布时间】:2012-06-01 20:26:54
【问题描述】:

我需要在网络 Zebra 打印机上打印。由于某些原因,我不能使用 winspool 打印(http://support.microsoft.com/kb/154078),我必须通过 IP 和端口上的套接字直接打印打印。这是我的打印方法:

System.Net.Sockets.TcpClient zebraClient = new System.Net.Sockets.TcpClient(); 
        try 
        { 
            zebraClient.SendTimeout = 5000; 
            zebraClient.Connect(IP, port);
        } 
        catch (Exception ex) 
        { 
            Utils.ShowError(ex); 
        } 
        if (zebraClient.Connected) 
        { 
            NetworkStream nStream; 
            nStream = zebraClient.GetStream(); 
            StreamWriter wStream; 
            using (nStream) 
            { 
                wStream = new StreamWriter(nStream); 
                using (wStream) 
                { 
                    wStream.Write(content); 
                    wStream.Flush(); 
                } 
            } 
            zebraClient.Close(); 
        } 

问题是,有时会出现“无法创建连接,因为目标计算机主动拒绝它”异常。我不知道为什么会发生这种情况(可能是完整的打印机缓冲区 - 如果是这样,我如何用两种语言检查它?)。所以我问是否有人遇到过这个问题,我该如何解决?

【问题讨论】:

    标签: c# .net zebra-printers zpl-ii epl


    【解决方案1】:

    尝试使用端口 9100。并确保您可以看到网络上的打印机 IP。

    【讨论】:

    • 这个东西设置得很好,一般来说它工作正常。此问题仅不时发生。现在我正在使用一些解决方法 - 如果我捕获套接字异常,我会等待一秒钟然后再试一次。到目前为止它有效,但它不是非常优雅和好的解决方案。
    • 我认为它只允许在每个端口上建立一定数量的连接。所以我的猜测是现有连接太多,所以它会自动拒绝新连接。确保在尝试重新连接之前在 finally{} 语句中关闭连接。
    【解决方案2】:

    这是我的 VB 代码。

        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
    

    这只是为了将文本打印到 s4m 打印机。

    【讨论】:

      【解决方案3】:

      我不确定这是否适用于您,但我在使用 asp classic 时遇到了类似的问题。我需要直接打印到斑马打印机而不更改默认打印机,所以我作为解决方案所做的是创建一个使用套接字连接到斑马打印机的 java 可执行文件。一旦我让 java 可执行文件能够通过打开的套接字上的流向斑马打印机发送 Zpl 字符串,我就创建了一个批处理文件来运行我的 java 可执行文件。由于可执行文件需要我的 asp 页面中的字符串,我在批处理文件中添加了一个用户输入变量。我将这 2 个文件(java jar 和 .bat 文件)放在共享驱动器上,并在 asp 页面上使用 ActiveX,我能够以字符串的形式将原始字节直接发送到我的斑马打印机。如果您有任何问题,请不要犹豫。下面是一个链接,它将帮助实现一种通过 java 将打印套接字打印到斑马打印机的方法。 https://km.zebra.com/kb/index?page=content&id=SO7149&actp=RSS

      【讨论】:

        【解决方案4】:

        这对我有用:

            using System.IO;
            using System.Net;
            using System.Net.Sockets;
            .
            .
            .
            private void btnPrint_Click(object sender, EventArgs e) {
              try {
            string ipAddress = txtIPAddr.Text.ToString(); ; //ie: 10.0.0.91
            int port = int.Parse(txtPort.Text.ToString()); //ie: 9100
        
            System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
            client.Connect(ipAddress, port);
            StreamReader reader = new StreamReader(txtFilename.Text.ToString()); //ie: C:\\Apps\\test.txt
            StreamWriter writer = new StreamWriter(client.GetStream());
            string testFile = reader.ReadToEnd();
            reader.Close();
            writer.Write(testFile);
            writer.WriteLine("Hello World!");
            writer.Flush();
            writer.Close();
            client.Close();
          }
          catch (Exception ex) {
            MessageBox.Show(ex.Message, "Error");
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-07
          • 1970-01-01
          • 1970-01-01
          • 2011-01-03
          • 2023-03-19
          • 1970-01-01
          相关资源
          最近更新 更多