Solved

Converting C code into VB.Net 2008

Posted on 2009-05-20
6
317 Views
Last Modified: 2012-05-07
Hello,

following is the code in C used in Modbus CRC calculation. Can someone convert this into VB.Net code since I am not good at C?

Regards,
MI
void ModbusRTU_CalculateCrc(void)

{

	BYTE ucCount = RESET,ucNoOfBits = RESET;

	WORD iCrc;

	

	iCrc = 0xFFFF ^ (*g_pucCRC_Buffer);

	

	for(ucCount = 1; ucCount <= g_ucCRCBufferCount ; ucCount++)

	{

		for(ucNoOfBits = 0; ucNoOfBits <= 7; ucNoOfBits++)

		{

			if((iCrc & 0x01) == 0X01)

			{

				iCrc >>= 1;

	        	iCrc = iCrc^0xA001;

			}	

			else

			{

				iCrc >>= 1;

            }

	 	}

		

		if(ucCount != g_ucCRCBufferCount)

		{

			iCrc = iCrc^(*(++g_pucCRC_Buffer));

		}

		else

		{

		}

	 }

	 

	//Extract lower byte crc.

	g_ucCRCLowByte = iCrc&0x00FF;

	//Extract higher byte of crc.

	g_ucCRCHighByte = ((iCrc>>8)&0x00FF);

	//End of function ModbusRTU_CalculateCrc()

}

Open in new window

0
Comment
Question by:vj_mi
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24435665
Rather than reinventing the wheel, I did a quick google:
http://modbus.control.com/thread/1026164935

It appears to be VB6 code, but it's quite easy to translate into VB.Net
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24435941
Saying that, I'm a masochist. Best test that it works properly though!
A couple of things: The C code uses DWORD, which is an unsigned 16bit integer. As VB.Net isn't too keen on UInt16, I've used a 32bit integer instead.
I've used a cross between the C code and the VB code in the link. I didn't like the C loop, and I didn't like the VB bitshifting (mainly because VB6 doesn't have a bitshift operator)
Private Shared Function ModbusCRC(ByVal buff As Byte()) As Integer

    Dim crc As Integer = &Hff

    For i As Integer = 0 To buff.Length - 1

        crc = crc Xor buff(i)

        For j As Integer = 0 To 7

            If (crc And &H1) = &H1 Then

                crc >>= 1

                crc = crc And &Ha001

            Else

                crc >>= 1

            End If

        Next

    Next

    

    ' Lower & Upper bytes (if you need them)

    Dim lower As Byte = CByte(crc And &Hff)

    Dim upper As Byte = CByte((crc >> 8) And &Hff00)

    

    Return crc

End Function

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24437096
Translated from your code (mind the notes in the comments)
Public Sub ModbusRTU_CalculateCrc()

    'Assumes you have the next line declared globally elsewhere

    'Dim g_pucCRC_Buffer, g_ucCRCBufferCount, g_ucCRCLowByte, g_ucCRCHighByte As Integer
 

    'Assumes RESET equals 0

    Dim ucCount As Integer = 0, ucNoOfBits As Integer = 0

    Dim iCrc As Integer
 

    iCrc = &HFFFF Xor g_pucCRC_Buffer
 

    For ucCount = 1 To g_ucCRCBufferCount

        For ucNoOfBits = 0 To 7

            If iCrc And &H1 = &H1 Then

                iCrc >>= 1

                iCrc = iCrc Xor &HA001

            Else

                iCrc >>= 1

            End If

        Next
 

        If ucCount <> g_ucCRCBufferCount Then

            g_pucCRC_Buffer += 1

            iCrc = iCrc Xor g_pucCRC_Buffer

        End If

    Next
 

    g_ucCRCLowByte = iCrc And &HFF

    g_ucCRCHighByte = (iCrc >> 8) And &HFF
 

End Sub

Open in new window

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 15

Expert Comment

by:oobayly
ID: 24439308
Just noticed my code calculates upper incorrectly, originally I had done the bitshift after the bitwise and.
Also, ignore my comments on DWORD, I was reading it as WORD by mistake.
Where was my mind when I posted the answer? <sigh>
Dim upper As Byte = CByte((crc >> 8) And &Hff)

Open in new window

0
 

Author Comment

by:vj_mi
ID: 24452937
Hi friends,

I am yet to test the above code snippets since we got bounced by the data that we send to serial port. Unless that code is fixed, I cannot run the above code snipper. Please bear with me for a day or two. Meantime, I will post the serial port issue as a new thread.

Regards,
MI
0
 

Author Comment

by:vj_mi
ID: 24503008
oobayly:
The code you have mentioned (along with the rectification you asked for returns both lower and upper bytes as 0.

kaufmed:
I had to modify the code slightly for it to work...

Regards,
MI
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
System32Int Error 8 56
Limiting string to two deciamls 18 49
Round a string to two digits 12 31
Disable and re-enable a dynamic System.Timers.Timer 6 39
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

912 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

26 Experts available now in Live!

Get 1:1 Help Now