我的代码有点挑战。我之前在 .NET 2.0 中编写了一个应用程序,该应用程序可以从基于 TCP 的 SerialPort 设备服务器 (Moxa NPort) 读取写入和读取/读取。
它在 .NET 2.0 中运行良好,但是当转换为 .NET 4.5 和 Windows Store App 时,我需要使用 NetworkStream.Read/Write 的 DataReader/Writer instread。
我已设法将代码写入设备,我可以在设备服务器的日志中看到它也回答了,但应用程序说超出范围。所以这显然是我的代码中的一个问题。
这是我的代码: 在底部,我附上了旧的 .NET 2.0 代码。
希望有人可以帮助我,在此先感谢。
新代码
Try
If SendCommand IsNot Nothing Then
'status.Text = "Forsøger at sende data ..."
Dim StreamWriter As New DataWriter(ClientSocket.OutputStream)
Dim outNumBytes As Integer = 0
Dim outStream As Byte() = New Byte((SendCommand.Length + 1) \ 3 - 1) {}
For outNumBytes = 0 To outStream.Length - 1
outStream(outNumBytes) = Byte.Parse(SendCommand.Substring(3 * outNumBytes, 2), Globalization.NumberStyles.HexNumber)
Next
StreamWriter.WriteBytes(outStream)
Await StreamWriter.StoreAsync()
StreamWriter.DetachStream()
StreamWriter.Dispose()
'status.Text = "Data afsendt (" & SendCommand & ")"
End If
Catch ex As Exception
returnError = "Fejl ved afsendelse af data: " & ex.Message
ErrorHandling(returnError)
ClientSocket.Dispose()
ClientSocket = Nothing
Return returnData
Exit Function
End Try
Await System.Threading.Tasks.Task.Delay(50)
Try
'status.Text = "Forsøger at modtage data ..."
Dim StreamReader As New DataReader(ClientSocket.InputStream)
StreamReader.InputStreamOptions = InputStreamOptions.Partial
Dim inStream(0) As Byte
Dim inText As New StringBuilder(inStream.Length * 2)
Await StreamReader.LoadAsync(256)
StreamReader.ReadBytes(inStream)
For Each inByte As Byte In inStream
inText.AppendFormat("{0:X2}:", inByte)
Next
returnData = inText.ToString()
StreamReader.DetachStream()
StreamReader.Dispose()
'status.Text = "Data modtaget (" & strReceived & ")"
Catch ex As Exception
returnError = "Fejl ved modtagelse af data: " & ex.Message
ErrorHandling(returnError)
ClientSocket.Dispose()
ClientSocket = Nothing
Return returnData
Exit Function
End Try
旧代码
Public Function SendReceive(ByVal SendCommand As String)
Dim returnCode As String = Nothing
'//Write
Dim outText As String = SendCommand
Dim outNumBytes As Integer = 0
Dim outStream As Byte() = New Byte((outText.Length + 1) \ 3 - 1) {}
If NetworkStream.CanWrite Then
Try
For outNumBytes = 0 To outStream.Length - 1
outStream(outNumBytes) = Byte.Parse(outText.Substring(3 * outNumBytes, 2), Globalization.NumberStyles.HexNumber)
Next
NetworkStream.Write(outStream, 0, outNumBytes)
Catch ex As Exception
returnCode = "Error: " & ex.Message
Return returnCode
Exit Function
End Try
End If
Threading.Thread.Sleep(50)
'//Read
Dim inStream(0) As Byte
Dim inText As New StringBuilder(inStream.Length * 2)
Do While NetworkStream.CanRead And NetworkStream.DataAvailable
Try
NetworkStream.Read(inStream, 0, inStream.Length)
For Each inByte As Byte In inStream
inText.AppendFormat("{0:X2}:", inByte)
Next
Catch ex As Exception
returnCode = "Error: " & ex.Message
Return returnCode
Exit Function
End Try
Loop
returnCode = inText.ToString()
If returnCode > Nothing Then returnCode = returnCode.Substring(0, returnCode.Length - 1)
Return returnCode
End Function
更改:使用 Do While StreamReader.UnconsumedBufferLength > 0 让它一次读取一个字节,直到流结束。
Dim StreamReader As New DataReader(ClientSocket.InputStream)
StreamReader.InputStreamOptions = InputStreamOptions.Partial
If srvMode = True Then
Dim strReceivedBytes As String = Await StreamReader.LoadAsync(256)
returnData = StreamReader.ReadString(strReceivedBytes)
Else
Dim inStream(0) As Byte
Dim inText As New StringBuilder(inStream.Length * 2)
Await StreamReader.LoadAsync(256)
Do While StreamReader.UnconsumedBufferLength > 0
StreamReader.ReadBytes(inStream)
For Each inByte As Byte In inStream
inText.AppendFormat("{0:X2}:", inByte)
Next
Loop
returnData = inText.ToString()