将文本发送到标签打印机根本不工作

时间:2016-01-29 22:20:56

标签: vb.net

一开始我想高兴的是,我已经尝试了很多东西来完成但是没有成功,我真的是堆栈,希望我在这里找到一些帮助。我的客户端有一些'sato'标签打印机放在一些IP地址上,在9100端口上,必须发送一些预定义格式的特定文本数据进行打印。我尝试了几个不同的代码而没有成功(下图) - 从我知道它应该没问题。我没有收到任何错误或者数据正常发送但打印机不想打印。我检查使用一些外部工具,如超级终端检查是否在所有打印机工作 - 它确实并且能够在此ip /端口上发送文件,打印机开始打印。我在做什么可能有什么问题?下面找到我的不同代码尝试。

    Public Sub WriteData(ByVal data As String, ByRef IP As String)
try
        Console.WriteLine("Sending message """ & data & """ to " & IP)
        Dim client As TcpClient = New TcpClient()
        client.Connect(New IPEndPoint(IPAddress.Parse(IP), My.Settings.CommPort))
        Dim stream As NetworkStream = client.GetStream()
        Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(data)
        stream.Write(sendBytes, 0, sendBytes.Length)
   Catch ex As Exception
MsgBox(ex.toString)
        End Try

    End Sub

也尝试了这个:

     ' Printer IP Address and communication port
        Dim ipAddress As String = "192.168.1.21"
        Dim port As Integer = 9100

        ' ZPL Command(s)
        Dim ZPLString As String = ""

        Try
            ' Open connection
            Using client As New System.Net.Sockets.TcpClient()
                client.Connect(ipAddress, port)

                ' Write ZPL String to connection
                Using writer As New System.IO.StreamWriter(client.GetStream())
                    writer.Write(ZPLString)
                    writer.Flush()
                End Using
            End Using
                ' Catch Exception
        Catch ex As Exception
MsgBox(ex.toString)
        End Try

还有这个:

Try
     Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        clientSocket.NoDelay = true;

        IPAddress ip = IPAddress.Parse("192.168.1.21");
        IPEndPoint ipep = new IPEndPoint(ip, 9100);
        clientSocket.Connect(ipep);

        byte[] fileBytes = File.ReadAllBytes("test.txt");

        clientSocket.Send(fileBytes);
        clientSocket.Close();
   Catch ex As Exception
MsgBox(ex.toString)
        End Try

和此:

Try
     Dim clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        clientSocket.NoDelay = True

        Dim ip As IPAddress = IPAddress.Parse("192.168.1.21")
        Dim ipep As New IPEndPoint(ip, 9100)
        clientSocket.Connect(ipep)

        Dim fileBytes As Byte() = File.ReadAllBytes("test.txt")

        clientSocket.Send(fileBytes)
        clientSocket.Close()
   Catch ex As Exception
MsgBox(ex.toString)
        End Try

2 个答案:

答案 0 :(得分:0)

编辑:我已经包含断开/关闭的代码,并处理了流和TcpClient。

对初始代码进行一些修改后,会发送一个完整的文件(如果这是您的标签打印机唯一要求的话,该文件应该有效):

Public Sub WriteFile(ByVal FilePath As String, ByRef IP As String)
    Try
        Console.WriteLine("Sending file """ & FilePath & """ to " & IP)
        Dim client As New TcpClient()
        client.Connect(IP, 9100)

        Dim stream As NetworkStream = client.GetStream()
        Dim sendBytes As Byte() = IO.File.ReadAllBytes(FilePath)

        stream.Write(sendBytes, 0, sendBytes.Length)

        stream.Close(5000) 'Wait 5 seconds to ensure that the data is sent.
        client.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

示例电话:

WriteFile("C:\Users\Vincent\Desktop\Print me.txt", "192.168.1.21")

答案 1 :(得分:0)

我有类似的问题。我必须获取文件信息,将其保存为.CLP文件(我的客户端打印机是Zebra热敏标签打印机)然后我的客户端可以打印文件。

我不知道“佐藤”的格式是什么?打印机使用,但如果您可以创建文本文件并使用该扩展名保存它,那可能会有效。