我通过TCP从相机读取数据,我首先通过TCP发送触发器拍照,然后我读取相机图像。在完成296次触发之前,一切都会被罚款,从那一刻起,程序在第一次TCP读取时被阻止。
我可以通过再次发送一个触发器(来自另一个程序)来临时修复此问题。我相信在那一刻相机会被触发2次(可能是被阻挡的一个和新的触发器(要修复))。
有人知道会出现什么问题吗?谢谢!
'Code in Timer
'Trigger
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("ip", port)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite Then
networkStream.Write(dataTrigger, 0, n)
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Console.WriteLine("cannot read data from this stream")
tcpClient.Close()
End If
End If
networkStream.Close()
'Read images
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect("ip", port)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanRead Then
' Reading data length
Dim dataLength(15) As Byte
networkStream.Read(dataLength, 0, 16)
' Reading all data
Dim bytesReaded, bytesLeft, bytesCounter As Integer
bytesCounter = 0
bytesLeft = dataLength
Do While bytesLeft > 0
bytesReaded = networkStream.Read(dataImages, bytesCounter, bytesLeft)
bytesCounter = bytesCounter + bytesReaded
bytesLeft = dataLengthInt - bytesCounter
Loop
tcpClient.Close()
Else
If Not networkStream.CanRead Then
Console.WriteLine("cannot not write data to this stream")
tcpClient.Close()
End If
End If
networkStream.Close()
答案 0 :(得分:0)
networkStream.Read(dataLength, 0, 16)
无法保证在单次读取中读取所有数据。你需要循环,直到你有16个字节,就像你的主循环一样。
您也bytesLeft = dataLengthInt - bytesCounter
但不在代码中的任何位置为dataLengthInt
分配值。
我不知道你是否有其他错误,(我不太了解VB.NET)但那两个跳出来的最大的错误。