Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VB to C#
Message
From
16/04/2009 19:29:17
 
 
To
All
General information
Forum:
ASP.NET
Category:
Forms
Title:
VB to C#
Miscellaneous
Thread ID:
01395221
Message ID:
01395221
Views:
148
The following VB code returns the correct value
Public Class Form1
    ''//------------------------------------------------------------------------------------------------
    ''// CRC Calculation routine for Kingfisher Protocol
    ''//------------------------------------------------------------------------------------------------
    ''unsigned int kf_crc_calc(char *string, int charnum)
    Private Function crc_calc(ByRef strIn As String, ByRef charnum As Short) As Integer
        Dim i As Short
        Dim j As Short
        Dim c As Integer
        Dim crc As Integer
        Dim tmp As Integer
        Dim ch As String

        For i = 1 To charnum
            ch = (Mid(strIn, i, 1))
            If Len(ch) = 0 Then ch = Chr(&H0)
            c = Asc(ch)
            'Debug.Print "c=" & ch & " : " & Hex(CInt(c))
            For j = 1 To 8
                If (crc And &H8000) > 0 Then

                    crc = crc * 2
                    c = c * 2
                    If (c And &H100) Then

                        crc = crc + 1
                    End If

                    crc = crc Xor &H1021
                    tmp = (crc \ &H10000)
                    crc = crc - (tmp * &H10000)
                Else
                    crc = CInt(crc * 2)
                    c = CInt(c * 2)
                    If (c And &H100) Then
                        crc = crc + 1
                    End If
                    c = (c And &HFFFF)
                    tmp = (crc \ &H10000)
                    crc = crc - (tmp * &H10000)
                    'Debug.Print "(j): " & i & ":" & j & "  c=" & Hex(c) & " crc=" & Hex(crc) & " tmp=" & tmp
                End If
            Next j
            'Debug.Print "I: " & i & ":" & j & "  c=" & Hex(c) & " crc=" & Hex(crc)
        Next i
        Return crc
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim TargetRTU As Integer = 0  'Should be passed in
        Dim ViaRTU As Integer = 0     'Set to the same as target if in Direct Mode
        Dim SourceRTU As Integer = 255 'Set to PC address
        Dim strCommand As String
        Dim c As Char
        c = Chr(8) & Chr(255)
        strCommand = Chr(TargetRTU) & _
                     Chr(8) & _
                     Chr(SourceRTU) & _
                     Chr(ViaRTU) & _
                     Chr(1) & _
                     Chr(34)
        Me.Result.Text = Hex(crc_calc(strCommand, strCommand.Length)).ToString
        Me.TextBox1.Text = c

    End Sub
End Class
This is my attempt to convert to C# which does not return the same value as the VB code. Could someone help me make this conversion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KF_CRC_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalcCRC_Click(object sender, EventArgs e)
        {
            int targetRTU = 0;
            int ViaRTU = 0;
            int SourceRTU = 255;
            
            string strCommand, tempString;


            strCommand = (Convert.ToChar(targetRTU).ToString() + 
                Convert.ToChar(8).ToString() + 
                Convert.ToChar(SourceRTU).ToString() + 
                Convert.ToChar(ViaRTU).ToString() + 
                Convert.ToChar(1).ToString() + 
                Convert.ToChar(34)).ToString();

            this.txtResult.Text  = calcCRC(strCommand, strCommand.Length).ToString();
            //this.txtResult.Text = int.Parse(tempString, System.Globalization.NumberStyles.HexNumber).ToString();


        }

        private int calcCRC(string msg, int msgLength)
        {
            int c, crc = 0, tmp;
            string ch;
            char C = '0';

            for (int i = 1; i < msgLength; i++)
            {
                ch = msg.Substring(i, 1);
                if (ch.Length == 0)
                    C = Convert.ToChar(ch);
                c = (int)C;

                for (int j = 1; j <= 8; j++)
                {
                    if ((crc & 0x8000) > 0)
                    {
                        crc *= 2;
                        c *= 2;
                        if ((c & 0x100) > 0)
                            crc += 1;

                        crc = crc ^ 0x1021;
                        tmp = crc % 0x10000;
                        crc = crc - tmp * 0x10000;
                    }
                    else
                    {
                        crc *= 2;
                        c *= 2;
                        if ((c & 0x100) > 0)
                            crc += 1;
                        c = c & 0xFFFF;
                        tmp = crc % 0x10000;
                        crc = crc - tmp * 0x10000;
                    }
                }//next j
            }//next i

            return crc;
        }
    }
}
Next
Reply
Map
View

Click here to load this message in the networking platform