有没有办法让TcpClient连接不断打开?我有一个应用程序,允许我们的用户扫描纸箱,执行一些数据库更新,并从用户正在使用的无线hip打印机(打印机型号为Zebra QLn420)发送和打印出货标签。
应用程序尝试通过TcpClient连接保持与无线打印机的连接,并在将生成的ZPL发送到打印机进行打印之前,在整个处理过程中进行多次检查以确保连接正常。
我们一直遇到偶尔会丢失标签的问题,似乎只要用户停止扫描几分钟,然后恢复。然而,当标签被跳过时很少见,因此很难再现(我自己无法复制它,但我看到它发生在仓库中)。
我想知道是否有办法确保连接始终打开(通过频繁“ping”设备),或者是否有办法获得数据已收到的反馈并打印。
这是我要求确保连接的代码:
Public Function Connect(strIP As String, intPort As Integer) As Boolean
Try
'connect to printer via TcpClient, need ip address and port number
'connects without thread, hangs program for 10-20 seconds if printer is not turned on, replaced with code below to thread the connection and set timeout
For i As Integer = 1 To 2
If Not (client IsNot Nothing AndAlso client.Connected) Then
'uses ClientSocketParameters structure to pass to recursive function ConnectionReturned()
clntSockParams = New ClientSocketParameters
clntSockParams.addrs = strIP
clntSockParams.prt = intPort
'create client and call BeginConnect (attempts to connect on separate thread until TimeoutTime has elapsed)
client = New System.Net.Sockets.TcpClient
client.SendTimeout = 5000
client.ReceiveTimeout = 5000
'setup timer with timeout length and start, if timer goes past intTimeoutLength, the Timeout() function is called which closes everything and leaves client = Nothing
AddHandler TimeoutTime.Elapsed, AddressOf Timeout
TimeoutTime.Interval = intTimeoutLength
TimeoutTime.Start()
client.BeginConnect(strIP, intPort, New AsyncCallback(AddressOf ConnectionReturned), clntSockParams)
'keeps the program from doing anything else until BeginConnect either succeeds or fails (due to connect on separate thread)
Do While TimeoutTime.Enabled
System.Threading.Thread.Sleep(500)
Loop
End If
'if TimeoutTime is elapsed and client is Nothing, connection didn't happen, throw an error
If client Is Nothing Then
blnConnected = False
Else
blnConnected = True
Exit For
End If
Next
Catch ex As Exception
blnConnected = False
End Try
Return blnConnected
End Function
Private Sub ConnectionReturned(ByVal ar As System.IAsyncResult)
'this method is called from the client.BeginConnect line in Connect(), make sure timer is running
If TimeoutTime.Enabled Then
'ensure client is initialized
If client Is Nothing Then client = New System.Net.Sockets.TcpClient
'keep calling ConnectionReturned until client.Connected is true
If client.Connected Then
TimeoutTime.Stop()
Else
Dim actualParameters As ClientSocketParameters = DirectCast(ar.AsyncState, ClientSocketParameters)
client.BeginConnect(actualParameters.addrs, actualParameters.prt, New AsyncCallback(AddressOf ConnectionReturned), clntSockParams)
End If
End If
End Sub
Private Sub Timeout(ByVal sender As Object, ByVal e As EventArgs)
'this method is only called if TimeoutTime elapsed, which means no connection was made. close the client object if needed, set to Nothing, and stop TimeoutTime
If TimeoutTime.Enabled Then
Try
client.Close()
Catch ex As Exception
End Try
client = Nothing
TimeoutTime.Stop()
End If
End Sub
根据这个问题: tcp client in vb.net not receiving the entire data response data from server TcpClient并不总能保证将所有数据传递到连接的另一端,因此如果有更可靠的连接方法,那么也值得一试。
如果需要更多信息,请与我们联系。谢谢!
最初我通过此链接获得了连接代码。我已经对它进行了修改,因为如果需要更长时间连接,它会将应用程序挂起10-20秒。这里的代码是在C#中,我翻译成VB: Send ZPL Commands via TCP/IP in C#
这是该课程文档的链接: TcpClient Class
答案 0 :(得分:2)
Zebra打印机在TCP上有超时设置,我认为默认设置为3或5分钟。首先要做的是关掉那个超时。还有其他原因导致打印机断开连接,因此您也需要处理它。
将其嵌入您的程序中:
! U1 setvar“wlan.ip.timeout.enable”“off”
确保在该行之前和之后发送CR / LF。
如果您在格式后发送查询,则可以知道整个格式是打印机。以下内容可行:
! U1 getvar“device.uptime”
答案 1 :(得分:0)
这不是一个理智的解决方案。一个理智的解决方案是:如果由于连接失败而没有将标签完全发送到打印机,请建立新连接并发送标签。