[time-nuts] Oncore Checksums

Matthew Smith matt at smiffytech.com
Thu Apr 17 21:26:37 EDT 2008


Hi Folks

I've been reading various notes on the Oncore protocol that have been 
suggested to me and have written a little bit of Perl that - I think - 
will output the correct set of bytes that should be sent to a receiver 
given a specific message.

If anyone is able to verify that I've got this right (or wrong), it 
would be appreciated, especially the checksum bit.

The code below has been written to display all values as 0xnn for ease 
of reading.  Once I know this is working, I'll convert this to a more 
machine-friendly format.  (Possibly as an array of chars, as this is 
heading for a microcontroller.)

Cheers

M

--
Matthew Smith
Smiffytech - Technology Consulting & Web Application Development
Business: http://www.smiffytech.com/
Personal: http://www.smiffysplace.com/
LinkedIn: http://www.linkedin.com/in/smiffy


#!/usr/bin/perl -w

# Oncore message fixer-upper.

use strict;

# Count number of arguments.
my $argc=@ARGV;

# Check that we have one argument
# and that it begins with @@
if ($argc!=1 || $ARGV[0]!~/^\@\@/)
{
   print "Usage: $0 \@\@[message]\n";
   exit;
}

# Set $instring to be the given
# argument, strip of the initial
# @@.
my $instring=$ARGV[0];
$instring=~s/^\@\@//;

# Start $outstring with the hex
# representation of @@.
my $outstring='0x40 0x40 ';


# Convert each character to hex and
# do checksum stuff.
my ($cs,$last);
for (my $i=0; $i<length($instring); $i++)
{
   my $thischar=ord(substr($instring,$i));
   $outstring.=sprintf("0x%x ",$thischar);

   # Handle the checksum bit.
   if ($i==1)
   {
     # If we're on the 2nd character,
     # XOR this character with the last.
     $cs=$last^$thischar;
   }
   elsif ($i>1)
   {
     # If we're past the 2nd character,
     # XOR this character with the
     # previous checksum.
     $cs=$cs^$thischar;
   }

   $last=$thischar;
}

# Convert the (decimal) checksum to
# hex.
$outstring.=sprintf("0x%x ",$cs);

# Finish off $outstring with the hex
# representation of <CR><LF>.
$outstring.='0x0d 0x0a';

# Return $outstring.
print "$outstring\n";




More information about the time-nuts mailing list