How to read serial port communication into buffer and parse out complete messages

Viewed 47953

I am using the following code to read values from a com port:

Private port As New SerialPort("COM13", 9600, Parity.None, 8, StopBits.One)

Private Sub port_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    Debug.Print(port.ReadExisting())
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived)
    port.Open()
End Sub

This works just fine, but every now and then it doesnt get all the data and in return results in two strings instead of just one.

An example would be if the com port was sending over the word "HELLO2YOU" it was look like:

HEL
LO2YOU

or

HELLO2
YOU

How can i place a buffer in there so that it makes sure it has all the data read before displaying it?

Thanks!

2 Answers
Related