Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found