1

私は自分のコードに少し挑戦しています。以前、TCP ベースの SerialPort Device Server (Moxa NPort) から読み書きするアプリケーションを .NET 2.0 でプログラムしました。

.NET 2.0 では問題なく動作しましたが、.NET 4.5 と Windows ストア アプリに変換すると、NetworkStream.Read/Write の代わりに DataReader/Writer を使用する必要があります。

コードをデバイスに書き込むことができました。デバイスサーバーからのログで、それも応答することがわかりますが、アプリは範囲外と言っています。どうやら私のコード内の問題です。

ここに私のコードがあります: 私は古い .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 を使用して、ストリームの最後まで一度に 1 バイトを読み取るようにしました。

        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()
4

1 に答える 1

0

変更: Do While StreamReader.UnconsumedBufferLength > 0 を使用して、ストリームの最後まで一度に 1 バイトを読み取るようにしました。

    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()
于 2013-11-01T16:45:11.130 に答える