サーバーへの接続を開き、生のバイト文字列を送信してから応答を読み取る以下の関数を持つクラスがあります。応答の長さは 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 の問題であると考えています。