Checksum Calculator in C# for Elk

GraysonPeddie

Active Member
(Hi. I'm not sure where to post, but I just thought I'd post in software forum... Please move my thread to where my thread belongs if needbe. Thanks.)

I wanted to show you how to calculate checksum for Elk M1 in C#.

[codebox]using System;
using System.Collections.Generic;
using System.Text;

namespace CalculateChecksum
{
class Program
{
static void Main ( string [ ] args )
{
// Start at a checksum of 0.
int checksumCounter = 0;

// Input the string used to calculate checksum.
string sendData;
Console.WriteLine ( "Enter the data used to calculate checksum. " );
Console.WriteLine ( "Leave blank and press [ENTER] for an example." );
Console.Write ( "\n\tInput: " );
sendData = Console.ReadLine ( );

// Perhaps a user just wanted to see an example, thereby leaving it blank...
if ( sendData == "" ) sendData = "13TR012007268750000";
Console.WriteLine ( "\n" ); // Do line carriage return twice.

// It's time to calculate checksum!
Console.WriteLine ( "Data used to calculate checksum: " + sendData );
char[] c = sendData.ToCharArray ( );
byte[] b = new byte[ [ c.Length ];
for ( int i = 0; i < b.Length; i++ )
checksumCounter += b [ i ] = ( byte ) c [ i ];
checksumCounter = ( checksumCounter ^ 0xFF ) + 1;
string checksum = String.Format ( "{0:X}" , checksumCounter );
checksum = checksum.Substring ( checksum.Length - 2 , 2 );

// Go ahead and show the output!
Console.WriteLine ( "Initial Checksum Value: " + checksum );
Console.ReadKey ( );
}
}
}
[/codebox]

You don't need to bother with the example in the Elk RS232 Protocol manual. I hope this helps.

(Edit: Quite weird... When I add syntax highlighting in C# for the codebox, the codebox (or is it the color tags?) discarded the indents...)

(Edit2: Sorry. Retracted...)
 
And by the way: I know, I know! There's Premise! Plrmise can get your server up and running for bla blah blah blah blah! Stop advocating Premise!!! "But--but--Premise is..." Blah blah blah blah blah... / Here's an example if you want to know why... *shrug* Elk Data Logging with PHP Code, Got a spare server?
Making unprovoked smart-ass attacks like that will simply get you ignored by many and hated by some and maybe even banned. Please, either be constructive in your criticism or keep your bashing to yourself.
 
Elk offers, upon request, a free program called M1SDK.exe that will build the length and checksum into a command string and send/receive the data to/from the M1 using the RS-232 port or Ethernet port. It is great for 3rd party code development working with the M1.
 
Here's a python checksum routine. I won't say it's the most efficient, but it does the job.

def checksum (string) :

..sum= 0

..for i in range(len(string)) :
....sum = sum + ord (string)

..rem = sum ^ 255
..rem = rem + 1
..rem = rem + 256
..cc1 = hex(rem)
..cc = cc1.upper()
..p=len(cc)
..return cc[p-2:p]
#end checksum

edited to fix spacing at front...
 
If you're interested in adding code to a community project that is centered around the Elk, check out my code at CodePlex: House.Robot. I do some integration of the Elk M1G with Z-Wave lighting controlled via Microsoft Robotics Studio. It's mainly a project for people who want to play with code and not for people who want an out of the box solution. I ave classes that send and receive a variety of Elk Messages. The article - along with one that does similar things using Windows Workflow is on the MSDN Coding4Fun site.
 
And by the way: I know, I know! There's Premise! Plrmise can get your server up and running for bla blah blah blah blah! Stop advocating Premise!!! "But--but--Premise is..." Blah blah blah blah blah... / Here's an example if you want to know why... *shrug* Elk Data Logging with PHP Code, Got a spare server?
Making unprovoked smart-ass attacks like that will simply get you ignored by many and hated by some and maybe even banned. Please, either be constructive in your criticism or keep your bashing to yourself.


Sorry, GP, I won't take the bait ... I'm saving my strength for the next "Insteon vs the World" smackdown! Woo-hoo!


Sonavagun, I replied to a post without mentioning Pr... well, you know.


Ohh! Post #500! Let the bells ring and the birds fly! :rolleyes:
 
I figured I'd add mine to the collection :angry: :


Code:
		private string checksum(string s)
		{
			int sum = 0;
			foreach (char c in s)
				sum += (int)c;

			sum = -(sum % 256);

			return ((byte)sum).ToString("X2");
		}

Johnny

edit: Ps: it's C#
 
Well... I guess I don't need to declare char[] and byte[], since I can take advantage of foreach. Sometimes, I think a bit too hard during coding.

Thanks!
 
Back
Top