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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:21:40+00:00 2026-05-16T15:21:40+00:00

Pretty simple question this time around. I have an application that communicates with another

  • 0

Pretty simple question this time around. I have an application that communicates with another copy of the application on another machines. One application sends a pretty constant stream of data, the other receives it.

The code to send data looks like this (where serialPort is an instance of the System.IO.Ports.SerialPorts class in C# .Net 2.0):

private void bgDataWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e){
    try{
        string tempStr = Convert.ToString(String.Format("{0:0.000000}", data));
        serialPort.Write(tempStr); // Write "data" out to 6 decimal places  
    }
    catch (TimeoutException){ }
    catch (InvalidOperationException err){ // Port is obstructed or closed
        this.Invoke((MethodInvoker)delegate{
            MessageBox.Show(this, "Couldn't send wireless data:\n\n" + 
                            err.ToString(), "NanoMETER - Wireless Error (Data)", 
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
            Global.remoteEna = false;
            serialPort.Close();
            usingBT = false;  
        }); 
    }
}

It’s called on a timer. The receive code is even more straightforward:

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {
    string buffer = serialPort.ReadExisting();
    HandleInput(buffer);
}

Data gets sent and handled and it’s all fine and dandy, but there’s some unwanted choppiness where it’s either not reliably sending data at a constant rate, or it’s not picking up everything. I’m not sure if this can be fixed in my code, or if it’s just the nature of having a few slow machines and a possibly shakey bluetooth connection. Any suggestions?

  • 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-05-16T15:21:41+00:00Added an answer on May 16, 2026 at 3:21 pm

    It’s not uncommon for interns to be assigned to converting old code to a newer platform.

    There are a few improvements you can make.

    1) The following strategy is good when the bytes sent through the port is meant to be interpreted in blocks, such as commands. Do you have some sort of protocol? Something that dictates the format of the message you are sending. For instance, a specific delimiter to indicate the beginning and the length of the upcoming command. This allows you to quickly determine if the command was only half sent, or if there were missing bytes. Even better is to add a CRC at the end.

    2) Instead of reading on a timer, base yourself on the events flagged by your serialport object. Here’s an example of what i use:

        //OnReceive event will only fire when at least 9 bytes are in the buffer.
        serialPort.ReceivedBytesThreshold = 9; 
        //register the event handlers
        serialPort.DataReceived += new SerialDataReceivedEventHandler(OnReceive);
        serialPort.PinChanged += new SerialPinChangedEventHandler(OnPinChanged);
    

    In the code above, i set a threshhold of 9, you should change that to whatever fits your context. Also, the Pinchanged event is something good to monitor, it will allow you to quickly identify if the cable has been disconnected. There is more on this, regarding CTSChanged but you can look it up if you are interested.

    Lastly, if this doesn’t help you get a little further, show an example of the problem that occured so the peolpe here can give you more help.

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

Sidebar

Related Questions

I have a question that's so simple I cannot believe I can't answer it
My question is pretty simple: how is event-driven programming actually achieved? To elaborate: I
This isn't a question of what stress testing tools are out there. I'm afraid
Say I have some simple class and once it's instantiated as an object I
I'm blocked with a very simple Query that it's not working as I think
Lately I have been interested in better understanding the optimizations done by compiler back-ends.
I have a fairly naive thing I want to do and I want to
Scott Hanselman ( alternate link ) suggested in a twitter tweet on November 18,
So last week I posted a problem for the ACM ICPC South East Regionals
I want to upload my first APK/app to the Android Market, but I got

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.