需要帮助使用perl从php套接字服务器解析流数据

时间:2009-05-13 12:05:07

标签: perl parsing sockets stream iostream

我正在为我的公司开发一个项目,该项目使用套接字服务器(php)从远程设备收集数据。如何让这个perl程序直接在流上运行,而不是先让服务器写入tmp文件,然后在该文件上运行该脚本,然后写出一个csv文件以插入数据库?

我考虑过使用IO :: Socket,但我不知道该怎么做。

此外,如果有人有任何关于如何清理此代码的提示/指示,将不胜感激。 (我仍然是Perl n00b; - ))

这是我到目前为止perl prog的内容。(运行后我最终得到两个tmp文件):


#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 个答案:

答案 0 :(得分:1)

如果“直接在流上运行”意味着“接受来自远程设备而不是PHP服务器的连接”,则IO :: Socket是一种可行的方法。 Google已plenty of examples,您还可以查看perlipc文档。