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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:30:32+00:00 2026-06-13T23:30:32+00:00

I am writing an application that receives some input from a long range radio

  • 0

I am writing an application that receives some input from a long range radio via a serial connection. I am currently using the SerialPort C# class to receive and send data, but I am having a few issues. I’ve noticed that the function I have for receiving data is not correctly setting the buffer byte array size. All data is being sent in bytecode (Hex).

Say the other node sends 103 bytes of data. Stepping through my code and setting a breakpoint at the “Read()” line, I see that “serialPort1.BytesToRead-1” evaluates to 103, BUT the byte[] array is only initialized to 17. I have no explanation for this behavior. As a result, only the first 17 bytes are put into the array. Continuing through the step through, this same event is triggered, this time with “serialPort1.BytesToRead-1” evaluating to 85 (presumably since only the first 17 of the 103 bytes were read.

If I hardcore the data array size at 103, it works flawlessly in one pass. However, at the moment I am unable to store all the data in my byte array in one pass, which is causing a lot of problems. Anyone know why my byte array is being initialized to such an arbitrary size???

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
        byte[] data = new byte[serialPort1.BytesToRead - 1];
        serialPort1.Read(data, 0, data.Length);
        DisplayData(ByteToHex(data) /*+ "\n"*/);
}

Updated: Here’s the new method I am attempting. isHeader is a boolean value initially set to true (as the first two bytes being received from the packet are in fact the length of the packet).

    const int NUM_HEADER_BYTES = 2;

    private void serialPort1_DataReceived(object sender,                     System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        byte[] headerdata = new byte[2];
        if (isHeader)
        {
            serialPort1.Read(headerdata, 0, NUM_HEADER_BYTES);
            int totalSize = (headerdata[0] << 8 | headerdata[1]) >> 6;
            serialPort1.ReceivedBytesThreshold = totalSize - NUM_HEADER_BYTES;
            data = new byte[totalSize - NUM_HEADER_BYTES];
            isHeader = false;
        }
        else
        {
            serialPort1.Read(data, 0, data.Length);
            double[][] results = ParseData(data, data.Length);
            serialPort1.ReceivedBytesThreshold = NUM_HEADER_BYTES;
            isHeader = true;
            DisplayData(ByteToHex(data) /*+ "\n"*/);
        }
    }
  • 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-13T23:30:33+00:00Added an answer on June 13, 2026 at 11:30 pm

    BytesToRead is equal to the number of bytes waiting in the buffer. It changes from moment to moment as new data arrives, and that’s what you’re seeing here.

    When you step through with the debugger, that takes additional time, and the rest of the serial data comes in while you’re stepping in the debugger, and so BytesToRead changes to the full value of 104.

    If you know that you need 103 bytes, I believe setting ReceivedBytesThreshold to 104 will trigger the DataRecieved event at the proper time. If you don’t know the size of the message you need to receive, you’ll need to do something else. I notice you’re throwing away one byte (serialPort1.BytesToRead – 1), is that an end-of-message byte that you can search for as you read data?

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

Sidebar

Related Questions

I'm currently writing a application that needs to capture some images. I start the
Im writing application that communicate (via serial port) with electronic device which i designed
I'm currently writing an application that displays a lot , and I mean, a
I'm currently writing a c++ console application that grabs the mouse position at regular
My script receives gzipped data from a desktop application via POST, which it retrieves
I am writing a C# .NET server application that sends and receives data over
I'm writing an application that needs to receive data from a socket every 10
i'm writing some code that basically receive data from socket, perform deserialization and then
I am writing an application which blocks on input from two istreams . Reading
I'm writing a C/C++ application under Linux that reads data from a raw socket

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.