asp.netvb.netbrother-print-sdk

Print to Brother P-Touch using ESC/P from ASP.NET Webforms by IP


I am trying to print calibration labels to a Brother P-Touch P95NW. The printer is configured with a static IP, I can ping it and print (by IP) from the P-Touch Editor.

To print from my webform I am trying to use code that works great for me with several different printers with varying languages.

Based on the printers developers manual, they supply a hex output as an example:

enter image description here

My desired code would send raw output to the printer.

Something like this:

    Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    clientSocket.Connect(New IPEndPoint(IPAddress.Parse(IP), 9100))
    Dim Label As String = "1B 69 61 00 1B 40 1B 69-6C D0 02 1B 24 3C 00 1B" &
                            "6B 00 1B 58 36 41 74 20-69 6F 75 72 20 73 69 64" &
                            "65 0C"
    clientSocket.Send(Encoding.ASCII.GetBytes(Label))
    clientSocket.Close()

When executed there is no reaction from the printer at all.

Brother has some tech video's and in their examples they send the data in a text file, so I tried that. The file referenced has the same content as the string above.

    Dim filePath As String = "\\myserver\Systems\_MISC\bwtest.txt"
    Dim fileReader As String = My.Computer.FileSystem.ReadAllText(filePath, Encoding.ASCII)

    Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    clientSocket.Connect(New IPEndPoint(IPAddress.Parse(IP), 9100))
    clientSocket.SendFile(filePath)
    clientSocket.Close()

Again I get the same result, nothing.

I also tried to use netcat to send the file, again with no results.

At this point I have to assume that I am missing something obvious with my output file or a configuration somewhere. I am printing on a .94" wide label with the same type of orientation as shown in their example.

Any advice appreciated!


Solution

  • So the issue was definitely the format in witch I was sending data to the printer. The printer would not interpret the string and required a byte array to work.

    Something like this works.

        Dim IP As String = "xxx.xx.xx.xx"
        Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        clientSocket.Connect(New IPEndPoint(IPAddress.Parse(IP), 9100))
        Dim Label As String = "1B 69 61 00 1B 40 1B 69-6C D0 02 1B 24 3C 00 1B " &
                                "6B 00 1B 58 36 41 74 20-79 6F 75 72 20 73 69 64 " &
                                "65 0C "
        Dim byteArray() As Byte = HexStringToBytes(Label)
        clientSocket.Send(byteArray)
        clientSocket.Close()
    

    By using a function to do the conversion.

    Function HexStringToBytes(ByVal hexString As String) As Byte()
        Dim result As New List(Of Byte)()
        For i As Integer = 0 To hexString.Length - 1 Step 3
            result.Add(Convert.ToByte(hexString.Substring(i, 2), 16))
        Next
        Return result.ToArray()
    End Function
    

    In a real world application this isn't really practical since making changes would be difficult from the hex string. You can do the conversion from the ESC/p code on the fly as well.

    Dim myText As String = StrToHex("At your side")
    
    Public Function StrToHex(ByRef Data As String) As String
        Dim sVal As String
        Dim sHex As String = ""
        While Data.Length > 0
            sVal = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()))
            Data = Data.Substring(1, Data.Length - 1)
            sHex = sHex & sVal & " "
        End While
    
        Return sHex
    End Function
    

    The biggest hang-up here is realizing that items like FF and ESC are ASCII characters and not the text you are supplied.