当以艺术眼光看程序,寻找程序后面的原理,做到化而不忘
全部博文(57)
分类: PERL
2013-04-18 15:23:06
use strict;
my $SourceFileAddAndName1;
my $NewFileAddrAndName1;
my $SourceFileAddAndName;
my $NewFileAddrAndName;
my $CANHandle;
my $CANNewFile;
*WITH_COMMENT=\1; #特别注意常量的使用方式
*NO_COMMENT=\0;
#注意:这里使用文件句柄时带了$,只有这样才可实现文件句柄作为函数参数传递
open($CANHandle,"D:\\ReversingProblem\\4.ASC") || die "$!";
open($CANNewFile,">D:\\ReversingProblem\\4Filtered.ASC") || die "$!";
#open($CANNewFile,">D:\\Project\\N351_IC\\1013338\\DEV\\TOOLS\\PC\\ManufactureDiagTool\\ManufactureDiagnoics\\LogFiles\\NoObstacleFilted.txt") || die "$!";
#FirstPara:new file handle
#SecondPara:odl file handle
#ThirdPara:Message ID
#FourthParam:the left distance from Rx
#FifthParam:the right distance from Rx
GetUsefulMsg($CANNewFile, $CANHandle, 161,10,19,*NO_COMMENT);
close CANHandle;
close CANNewFile;
open($CANNewFile,"D:\\Project\\N351_IC\\1013338\\DEV\\TOOLS\\PC\\ManufactureDiagTool\\ManufactureDiagnoics\\LogFiles\\NoObstacleFilted.txt") || die "$!";
while(<$CANNewFile>)
{
my $Num2Value = GetDecValueByByteIndexAndNum(1);
#得到一个字节的数据信息
my $DecValue = GetDecValueFromMsg($_,$Num2Value,1);
#print $DecValue, "," ,$Num2Value, "...\n";
}
###############################################################
################
###############################################################
sub ProcessFileAddress
{
my ($FileAddrAndName)=@_;
#"\-->\\",将一条反w斜线换成二条
$FileAddrAndName=~s/\\/\\\\/;
return $FileAddrAndName;
}
sub GetUsefulMsg
{
my ($NewFileHandle,$SourceFileHandle,$MsgID,$StartDis,$EndDis,$CommentInfo)=@_;
while(<$SourceFileHandle>)
{
#两个空格中间夹着消息ID,其格式如:
#0.175826 1 151 Tx d 8 00 03 00 80 00 00 00 00 Length = 960000 BitCount = 123 ID = 337
#0.184991 1 221 Tx d 8 29 55 2E F1 00 00 00 00 Length = 928000 BitCount = 119 ID = 545
#0.186886 1 161 Tx d 8 02 FF FF FF 00 00 00 00 Length = 952000 BitCount = 122 ID = 353
#添加后的效果
#0.186886 1 161 Tx d 8 02 [FF FF FF] 00 00 00 00 Length = 952000 BitCount = 122 ID = 353
if($_=~/\s$MsgID\s/) #正则表达式中也可以实现变量内插
{
if($CommentInfo == *WITH_COMMENT)#如果要添加注释
{
#the two line is used to comment the data
#使用()实现a匹配捕捉,
#添加中括号,以便于查看
#这里的\1表示小括号中的a匹配的内容,好像用$1也是可以的
s/Rx(.{$StartDis})/Rx\1\[/;#添加d左中括号
s/Rx(.{$EndDis})/Rx\1\]/;#添加右中括号
}
print $NewFileHandle $_;#将结果r打印到文件,注意文件句柄后面没有逗号
}
}
}
#first Param:the index of the request message
sub GetDecValueByByteIndexAndNum
{
#得到Rx在一个串中开始的索引值,因为时间不同,相应的字符串的长度也不同,
#Rx位于字符串的索引也是不同的
my ($ByteIndex)=@_;
my $RevDis=index($_,"Rx ");
if($RevDis != -1)
{
return $RevDis+9+3*$ByteIndex;
}
return undef;
}
#first param:message line,type $
#second Param:the left distance from Rx
#third Param:the number
sub GetDecValueFromMsg
{
my ($LineContent,$LeftDisFromRx, $ByteNum)=@_;
#将串分割成数组,当成数字来对待
my @ContentArray=split(//,$LineContent);
#从CAN的8字节消息中提取数据信息,这里提取的信息可能是1,2,3,4字节,
my $HexValue=0;
if($ByteNum==1)
{
];
}
elsif($ByteNum==2)
{
]
];
}
elsif($ByteNum==3)
{
]
]
];
}
elsif($ByteNum==4)
{
]
]
]
];
}
else
{
$HexValue=undef;
}
return hex($HexValue);
}