• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1349
  • Last Modified:

C# Funtion Calculating UTF-8 BCC

Hello guys,
I have a string and convert it into UTF-8 byte array, so this array needs to be an parameter of a function, that will return the BCC. But I need the function.
0
IncognitoMan
Asked:
IncognitoMan
  • 3
1 Solution
 
ricovoxCommented:
Hello,

I believe by BCC you mean byte,block, and checksum, right?
Can you tell me a little more about the way your data is formatted?

What I mean is that usually you send the data in a transmission that includes additional information like this:
<data...><additional bytes><checksum>

Where "additional bytes" are 1 or 2 additional random bytes that you add to the transmission to ensure better verification accuracy. These would be optional, and maybe you have them and maybe you don't. It depends on your application.

Usually you want to compare the calculated checksum of the data plus the additional bytes AFTER transmission with the checksum calculated BEFORE transmission. Does this sound about right? Do you use the additional bytes or not?

I don't know if the definition for BCC is standardized, but I saw an 8-bit BCC calculated by XORing all the bytes in the data:

private byte CalcBCC(byte[] buffer)
{
    byte calc = 0x00;
   
    //XOR with each byte in the buffer
    for (int i = 1; i < buffer.Length - 1; i++)
    {
        calc ^= buffer[i];
    }
    return calc;
}

You would just have to be consistent in using the same function both before and after transmission to obtain the same checksum. Also make sure to be consistent with what part of the datastream you calculate. I.e. if there are "additional bytes" then make sure to include them in both calculations for consistency.

Hope that helps.
0
 
ricovoxCommented:
After more searching I found another reference about BCC.
This is a PDF that is probably for a specific RS232 instrument.
http://www.bttautomatyka.com.pl/pdf/HA466357.pdf

In it, they have data formatted like this:
<STX><DATA><ETX><CHECKSUM>
Where STX and ETX are each single bytes representing the start and end of the data, respectively.
The BCC is calculated exactly as I specified in the code in my previous post, but it INCLUDES both the STX and ETX. So you have to make sure that these are included in the byte array you pass in to the function. Then you have to remember that the data does NOT include these two characters. The data also does not include the checksum at the end, which is another single byte.

I can send you some more code if you need additional help.
0
 
ricovoxCommented:
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now