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

  • Home
  • SEARCH
  • 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 8891785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:49:47+00:00 2026-06-14T22:49:47+00:00

I’m receiving periodically some data via Serial Port, in order to plot it and

  • 0

I’m receiving periodically some data via Serial Port, in order to plot it and do some more stuff. In order to achive this purpose, I send the data from my microcontroller to my computer with a header, which specifies the length of each packet.

I have the program running and working perfectly except a one last detail. When the header specifies a lenght, my program will not stop until it reachs that amount of bytes. So if, for some reason, some data from one packet is missed, the program wait and take the beginning of the next packet…and then start the real problems. Since that moment, every fails.

I thought about rising a Timer every 0.9 seconds ( the packages come every second) who will give a command in order to comeback to wait and reset variables. But I don’t know how to do it, I tried but I obtain errors while running. Since IndCom ( see next code) resets in the midle of some function and errors as “Index out of bounds” arises.

I attach my code ( without timer)

private void routineRx(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        try
        {
            int BytesWaiting;


            do
            {
                BytesWaiting = this.serialPort.BytesToRead;
                //Copy it to the BuffCom
                while (BytesWaiting > 0)
                {
                    BuffCom[IndCom] = (byte)this.serialPort.ReadByte();
                    IndCom = IndCom + 1;
                    BytesWaiting = BytesWaiting - 1;
                }

            } while (IndCom < HeaderLength);
            //I have to read until I got the whole Header which gives the info about the current packet

            PacketLength = getIntInfo(BuffCom,4);

            while (IndCom < PacketLength)
            {
                BytesWaiting = this.serialPort.BytesToRead;
                //Copy it to the BuffCom
                while (BytesWaiting > 0)
                {
                    BuffCom[IndCom] = (byte)this.serialPort.ReadByte();
                    IndCom = IndCom + 1;
                    BytesWaiting = BytesWaiting - 1;
                }
             }

            //If we have a packet--> check if it is valid and, if so, what kind of packet is
            this.Invoke(new EventHandler(checkPacket));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

I’m new in object-oriented programming and c#, so be clement, please! And thank you very much

  • 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-14T22:49:48+00:00Added an answer on June 14, 2026 at 10:49 pm

    What you might do is use a Stopwatch.

    const long COM_TIMEOUT = 500;
    
    Stopwatch spw = new Stopwatch();
    spw.Restart();
    while (IndCom < PacketLength)
    {
        //read byte, do stuff
        if (spw.ElapsedMilliseconds > COM_TIMEOUT) break;  //etc
    }
    

    Restart the stopwatch at the beginning and check the time in each while loop, then break out(and clean up) if the timeout hits. 900ms is probably too much, even, if you’re only expecting a few bytes. Com traffic is quite fast – if you don’t get the whole thing immediately it’s probably not coming.

    I like to use termination characters in communication protocols (like [CR], etc). This allows you to read until you find the termination character, then stop. This prevents reading into the next command. Even if you don’t want to use termination characters, changing your code to something like this :

     while (IndCom < PacketLength)
     {
         if (serialPort.BytesToRead > 0) 
         {
             BuffCom[IndCom] = (byte)this.serialPort.ReadByte();
             IndCom++;             
         }
      }
    

    it allows you to stop when you reach your packet size, leaving any remaining characters in the buffer for the next round through (ie: the next command). You can add the stopwatch timeout in the above also.

    The other nice thing about termination characters is that you don’t have to know in advance how long the packet should be – you just read until you reach the termination character and then process/parse the whole thing once you’ve got it. It makes your two-step port read into a one-step port read.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

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.