Solved

Calculating a BCC (Xor) for a message

Posted on 2016-10-20
13
52 Views
Last Modified: 2016-10-24
How can I calculate the BCC for the follow message:

  Dim s As String = ChrW(&H1) & ChrW(&H56) & "01" & ChrW(&H2) & "123" & ChrW(&H3) & ChrW(&HD)

BCC is XOR of all the bytes in a given byte stream excluding the first SOH[chr(1)] or STX[chr(2)] till first ETX[chr(3)] or EOT[chr(4)]. ETX is included in the BCC.

Thanks in advance
0
Comment
Question by:cmdolcet
  • 8
  • 4
13 Comments
 

Author Comment

by:cmdolcet
ID: 41852581
no its just all .net environment.
0
 
LVL 27

Expert Comment

by:Ark
ID: 41853131
Private Function GetBCC(input As String) As Byte
    Dim data = System.Text.Encoding.ASCII.GetBytes(input)
    Dim firstTime = True
    Dim checkSum As Byte = 0
    Dim start As Integer = 1 'skip first SOH or STX
    For i = start To data.Length - 1
        If data(i) = 3 OrElse data(i) = 4 Then Exit For
        If firstTime Then
            firstTime = False
            checkSum = data(i)
        Else
            checkSum = checkSum Xor data(i)
        End If
    Next
    Return checkSum
    'or for 8-bit
    'Return checkSum And &H7F
End Function

Open in new window

Dim s As String = ChrW(&H1) & ChrW(&H56) & "01" & ChrW(&H2) & "123" & ChrW(&H3) & ChrW(&HD)
MsgBox("BCC = 0x" & GetBCC(s).ToString("X"))

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 41853677
Ark, thank you so much for the function.

I am having issues where I use this message:

SOH
1
STX
01ABC123 ETX 238 CR


The BCC which is the 238 is calculated from the following:
1
01
ABC123


using the function above I do not get the returned Hex EE
0
 

Author Comment

by:cmdolcet
ID: 41853769
Using the values
1
01
ABC123

I get the BCC to be decimal 64
0
 

Author Comment

by:cmdolcet
ID: 41853882
This is what the vendor says in the manual:

See attached screen shot.
Manual-Screen-Shot.PNG
0
 

Author Comment

by:cmdolcet
ID: 41854336
OK looks like I am learning a little and figuring things out on my own ;)

I did run into an issue with the return of the following:

I get a Hex value in this function:
 Dim testStr As String = "V" & "01" & Telesis_DateFormat
        Dim byteArray() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(testStr)
        Dim total As Integer
        Try
            For x As Integer = 0 To byteArray.Length - 1
                total += byteArray(x)
            Next
            MsgBox(total.ToString("X"))

Open in new window


But when sending to the device I need this to be more dynamic and not hard code the decimal values on the checksum

 Dim BCCValue As String = String.Empty
                GetBCC(BCCValue)
                Dim s1 As String = ChrW(&H1) & "V" & ChrW(&H2) & "01" & Telesis_DateFormat & ChrW(&H3) & ChrW(&H31) & ChrW(&H39) & ChrW(&H30) & ChrW(&HD)
                Dim encodedBytes1 As Byte() = utf8.GetBytes(s1)
                TelesisSerialPort1.Write(encodedBytes1, 0, encodedBytes1.Length)

Open in new window


Therefore I need To convert my return HEX BE into decimal and then save that.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Ark
ID: 41854800
You wrote that BCC is XOR while manual says that it's 8-bit sum. So correct code (according manual):
    Private Function GetBCC(input As String) As Byte
        Dim data = System.Text.Encoding.ASCII.GetBytes(input)
        Dim checkSum As Long = 0
        For i = 0 To data.Length - 1
            If data(i) = 1 OrElse data(i) = 2 Then Continue For
            If data(i) = 3 OrElse data(i) = 4 Then Exit For
            checkSum += data(i)
        Next
        Return checkSum And &HFF
    End Function

Open in new window

      Dim s = Chr(1) & "1" & Chr(2) & "01ABC123" & Chr(3) & "238" & vbCr
        MsgBox(GetBCC(s))

Open in new window

gives 238
0
 

Author Comment

by:cmdolcet
ID: 41855113
Ark yes sorry I was confused.

How can I know take the result of what I get in the BCC function and apply it dynamically to my serialport.write code? WhatI gave use  I am confused on also is that for example I gave with decimal 238 they break it down as ChrW(&H31) & ChrW(&H39) & ChrW(&H30) how can I do this more dynamically?

 Dim BCCValue As String = String.Empty
                GetBCC(BCCValue)
                Dim s1 As String = ChrW(&H1) & "V" & ChrW(&H2) & "01" & Telesis_DateFormat & ChrW(&H3) & ChrW(&H31) & ChrW(&H39) & ChrW(&H30) & ChrW(&HD)
                Dim encodedBytes1 As Byte() = utf8.GetBytes(s1)
                TelesisSerialPort1.Write(encodedBytes1, 0, encodedBytes1.Length)

Open in new window

0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 41855725
Unfortunatelly, I don't know the specification  of TelesisSerialPort input string. Most serial ports controllers calculates BCC (as well as appends STX and ETX marks) automatically, so you just need send "ABC123" string as byte array and SP controller add all missing SOH,STX, BCC and ETX. If you need add them manually - here are functions according Manual you sent:
Private Function GetBCC(input As String) As Byte
    Dim data = System.Text.Encoding.ASCII.GetBytes(input)
    Dim checkSum As Long = 0
    For i = 0 To data.Length - 1
        If data(i) = 1 OrElse data(i) = 2 Then Continue For
        If data(i) = 3 OrElse data(i) = 4 Then Exit For
        checkSum += data(i)
    Next
    Return checkSum And &HFF
End Function

Private Function ByteToHex(input As Byte) As Byte()
    Return System.Text.Encoding.ASCII.GetBytes(input.ToString)
End Function

Open in new window

Using:
Private SOH As String = Chr(1), STX As String = Chr(2), ETX As String = Chr(3)
Private EOS As String = vbCr, MessageType As String = "1", FieldNumber = "01"

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim message = "ABC123"
    Dim s = SOH & MessageType & STX & FieldNumber & message & ETX
    Dim bcc = GetBCC(s)
    Dim bccBytes = ByteToHex(bcc)
    'Complete string to pass to SP, including SOH, STX, BCC etc:
    Dim StringToSend = s & System.Text.Encoding.ASCII.GetString(bccBytes) & EOS
    'Just to show string in Hex:
    Dim sb As New System.Text.StringBuilder
    For Each c In StringToSend
        sb.AppendFormat("{0:X3}H ", Asc(c))
    Next
    MsgBox("Hex string to send:" & vbCrLf & sb.ToString)
End Sub

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 41856483
So what your saying is the checksum will be calculated automatically from the com
Port ? Can you clarify the last post for me

Also I want to be able to append the checksum results automatically to the message and not take each decimal value and return it's equivalent
0
 

Author Comment

by:cmdolcet
ID: 41856956
Ark,

What is c?
In for  For Each c In StringToSend.............
0
 
LVL 27

Expert Comment

by:Ark
ID: 41857792
For each c as Char  In StringToSend...
This loop convert each char to 3 digit hex with "H" at the end just to display string similar to manual. MsgBox displays:
001H 031H 002H 030H 031H 041H 042H 043H 031H 032H 033H 003H 032H 033H 038H 00DH
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 35
VB.NET HttpWebRequest 12 56
vb.net checkbox 7 41
VB.Net - Windows UserName index and length error 4 20
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now