Reading byte value 26 in SerialPort vb.net
<p>I have an vb.net winforms application that reads/writes serial data to several devices connected to a USB-RS485 device. The application is written in visual basic .NET 2008, altough i tried .NET 2015 as well.</p>

<p>Almost everything is working well except when one of the devices sends a message with a byte value of 26. Then im not sure what exactly happens but after the byte value of 26 the reading seems to stop. And all data after that is read in the next DataReceived event.</p>

<p>I have made a short program to only test the serialport. And it shows exactly the same. Also i have only one device connected with the test.
The data the device is sending consists of 10 bytes. The values are 251,20,(6 data bytes),checksum lo, checksum hi.</p>

<p>The first two and last two bytes can never reach a value of 26, it is just the data bytes that can do that.</p>

<p>Imports:</p>

<pre><code>Imports System.IO.Ports
Imports System.Text
</code></pre>

<p>Variables:</p>

<pre><code>Dim WithEvents SP_TestCom As SerialPort
Public DataArray() As Byte
</code></pre>

<p>subs:</p>

<pre><code>Public Sub OpenCom()
    If SP_TestCom.IsOpen Then
        Exit Sub
    End If
    SP_TestCom.Open()
End Sub

Public Sub CloseCom()
    If Not SP_TestCom.IsOpen Then
        Exit Sub
    End If
    SP_TestCom.Close()
End Sub

Public Sub SendData(ByVal Data() As Byte)
    Try
        If SP_TestCom.IsOpen Then
            SP_TestCom.Write(Data, 0, Data.Length)
        End If
    Catch ex As Exception

    End Try
End Sub


Private Sub SP_TestCom_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP_TestCom.DataReceived
    Try
        If SP_TestCom.BytesToRead &gt; 0 Then
            Dim DataArray_Temp(SP_TestCom.BytesToRead + 1) As Byte
            SP_TestCom.Read(DataArray_Temp, 0, SP_TestCom.BytesToRead)

            Array.Copy(DataArray_Temp, DataArray, DataArray_Temp.Length)
        End If
    Catch ex As Exception

    End Try
End Sub
</code></pre>

<p>I have a main form with two buttons and a timer. The buttons are for opening and closing the port. The timer reads the dataarray</p>

<p>I have tried several ways of reading.</p>

<ul>
<li>Reading directly into an array</li>
<li>Reading each byte seperatly</li>
<li>Reading into a string and then converting it into a byte array</li>
</ul>

<p>I also tried different encodings. Although i am not sure if it would have any effect.</p>

<ul>
<li>ASCII</li>
<li>UNICODE</li>
<li>UTF</li>
</ul>

<p>All gives the same error.</p>

<p>I could bypass the error by ignoring any message containing a byte value of 26. This at least does not give me strange results in the rest of my application but it is not the perfect solution.</p>

<p>Does anyone have an idea of what the issue might be?</p>

<p>Edit:</p>

<pre><code>Public Sub InitSerial()
    SP_TestCom = New SerialPort
    SP_TestCom.PortName = "COM26"
    SP_TestCom.ReadTimeout = 20
    SP_TestCom.BaudRate = 19200
    SP_TestCom.DataBits = 8
    SP_TestCom.StopBits = StopBits.One
    SP_TestCom.Parity = Parity.None
    SP_TestCom.ReceivedBytesThreshold = 10
End Sub
</code></pre>