4

我需要在网络 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(); 
        } 

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

4

4 回答 4

0

这是我的 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 打印机。

于 2012-07-09T22:27:13.007 回答
0

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

于 2012-06-12T19:19:42.207 回答
0

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

于 2016-06-21T04:53:55.620 回答
0

这对我有用:

    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");
  }
}
于 2020-05-02T09:34:03.207 回答