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...)
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...)