Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 208575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:50:16+00:00 2026-05-11T17:50:16+00:00

I’m working on a project for my company that uses a socket server (php)

  • 0

I’m working on a project for my company that uses a socket server (php) to gather data from a remote device. How can I make this perl program run directly on the stream instead of first having the server write to a tmp file then running this script on that file then writing out a csv file for insertion into a database?

I thought about using IO::Socket but I’m not sure how to go about it.

Also, If anyone has any tips/pointers on how to clean up this code it would be appreciated. (I’m still very much a Perl n00b ;-))

Here’s what I have so far for the perl prog.( I wind up with two tmp files after this is run):


#vim set sw=2 ts=2
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
&convert;

#open the source file, strip out any unneeded chars, reformat the data
sub convert{
    my $source = "$ARGV[0]";
    my $dest = "$ARGV[0]"."_tmp.txt";
    chomp $source;
    open SOURCE, '', $dest or die "Could not open '$dest' $!";

    # move the data from the source file into an array
    my %fields;
    my @field_names = qw/FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6/;
    my $pack_definition = 'a4 a2 a1 a4 a4 a8A*'; 
    while(){

        # strip out the packet header 
        s#T18##g;
        s#T00##g;
        s/^(FF14).*$//g;
        s/^(FF18).*$//g;
        s/(^|\n)[\n\s]*/$1/g;

        # arrange the data into the necessary order
        @fields{@field_names} = unpack($pack_definition, $_);
        s#(FF15)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})#$1\t$2\t$3\t$5$4\t$7$6\t$11$10$9$8#g;
        s#(FF16)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})#$1\t$2\t$3\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t$11#g;
        s#(FF17)(.{2})(.{1})(.{2})(.{2})(.{2})(.{2})(.{4})(.{4})#$1\t$2\t$3\t$5$4\t$7$6\t$8\t$9#g;
        my @spds =  /(FF15)\t(.{2})\t(.{1})\t(.{4})\t(.{4})\t(.{8})/;

        # convert the data from hex to ascii
        foreach my $data (@spds){
            my $replacement = hex($data);  
            s#$data#$replacement#g;
        }
        my @psis = /(FF16)\t(.{2})\t(.{1})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})\t(.{2})/;
        foreach my $data1(@psis){
            my $replacement1 = hex($data1);
            s#$data1#$replacement1#g;
        }
        my @rates=  /(FF17)\t(.{2})\t(.{1})\t(.{4})\t(.{1,4})\t(.{4})\t(.{4})/;
        foreach my $data2 (@rates){
            my $replacement2 = hex($data2);  
            s#$data2#$replacement2#g;
        }

        # print the converted data to the destination file
        print DEST;
    }
    # close the files
    close SOURCE;
    close DEST;
}
&create_vals;

# perform conversion from raw values to human readable output
sub create_vals{      
    my $source = "$ARGV[0]"."_tmp.txt";
    my $dest = "$ARGV[0]"."_converted.txt";
    chomp $source;
    open SOURCE, '', $dest or die "Cannot open '$dest' for writing $!";
    while(){
        s#(65301)\t(.{2})\t(8)\t(.{1,5})\t(.{1,5})\t(.{1,4})#"'".$1."','". $2."','". $3."','". ($4/8)."','". ($5/8)."','". ($6/20)."'"#eg;
        s#(65302)\t(.{2})\t(8)\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})\t(.{1,3})#"'".$1."','". $2."','". $3."','". $4."','". (($5*1.8)-40)."','". (($6*1.8)-40)."','". ($7*.58)."','".($8*.58)."','".($9*.29008)."','".(($10*1.8)-40)."','".$11."'"#eg;
        s#(65303)\t(.{2})\t(8)\t(.{1,5})\t(.{1,5})\t(.{1,5})\t(.{1,5})#"'".$1."','". $2."','". $3."','". ($4*0.014)."','". ($5*.05)."'"#eg;
        print DEST;
    }
}
    close SOURCE;
    close DEST;
__END__
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:50:17+00:00Added an answer on May 11, 2026 at 5:50 pm

    If “run directly on the stream” means “accept connections from a remote device instead of the PHP server” then IO::Socket is a way to go. Google has plenty of examples and you can also check out perlipc documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Cracked it! One can use COleClientItem object to obtain an… May 11, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer NHibernate works this way within a single session (Unit Of… May 11, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer The following should do the trick... var $liWrapper = $("<li… May 11, 2026 at 9:05 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.