Solved

Porting to C/C++

Posted on 2004-09-12
9
234 Views
Last Modified: 2012-05-05
I have a piece of perl code. can someone help me convert it to C or C++? I have done a lil bit of perl programming but this stuff deals with serial ports. since I have five files ~250 lines each I do not want translation for the complete code. Just translate one file for me with comments explaining how & why?
like so:

========
<Perl code>
----------------
<C-code>
----------------
explanation
========
<Perl code>
----------------
<C-code>
----------------
explanation
========
...


could you also point me to some good perl tutorial sites?

Thank you,
0
Comment
Question by:jhshukla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 9

Author Comment

by:jhshukla
ID: 12038572
#!/usr/bin/perl

# $CSK$

# Copyright (c) 2004 Chris Kuethe <ckuethe@ualberta.ca>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#use warnings;
use strict;

use Device::SerialPort;
use Time::HiRes qw( gettimeofday );
use Time::Local;

# http://search.cpan.org/src/SREZIC/perl-GPS-0.14/NMEA/Handler.pm
# http://www.gpsinformation.org/dale/nmea.htm
# http://www.commlinx.com.au/NMEA_sentences.htm
# http://pcptpp030.psychologie.uni-regensburg.de/trafficresearch/NMEA0183/
# http://vancouver-webpages.com/peter/idx_nmeadoc.html

$SIG{'INT'} = $SIG{'HUP'} = $SIG{'TERM'} = \&bye;
my ($PORT, $sp, $s, $v, %m, %old);
my ($tm, @tm);

$PORT = "/dev/ttyU0";
$sp = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!";
$sp->baudrate(4800)      || die "failed setting baudrate";
$sp->parity("none")      || die "failed setting parity";
$sp->databits(8)      || die "failed setting databits";
$sp->handshake("none")      || die "failed setting handshake";

open(DEV, "<$PORT") || die "Cannot open $PORT: $!\n";

$| = 1;
my $n = "";
while(<DEV>){
      $tm = sprintf("%02d%02d%02d.%03d", (gmtime())[2,1,0], ((gettimeofday())[1])/1000);
      next unless (/^\$(\w{5}),(.+)$/);
      $n .= $_;
      ($s,$v) = ($1, $2);


      $@ = "";

      eval {my $func = \&{"parse_$s"} ; &$func($v); } ;
      print "Unhandled record type: $s\n" if ($@);

#      finish_record() if ($s eq "GPRMC");
}


sub bye{
      #do cleanup stuff here
      close DEV;
      undef $sp;
      die "Caught signal... exiting.\n"
}

sub finish_record{
      my (@k1, @k2, @k3, $k1, $k2, $k3, %h1, %h2, %h3);
      my %ignore = ('backtrack' => 1, 'error' => 1, 'satellites' => 0, 'bearing' => 1);

      system("clear");
#      print $n; $n = "";
      @k1 = sort keys %m;
      foreach $k1 (@k1){
            next if ($ignore{$k1});
            %h1 = %{$m{$k1}};
            @k2 = sort keys %h1;
            foreach $k2 (@k2){
                  if (($k1 eq "satellites") && ($k2 =~ /\d{2}/)){
                        %h2 = %{$h1{$k2}};
                        @k3 = sort keys %h2;
                        foreach $k3 (@k3){
                              print "$k1.$k2.$k3 -> " . $h2{$k3} . "\n";
                        }
                  } else {
                        print "$k1.$k2 -> " . $h1{$k2} . "\n";
                  }
            }
      }

      %old = %m;
      print "-" x 30 . "\n";
      sleep 2;
 }

###################################################

sub parse_GPRTE{
#      print "GPRTE - " . $_[0] . "\n";
}

sub parse_GPGGA{
      (
      $m{'fixdata'}->{'time'},
      $m{'fixdata'}->{'latitude'},
      $m{'fixdata'}->{'lt_hemi'},
      $m{'fixdata'}->{'longitude'},
      $m{'fixdata'}->{'ln_hemi'},
      $m{'fixdata'}->{'quality'},
      $m{'fixdata'}->{'satellites'},
      $m{'fixdata'}->{'precision'},
      $m{'fixdata'}->{'ant_height'},
      undef,
      $m{'fixdata'}->{'geoid_height'},
      undef,
      $m{'fixdata'}->{'dgps_age'},
      $m{'fixdata'}->{'dgps_refid'},
      undef) = split(/[,\*]/ , $_[0]);
      $m{'fixdata'}->{'gtod'} = $tm;
}

sub parse_GPRMC{
      (
      $m{'pvt'}->{'time'},
      $m{'pvt'}->{'status'},
      $m{'pvt'}->{'latitude'},
      $m{'pvt'}->{'lt_hemi'},
      $m{'pvt'}->{'longitude'},
      $m{'pvt'}->{'ln_hemi'},
      $m{'pvt'}->{'speed'},
      $m{'pvt'}->{'course'},
      $m{'pvt'}->{'date'},
      $m{'pvt'}->{'magnetic'},
      $m{'pvt'}->{'mag_dir'},
      undef) = split(/[,\*]/ , $_[0]);
      $m{'pvt'}->{'gtod'} = $tm;
      # print "RMC: $_[0]\n";
}

sub parse_PGRME{
      (
      $m{'error'}->{'err_hor'},
      undef,
      $m{'error'}->{'err_ver'},
      undef,
      $m{'error'}->{'err_sph'},
      undef) = split(/[,\*]/ , $_[0]);
}

sub parse_PGRMZ{
      (
      $m{'altitude'}->{'altitude'},
      undef,
      $m{'altitude'}->{'fix'},
      undef) = split(/[,\*]/ , $_[0]);
}

sub parse_GPGLL{
      (
      $m{'position'}->{'latitude'},
      $m{'position'}->{'lt_hemi'},
      $m{'position'}->{'longitude'},
      $m{'position'}->{'ln_hemi'},
      $m{'position'}->{'time'},
      $m{'position'}->{'status'},
      undef) = split(/[,\*]/ , $_[0]);
      $m{'position'}->{'gtod'} = $tm;
}

sub parse_GPBOD{
      (
      $m{'bearing'}->{'true'},
      undef,
      $m{'bearing'}->{'magnetic'},
      undef,
      $m{'bearing'}->{'finish'},
      $m{'bearing'}->{'origin'},
      undef) = split(/[,\*]/ , $_[0]);
}

sub parse_GPRMB{
      (
      $m{'backtrack'}->{'status'},
      $m{'backtrack'}->{'error'},
      $m{'backtrack'}->{'turn_to'},
      $m{'backtrack'}->{'origin'},
      $m{'backtrack'}->{'dest'},
      $m{'backtrack'}->{'latitude'},
      $m{'backtrack'}->{'lt_hemi'},
      $m{'backtrack'}->{'longitude'},
      $m{'backtrack'}->{'ln_hemi'},
      $m{'backtrack'}->{'distance'},
      $m{'backtrack'}->{'bearing'},
      $m{'backtrack'}->{'velocity'},
      $m{'backtrack'}->{'arrival'},
      undef) = split(/[,\*]/ , $_[0]);
}

sub parse_GPGSV{
      my @w = split(/[,\*]/ , $_[0]);
      $m{'satellites'}->{'constellation'} = $w[2];

      my ($i, @x, $a, $b);
      for($i = 0; $i < ((scalar @w)/4 -1); $i++){
            @x = @w[(4*$i+3)..(4*$i+6)];
            $x[0] += 0; $x[1] += 0; $x[2] += 0; $x[3] += 0;
            if ($x[3]){
                  $m{'satellites'}->{$x[0]}->{'elevation'} = $x[1];
                  $m{'satellites'}->{$x[0]}->{'azimuth'} = $x[2];
                  $m{'satellites'}->{$x[0]}->{'snr'} = $x[3];
                  printf("GPGSV - satellite %d: elevation %d, azimuth %d, SNR %d\n", @x) if ($x[3] > 0);
            }
      }
}

sub parse_GPGSA{
      (
      $m{'active_sat'}->{'mode'},
      $m{'active_sat'}->{'fix'},

      $m{'active_sat'}->{"channel_01"},
      $m{'active_sat'}->{"channel_02"},
      $m{'active_sat'}->{"channel_03"},
      $m{'active_sat'}->{"channel_04"},
      $m{'active_sat'}->{"channel_05"},
      $m{'active_sat'}->{"channel_06"},
      $m{'active_sat'}->{"channel_07"},
      $m{'active_sat'}->{"channel_08"},
      $m{'active_sat'}->{"channel_09"},
      $m{'active_sat'}->{"channel_10"},
      $m{'active_sat'}->{"channel_11"},
      $m{'active_sat'}->{"channel_12"},

      $m{'active_sat'}->{'pdop'},
      $m{'active_sat'}->{'hdop'},
      $m{'active_sat'}->{'vdop'},
      undef) = split(/[,\*]/ , $_[0]);
      # print "GSA: $_[0]\n";
}

in case you want to look at complete code: http://www.ualberta.ca/~ckuethe/gps/gps-kit-cvs.tgz
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 12040523
It would be a big job to translate all this code by hand. Additionally, if you want to run this code you may then also need to convert all the included perl modules to C.

Perl does include a compiler which will take perl code and emit it as C code automaticaaly:
http://www.perldoc.com/perl5.8.4/bin/perlcc.html

Also curious as to why you need to convert this code. What do you expect to gain by using C instead?
0
 
LVL 9

Author Comment

by:jhshukla
ID: 12041058
I want to create a standalone application that uses a GPS sensor. This code allows me to read raw data from the device and output formatted data. the application would be tested on a handheld device in its final phase which obviously would not have perl interpreter installed on it. Furthermore, I need to send the info extracted by this thread to another thread (to be written in C) and combine info from a total of three threads to produce final output.

perlcc does the job so you dont get full credit. but thanks.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 19

Accepted Solution

by:
Kim Ryan earned 75 total points
ID: 12041744
Yes, thanks jmcg for your comments.

jhshukla, the hand conversion of this code to C is a big task, in my opinion much larger than the average EE type of question requires. Your need for C seems to stem from the requirement for portable code and integration with other C programs, not to actually learn C syntax. So I thought that  the perl compiler would quickly and easily solve your problem.

Anyway, could we perhaps look at closing off this question now?
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 12081892
jmcg, if my answer has helped to resolve the issue, I think C is an unfair grading to receive. I think the effort involved to solve it was appropriate for the number of points on offer.
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 12107420
thanks jmcg
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question