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

How to convert this C or C++ function to VB.NET function

Dear everyone,

I am programming a Serial Port Communicator Interface, my only problem is, that I cannot convert this C (or C++, don't really know which type of) function to work correctly with Visual Basic .NET in Visual Studio 2005.

This function generates a CRC checksum value for a specified command string. The command string start with STX, followed by the command, which can be max. 255 characters long and ends with ETX. In order to get my command working I need this function to calculate the CRC for my string.

The check sum is an 8 bit CRC from the beginning of the text (STX) to the end of the text (ETX)
included all characters (STX/ETX as well). For the message can’t contain control characters, we
send/wait the CRC converted into two characters format in hex.

The function is as follows:

byte crc8(byte *str, int len)
{
int i, f;
byte data;
byte crc;
crc = 0;
while (len--) {
                    data = *str++;
                    for (i = 0; i < 8; ++i) {
                                                     f = 1 & (data ^ crc);
                                                     crc >>= 1;
                                                     data >>= 1;
                                                     if (f) {
                                                              crc ^= 0x8c;
                                                             }
                                                     }
                   }
return crc;
}

What I need is to convert this function to Visual Basic .NET to calculate the correct CRC value for my command string.

This is a most urgent problem, because I cannot continue my project, if I cannot solve this issue.

Thank you for your help,

Cssgyula
0
cssgyula
Asked:
cssgyula
1 Solution
 
cssgyulaAuthor Commented:
Hi there,

After a non-stop 8 hours brainstroming and C++ book-reading I have finally made it possible to convert the code to VB.NET. I have already tested it on the device and is working correctly.

The translation is as follows:

Function Check_CRC(ByVal CommandString As String) As String
        Dim index, i, f As Integer
        Dim crc, data As Byte
        Dim Length As Integer = CommandString.Length
        Dim HexaConstant As Byte = Convert.ToByte("8C", 16)
        crc = 0
        index = 0

        While System.Math.Max(System.Threading.Interlocked.Decrement(Length), Length + 1)
            data = Asc(CommandString(index))
            index += 1
            i = 0
            Do
                f = 1 And (data Xor crc)
                crc >>= 1
                data >>= 1
                If (f) Then
                    crc = crc Xor HexaConstant
                End If
                i += 1
            Loop Until i = 8
        End While
        Dim ResultStr As String = CommandString & Hex(crc)
        Return ResultStr
    End Function

Anyway, thank you for reading this question!
0
 
EE_AutoDeleterCommented:
cssgyula,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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