Here is a "first draft" of a working s-record parser using pack and unpack. (Sorry for the delay. Life got more chaotic and I forgot.)

I am just storing the data in @data. While not efficient, Perl arrays are "sparse", so not eating huge volumes of memory. Also, because the "keys" are numbers (addresses in memory), look up is slightly faster than using a hash (which has to handle any characters in keys).

#!perl -w use warnings; use strict; my @data; sub srecParse { my $sr = $_[0]; # extract record type (as single digit) and data (as pairs of hex +digits) $sr =~ /^S([0-9])((?:[0-9A-Fa-f][0-9A-Fa-f])+)/; (defined($1) and defined($2)) or return warn("Bad format: $_[0]"); my $rt = $1; my $hd = $2; my $bd = pack('H*', $hd); # convert the hexidecimal string to a by +te stream my $cs = unpack('%8C*', $bd); # calculate byte-wise checksum, incl +uding the checksum byte # final result should be 0xFF because checksum byte is 1s compleme +nt of sum of preceding. ($cs == 0xFF) or return warn("Failed checksum: $_[0]"); my ($cnt, $adr, $adrh, $data); if ($rt eq '0') # Text data. Extract like an S1, then display text +. { ($cnt, $adr, $data) = unpack('C n X3 C /a', $bd); # C:count, n +:adr16, /a:data return print STDERR "# " . substr($data, 2, -1) . "\n"; } elsif (($rt eq '1') or ($rt eq '9')) # Data with 16 bit address { ($cnt, $adr, $data) = unpack('C n X3 C /a', $bd); # C:count, n +:adr16, /a:data $cnt -= 2; # adjust byte count, then trim address from in fron +t of the data. $data = substr($data, 2, -1); } elsif (($rt eq '2') or ($rt eq '8')) # Data with 24 bit address { # Because there is no code for a 24 bit integer, unpack 24 bit + address as high byte and low word. ($cnt, $adrh, $adr, $data) = unpack('C C n X4 C /a', $bd); # C +:count, C:adr8 (MSB), n:adr16 (LSW), /a:data $cnt -= 3; # adjust byte count $adr += $adrh * 256; # prepend high byte of address t +o low word $data = substr($data, 3, -1); # trim address from in front of +the data. } elsif (($rt eq '3') or ($rt eq '7')) # Data with 32 bit address { ($cnt, $adr, $data) = unpack('C N X5 C /a', $bd); # C:count, N +:adr32, /a:data $cnt -= 4; # adjust byte count, then trim address from in fron +t of the data. $data = substr($data, 4, -1); } else { return warn("Unsupported record type: $_[0]"); } printf STDERR "c:%d a:%0X d:%s\n", $cnt, $adr, unpack('H*', $data) +; if (($rt >= 1) and ($rt <= 3)) { for my $i (0 .. ($cnt - 1)) { $data[$adr + $i] = substr($data, $i, 1); } } elsif (($rt >= 7) and ($rt <= 9)) { printf STDERR "Start address: 0x%0X\n", $adr; } } my $curFile = ''; while (<>) { print STDERR "Reading $ARGV\n" unless ($ARGV eq $curFile); $curFile = $ARGV; chomp; chomp; srecParse($_); } continue { close ARGV if eof; # Not eof()! }

In reply to Re: Parsing Motorola S-Rec file by RonW
in thread Parsing Motorola S-Rec file by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.