0

サーバーへの接続を開き、生のバイト文字列を送信してから応答を読み取る以下の関数を持つクラスがあります。応答の長さは 23 バイトで、ハイパーターミナルで同じ最初のメッセージを送信し、そこで応答を表示することにより、応答がサーバーによって送信されていることを確認しました。

ただし、VB.NET Windows フォーム アプリケーションでは、Response.Data に保存されたデータは 5 バイトの長さしかないように見え、接続がタイムアウトします (私は stream.ReadTimeout = 1000 を持っています)。場合?

 Public Function sendCommand(command As String) As Boolean
        Dim lst As New List(Of Byte)()
        Dim buf(50) As Byte
        Dim numRead As Integer
        Dim ofst As Integer = 0

        lst.AddRange(Encoding.ASCII.GetBytes(command))  ' Convert the command string to a list of bytes
        lst.Add(&H4)    ' Add 0x04, 0x0A and a newline to the end of the list
        lst.Add(&HA)
        lst.AddRange(Encoding.ASCII.GetBytes(vbNewLine))
        buf = lst.ToArray   ' Convert the list to an array

        If Not makeConnection() Then    ' Make the connection (client & stream) and check if it can be read and written to.
            Return False
        End If

        stm.Write(buf, 0, buf.Length)   ' Write the array to the stream

        Try
            Do    
                numRead = stm.Read(buf, ofst, 5)  ' Try and read the response from the stream
                ofst += numRead
            Loop While numRead > 0
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try

        breakConnection()   ' Close the connection

        Response.Type = Type.Strng  ' Save the response data
        Response.Data = System.Text.Encoding.ASCII.GetString(buf, 0, ofst) 'Changed to ofst
        'Response.Type = Type.Int
        'Response.Data = numRead.ToString
        Return True
    End Function

更新:それ以来、スコープを使用して、応答データをサーバーに供給するシリアルラインをチェックしました。これ以上データが送信されないように、シリアル ラインをハイに保持します。サーバーの設定を確認する必要がありますが、HyperTerm では問題ないので、これは私の VB の問題であると考えています。

4

2 に答える 2

1

あなたが投稿したコードでは、これは への呼び出しが に割り当てるResponse.Data最大の数であるため、 は 5 バイトを超えることはありません。代わりに必要だと思います(ストリームの読み取り前ではなく、読み取り後にインクリメントする必要がある場合があります)。stm.ReadnumReadofst

于 2013-07-17T13:22:56.617 に答える