Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:00:28+00:00 2026-06-07T11:00:28+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:00:30+00:00Added an answer on June 7, 2026 at 11:00 am

    You have to think of Serial Port communications as streaming data. Any time you receive data, you have to expect that it may be a complete message, only a partial message, or multiple messages. It all depends how fast the data is coming in and how fast you application is able to read from the queue. Therefore, you are right in thinking you need a buffer. However, what you may not be realizing yet, is that there is no way to know, strictly via, the Serial Port, where each message begins and ends. That has to be handled via some agreed upon protocol between the sender and the receiver. For instance, many people use the standard start-of-text (STX) and end-of-text (ETX) characters to indicate the beginning and ending of each message send. That way, when you receive the data, you can tell when you have received a complete message.

    For instance, if you used STX and ETX characters, you could make a class like this:

    Public Class DataBuffer
        Private ReadOnly _startOfText As String = ASCII.GetChars(New Byte() {2})
        Private ReadOnly _endOfText As String = ASCII.GetChars(New Byte() {4})
    
        Public Event MessageReceived(ByVal message As String)
        Public Event DataIgnored(ByVal text As String)
    
        Private _buffer As StringBuilder = New StringBuilder
    
        Public Sub AppendText(ByVal text As String)
            _buffer.Append(text)
            While processBuffer(_buffer)
            End While
        End Sub
    
        Private Function processBuffer(ByVal buffer As StringBuilder) As Boolean
            Dim foundSomethingToProcess As Boolean = False
            Dim current As String = buffer.ToString()
            Dim stxPosition As Integer = current.IndexOf(_startOfText)
            Dim etxPosition As Integer = current.IndexOf(_endOfText)
            If (stxPosition >= 0) And (etxPosition >= 0) And (etxPosition > stxPosition) Then
                Dim messageText As String = current.Substring(0, etxPosition + 1)
                buffer.Remove(0, messageText.Length)
                If stxPosition > 0 Then
                    RaiseEvent DataIgnored(messageText.Substring(0, stxPosition))
                    messageText = messageText.Substring(stxPosition)
                End If
                RaiseEvent MessageReceived(messageText)
                foundSomethingToProcess = True
            ElseIf (stxPosition = -1) And (current.Length <> 0) Then
                buffer.Remove(0, current.Length)
                RaiseEvent DataIgnored(current)
                foundSomethingToProcess = True
            End If
            Return foundSomethingToProcess
        End Function
    
    
        Public Sub Flush()
            If _buffer.Length <> 0 Then
                RaiseEvent DataIgnored(_buffer.ToString())
            End If
        End Sub
    End Class
    

    I should also mention that, in communication protocols, it is typical to have a checksum byte by which you can determine if the message got corrupted during its transmission between the sender and the receiver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to read an RSS feed and output the results.
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
Using following code I try to get updated list of checkbuttons' corresponding text values,
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I am using following code to get bitmap from url. This function is used
I have the following code, which basically takes values from a database and populates
I am using the following tcl code to store a file from my deskstop
I have the following code: http = Net::HTTP.new(www.something.com, 80) http.set_debug_output($stdout) http.post(/blah, something, {'random-parameter' =>
In my application I read RGB pixel values from several images using fast unmanaged
I'm reading some XML with XmlReader using the following code: XmlReaderSettings settings = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.