Block Check Character(BCC) I need a function to calculate Block Check Character(BCC) in C#. 01 30 02 4D 21 20 20 03 This is the string, how do I calculate "Block Check Character" for this string.
BCC = Exclusive OR from SOH to ETX SOH ID STX CODE ETX BCC 0x01 0x30 0x02 0x40 0x03
I need to add BCC and send data to COM. If possible Please give me a function so that I can send "01 30 02 4D 21 20 20 03" and get the BCC.
Thanks
From stackoverflow
-
From what I know is that BCC is XOR of all the bytes in a given byte stream excluding the first SOH or STX till first ETX or EOT. ETX is included in the BCC. Split your byte stream after each ETX and call following function to get the BCC.
public static byte GetBCC(this byte[] inputStream) { byte bcc = 0; if (inputStream != null && inputStream.Length > 0) { // Exclude SOH during BCC calculation for (int i = 1; i < inputStream.Length; i++) { bcc ^= inputStream[i]; } } return bcc; }vvenkat62 : I am getting 66 instead I need BCC as 5C thanksLasse V. Karlsen : post a short, but complete, piece of code that gives you 66 instead of 5c.Vivek : @wenkat62 with values given above { 0x01, 0x30, 0x02, 0x4D, 0x21, 0x20, 0x20, 0x03 } I get 0x5D. When I change i=1 to i=0 the code above, the result is 0x5C. This is probably what you want. But please verify the documentation of the COM device you are programming.vvenkat62 : Sorry... can you explain to me little bit more how did you get the 0x5D. And in the documentation this is what they mentioned, nothing else. "BCC = Exclusive OR from SOH to ETX" Also if possible can you give me exact syntax how to call GetBCC function, since morning I am getting error as " GetBCC has some invalid arguments...." thanksvvenkat62 : byte stt1 =0; stt1 = GetBCC("30024D20212003"); Is this correct way to call and get the value... thanks
0 comments:
Post a Comment