0

I tried to send Hex bytes through the TCP using VB.net. And receive the response of data.

Following code I used,

    Dim tcpClient As New System.Net.Sockets.TcpClient()
    tcpClient.Connect("192.168.1.10", 502)
    Dim networkStream As NetworkStream = tcpClient.GetStream()


    If networkStream.CanWrite And networkStream.CanRead Then
        ' Do a simple write.
        Dim sendBytes As [Byte]() = {&H0, &H4, &H0, &H0, &H0, &H6, &H5, &H3, &HB, &HD3, &H0, &H1}
        networkStream.Write(sendBytes, 0, sendBytes.Length)
        ' Read the NetworkStream into a byte buffer.
        Dim bytes(tcpClient.ReceiveBufferSize) As Byte
        networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
        ' Output the data received from the host to the console.
        Dim returndata As String = Encoding.ASCII.GetString(bytes)
        TextBox1.Text = ("Host returned: " + returndata)
    Else
        If Not networkStream.CanRead Then
            TextBox1.Text = "cannot not write data to this stream"
            tcpClient.Close()
        Else
            If Not networkStream.CanWrite Then
                TextBox1.Text = "cannot read data from this stream"
                tcpClient.Close()
            End If
        End If
    End If

When I send sendbytes data, I did not get any data. When I send data, master automatically sends me data but I did not get any data. This is Modbus communication.

I can only see Host returned:

1 Answer 1

1

The data is there, but you cannot see it because it starts with a null byte (&H0 or just 0). Most text controls that encounter a null byte interprets that as the end of the string and thus doesn't render the rest of the text.

GetString() merely takes the bytes as is and converts them into the respective chars with the same values. It is up to you to turn the result into a readable format.

The solution is to skip GetString() and instead iterate the array, converting every byte into a hex or number string.

Also, two very important things:

  1. You shouldn't use TcpClient.ReceiveBufferSize in your code as it is used for the internal buffer. You should always decide the buffer size on your own.

  2. Since TCP is a stream-based protocol the application layer has no notion of packets. One 'send' from the server usually does not equal one 'receive'. You might receive more or less data than what the first packet actually is. Use the return value from NetworkStream.Read() to determine how much has been read.

    You then need to read up on the Modbus documentation and see if its data contains something that indicates the end or the length of a packet.

'Custom buffer: 8 KB.
Dim bytes(8192 - 1) As Byte
Dim bytesRead As Integer = networkStream.Read(bytes, 0, bytes.Length)

Dim returndata As String = "{"

'Convert each byte into a hex string, separated by commas.
For x = 0 To bytesRead - 1
    returnData &= "0x" & bytes(x).ToString("X2") & If(x < bytesRead - 1, ", ", "}")
Next

TextBox1.Text = "Host returned: " & returnData
Sign up to request clarification or add additional context in comments.

1 Comment

great answer mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.