Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Block Check Character

Status
Not open for further replies.

boston49

Vendor
Mar 3, 2002
9
0
0
US
Is there a function, or an undestandable formula, that will calculate the BCC (Block Check Character)of a data string? The string typically looks like <STX> .... 54 to 57 Characters.... <ETX>. The Bcc needs to be calculated on characters starting with the first character after the stx to the end including the etx.

Thanks in advance for your time.

 

??? 0 out of 5 ??? with only 1 reply in the 5 threads that you have started???

Please Read FAQ222-2244

Then to answer your question look up string handling functions...
InStr
Mid
Left
Right
Len
InStrRev
StrReverse

Good Luck

 

boston49,

Did you look up those functions? Did you get it working?

 
Not working yet. I am not trying to manipulate the string or change the string other than to add the BCC to the end of it. I have searched all the faqs that I could find with out any luck. The closest thing I have found, searching on google, is that it is the &quot;exclusive OR of the sum of the characters&quot;. One software company said I didn't need any special &quot;tools&quot; use &quot;mod (256)&quot;. The application I need this for I created in dos foxpro, years ago. Foxpro had an addin that provided the BCC funtion. I have not been able to create a formula in VB6 that gives me the same answer as the foxpro program.
 
Just run through the string character by character xor-ing each character with the previous result:


For intChar = 1 to len(strTest)
intResult = intResult XOr Asc(mid(strTest,intChar,1))
Next intChar

That should do the trick.....


Greetings,
Rick
 
Whoops, seems like Matt types faster than I do....

Greetings,
Rick
 
LazyMe

Either its a bit more complex than that...

OR

I've gone overboard on code!

boston49

Her is a very rough solution, not optimised or error checked....

Code:
'---------------------------------------------------------------------------------------
' Procedure : HexToInt
' DateTime  : 07/11/2003 12:54
' Author    : Matthew Knight
' Purpose   : Rather Brute Hex to Int Conversion
'           : Assumes that the string will be 2 valid hex char long
'---------------------------------------------------------------------------------------
'
Public Function HexToInt(strSub As String) As Integer

    Dim intResult As Integer
    intResult = (16 * HX(Left$(strSub, 1))) + HX(Right$(strSub, 1))

    HexToInt = intResult

End Function
Public Function HX(strSub As String) As Integer
'---------------------------------------------------------------------------------------
' Procedure : HX
' DateTime  : 07/11/2003 12:54
' Author    : Matthew Knight
' Purpose   : even more Brute Hex to Int Conversion
'           : Assumes single valid hex char
'---------------------------------------------------------------------------------------
'
    Dim intResult As Integer
    Select Case strSub
    Case &quot;A&quot;
    intResult = 10
    Case &quot;B&quot;
    intResult = 11
    Case &quot;C&quot;
    intResult = 12
    Case &quot;D&quot;
    intResult = 13
    Case &quot;E&quot;
    intResult = 14
    Case &quot;F&quot;
    intResult = 15
    Case Else
    intResult = CInt(strSub)
    End Select
    HX = intResult
End Function

'---------------------------------------------------------------------------------------
' Procedure : CalculateBCC
' DateTime  : 07/11/2003 12:56
' Author    : Matthew Knight
' Purpose   : Calculate BCC for stream of data
'           : assumes STB has been removed
'---------------------------------------------------------------------------------------
'
Public Function CalculateBCC(strStream As String) As String

Dim intStringLen As Integer, intCount As Integer, intSub As Integer
Dim strSub As String
Dim intBCC As Integer, blnSkippedControl As Boolean

intStringLen = Len(strStream)

For intCount = 1 To intStringLen Step 2
    'Get our byte value
    strSub = UCase$(Mid$(strStream, intCount, 2))
    'Convert to decimal integer
    intSub = HexToInt(strSub)
    
    
If intSub = 16 And blnSkippedControl = False Then
    'Set our flag to show we found a control code
    'need to cope with situation where &H10 is valid data
    'i.e. stream would look like ...1010...
    blnSkippedControl = True
Else
    ' XOR this byte with result
    intBCC = intBCC Xor intSub
    blnSkippedControl = False

End If
Next intCount
    'convert to hex and return
    CalculateBCC = Hex$(intBCC)

End Function

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Well, actually I'd say you indeed got a little overbaord with code.


Usually characters at the OCM port are presented in bytes, right? Reading them into a string and then taking the ascii value of each byte would do I guess.....

It is not very common to send a byte in hex characters, since this would mean two bytes are necessary to send one (0xFF would have to be 'FF', right?).

Greetings,
Rick
 
lazyMe
>Usually characters at the OCM port are presented in bytes, right?

Sort of, but both posts from boston49 specifically mention characters and string
quote
&quot;<STX> .... 54 to 57 Characters.... <ETX>&quot; and
&quot; manipulate the string or change the string other than to add the BCC&quot;


You are also taking the ascii code for the char, which is not the same as thebyte value

i.e. asc(&quot;0&quot;) is not equal to 0...

Neither does it take account of the control byte code...

I have tried your agolrithm agains the test data in the link posted above, it don't work!

[soapbox]



Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Matt,


I know all that. Asc(&quot;0&quot;) is NOT 0. The problem is that characters actually don't arrive at the COM port as a string (and string being &quot;readable data&quot;). But when reading it through the usual VB controls, you'll get a string, won't you? And this string, when printed contains the most serious rubbish you've ever seen, since it is not meant to be a readable string. So when anyone sending an actual 0 (ascii 0) into his/her COM port, the string returned from the receiving COM port will actually be this ascii 0 and not the ascii 48 (which would be a readable &quot;0&quot;, if I'm not mistaking..).

So when reading byte data at the COM port you should always take ASC, since this is exactly the byte value it should represent. How else would you send a byte value of 234? This would mean that you need three bytes to send this value over (&quot;2&quot; + &quot;3&quot; + &quot;4&quot;). People don't send over data this way. At least, I wouldn't (and I've also never seen data being send over that way). And EVEN if this would be the case, you'd still have to calculate the BCC like I did, since these are the acutal bytes that have been send / are to be send. After all; it's all about checking if the same data that has been send, also has arrived, isn't it?

And I did take in account the control byte, since my string says strTest, which is up to the user to enter any string he/she wants to test. Taking in account any control characters is up to the application, the loop just calculates the BCC of the specified string....


I'm probably very bad or, at best, very vague in explaining what I want to state. Still I hope you understand the point I'm trying to make here...

Greetings,
Rick
 
<sound fx>
penny drops
</soundfx>

Oops, sorry

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Thanks everyone. I am now able to calculate a bcc. It doesn't exactly equal the old foxpro program but I think thats my problem with the string. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top