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 8670897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:48:54+00:00 2026-06-12T18:48:54+00:00

I am working with a microcontroller that sends data to PC’s serial port according

  • 0

I am working with a microcontroller that sends data to PC’s serial port according to the format below:

Start Byte = 0x7E
Data Bytes ...........
StopByte = 0x7E

So basically I want to read this packets in a byte array. I am trying to do this using the DataReceived event of SerialPort class, but it just fails and never completely receives a good packet of data:

private List<int> _readBuffer = new List<int>();

private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    var sp = (SerialPort)sender;
    var indata = sp.ReadChar();

    if (indata == 0x7E && _readBuffer.Count == 0)
        _readBuffer.Add(indata);

    if(_readBuffer.Count > 0 && indata != 0x7E)
        _readBuffer.Add(indata);

    if(_readBuffer.Count > 0 && indata == 0x7E)
    {
        _readBuffer.Add(indata);

        //Dump packet to textbox
        Invoke(new EventHandler((o, args) =>
        {
           foreach(var i in _readBuffer)
           {
               tbIn.Text += string.Format("{0:X} ", i);
           }

            _readBuffer = new List<int>(); //Renew the reading buffer array!
        }));
    }
}

I can’t figure out what can be the problem, I get only the start and end bytes to the textbox, like:

7E 7E

While I know the correct packet is:

7E 0 43 00 FF FF 0 0 7E

Looking forward to your tips/tricks!

  • 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-12T18:48:56+00:00Added an answer on June 12, 2026 at 6:48 pm

    Hmm… so let’s look at that code. I’ve added a few comments that I reference below.

    var sp = (SerialPort)sender;
    var indata = sp.ReadChar();  // A
    
    if (indata == 0x7E && _readBuffer.Count == 0) // B
        _readBuffer.Add(indata);  
    
    if(_readBuffer.Count > 0 && indata != 0x7E) // C
        _readBuffer.Add(indata);
    
    if(_readBuffer.Count > 0 && indata == 0x7E) // D
    {
        _readBuffer.Add(indata);
    
        //Dump packet to textbox
        Invoke(new EventHandler((o, args) =>
        {
           foreach(var i in _readBuffer)
           {
               tbIn.Text += string.Format("{0:X} ", i);
           }
    
            _readBuffer = new List<int>(); //Renew the reading buffer array!
        }));
    }
    

    You call sp.ReadChar() at “A”

    At “B” you test and it equals, presumably, 0x7E and you’ve got an empty list so you add the 7E to the list.

    “C” doesn’t evaluate to true because indata does in fact equal 7E

    “D” does evaluate to true because indata still equals 7E and you’ve got 1 thing in the list.

    So now 7E is in the list twice. You’ve just copied the same byte twice now.

    Then you push the two list items into the textbox… 7E twice.

    You’ve only called sp.ReadChar once. You need to read the rest of the data off the SerialPort if you want to get that data. Do that, and I think you’ll find all the bytes you’re looking for.

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

Sidebar

Related Questions

I'm very new to serial port/windows programming... and need to communicate with a microcontroller
I'm working on an i/o project where I have a microcontroller that communicates with
I am working with a microcontroller that has an external EEPROM containing tables of
I'm working in an embedded microcontroller and I have to read/write multiple types data
I have a microcontroller that must download a large file from a PC serial
I'm working with embedded C for a microcontroller right now and I find that
Working with an undisclosed API, I found a function that can set the number
I am working on robot which has to control using wireless serial communication. The
I am working with a Microchip dsPIC33FJ128GP802. It's a small DSP-based microcontroller, and it
I am working on a dynamic language (that compiles into C++) for microcontrollers, being

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.