Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VB to C#
Message
From
17/04/2009 10:59:36
 
 
To
17/04/2009 07:05:43
General information
Forum:
ASP.NET
Category:
Forms
Title:
Miscellaneous
Thread ID:
01395221
Message ID:
01395272
Views:
78
>Jim,
>
>I haven't gone through all the code, but the first char in a string has offset/index zero and not one
>
>
> replace :  for (int i = 1; i < msgLength; i++)
>
>  by       :for (int i = 0; i < msgLength; i++)
>
>
>
>
>
>>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;
>>        }
>>    }
>>}
>>
Thanks for noticing. I had already tried with a zero offset with no luck. If it helps here is the original C code provided by the manufacturer.
//
// CRC Calculation & Checking routines for Kingfisher Protocol
//
unsigned int kf_crc_calc(BYTE *string, int charnum)
{
int i, j;
int c, crc = 0;
for (i=0; i<charnum; i++)
{ c = string[i] & 0xff;
for (j=0; j<8; j++)
{ if (crc & 0x8000)
{ crc <<= 1;
crc += (((c <<= 1) & 0x100) != 0);
crc ^= 0x1021;
}
else
{ crc <<= 1;
crc += (((c <<= 1) & 0x100) != 0);
}
}
}
return(crc);
}
int kf_crc_check(BYTE *string, int charnum)
{
unsigned int crc1 = kf_crc_calc(string, charnum);
unsigned int crc2 = (int)string[charnum] * 256 + (int)string[charnum+1];
if (crc1 == crc2) return(0);
else return(1);
}
void kf_crc_append(BYTE *string, int charnum)
{
unsigned int crc = kf_crc_calc(string, charnum);
string[charnum] = crc / 256;
string[charnum + 1] = crc % 256;
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform