.NET network socket print on Zebra EPL/ZPL

6.5k views Asked by At

I need to print on network Zebra printer. From some reasons, I cannot use winspool printing ( http://support.microsoft.com/kb/154078 ), I have to print print directly through sockets on IP and port. Here is my print method:

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

Problem is, that from time to time "No connection could be created, because target computer actively refused it" exception occurs. I have no idea why is that happening (maybe full printer buffer - and if so, how can I check it in both languages?). So I ask if anybody have had this problem and how can I fix it?

4

There are 4 answers

0
AudioBubble On

I'm not sure if this would apply to you, but I ran into a similar problem using asp classic. I needed to print directly to a zebra printer without changing the default printer, so what I did as a solution was create a java executable that uses sockets to connect to the zebra printer. Once I had the java executable able to send a string of Zpl to the zebra printer through a stream on the open sockets I created a batch file to run my java executable. Since the executable needed a string from my asp page, I added a user input variable to the batch file. I placed these 2 files(the java jar and .bat file) on a shared drive and using ActiveX on the asp page, I was able to send raw bytes in the form of a string directly to my zebra printer. If you have any questions don't hesitate to ask. Below is a link that will help with implementing a way to socket print to a zebra printer through java. https://km.zebra.com/kb/index?page=content&id=SO7149&actp=RSS

0
user1388706 On

This worked for me:

    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");
  }
}
0
Calvin On

Heres my VB code for this.

    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

AND

    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

This is just for printing out text to a s4m printer.

2
cDecker32 On

Try port 9100. And make sure you can see the printers IP on the network.