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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:13:29+00:00 2026-05-12T16:13:29+00:00

I’ve got a little application written in C# that listens on a SerialPort for

  • 0

I’ve got a little application written in C# that listens on a SerialPort for information to come in. The information comes in as: STX + data + ETX + BCC. We then calculate the BCC of the transmission packet and compare. The function is:

private bool ConsistencyCheck(byte[] buffer)
{
    byte expected = buffer[buffer.Length - 1];
    byte actual = 0x00;

    for (int i = 1; i < buffer.Length - 1; i++)
    {
        actual ^= buffer[i];
    }

    if ((expected & 0xFF) != (actual & 0xFF))
    {
        if (AppTools.Logger.IsDebugEnabled)
        {
            AppTools.Logger.Warn(String.Format("ConsistencyCheck failed: Expected: #{0} Got: #{1}", expected, actual));
        }
    }

    return (expected & 0xFF) == (actual & 0xFF);
}

And it seems to work more or less. It is accurately not including the STX or the BCC and accurately including the ETX in it’s calculations. It seems to work a very large percentage of the time, however we have at least two machines we are running this on, both of which are Windows 2008 64-bit in which the BCC calculation NEVER adds up. Pulling from a recent log I had in one byte 20 was sent and I calculated 16 and one where 11 was sent and I calculated 27.

I’m absolutely stumped as to what is going on here. Is there perhaps a 64 bit or Windows 2008 “gotcha” I’m missing here? Any help or even wild ideas would be appreciated.

EDIT:

Here’s the code that reads the data in:

private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    // Retrieve number of bytes in the buffer
    int bytes = serialPort.BytesToRead;

    // Create a byte array to hold the awaiting data
    byte[] received = new byte[bytes];

    //read the data and store it
    serialPort.Read(received, 0, bytes);

    DataReceived(received);
}

And the DataReceived() function takes that string and appends it to global StringBuilder object. It then stays as a string builder until it’s passed to these various functions at which point the .ToString() is called on it.

EDIT2: Changed the code to reflect my altered routines that operate on bytes/byte arrays rather than strings.

EDIT3: I still haven’t figured this out yet, and I’ve gotten more test data that has completely inconsistent results (the amount I’m off of the send checksum varies each time with no pattern). It feels like I’m just calculating the checksum wrong, but I don’t know how.

  • 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-12T16:13:29+00:00Added an answer on May 12, 2026 at 4:13 pm

    The buffer is defined as a String. While I suspect the data you are transmitting are bytes. I would recommend using byte arrays (even if you are sending ascii/utf/whatever encoding). Then after the checksum is valid, convert the data to a string

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.