- 论坛徽章:
- 0
|
谢谢!
#!C:\Perl\bin
# driverreg.pl
# This is a Perl program for Driver Regression Test with Serial Port.
# Run on Windows OS.
#Define title of the software.
format SYSNAME=
========================================
Driver Regression Test System [Ver 1.0]
========================================
.
use File::Copy;
use Win32::SerialPort;
use Time::HiRes qw /usleep/; #Sleep for some time less than 1 second.
#use strict;
# Define some values
my $drvCaseFile = "D:/PerlDev/DrvTest/drvcase.txt";
my $drvTestReport = "D:/PerlDev/DrvTest/DrvTestReport.txt";
my $portCom1;
my $Configuration_File_Name = "D:/PerlDev/DrvTest/Com1Port.cfg";
# Start test.
$| = 1; # Update before write or read.
$~=SYSNAME; #Display the title.
write;
if (-e $drvTestReport) #Delete the report file if exist.
{
print "Exist file $drvTestReport.";
system ("rm -f $drvTestReport");
}
# SerialPort Constructor
unless ($portCom1 = Win32::SerialPort->new ('com1')) {
printf "Could not open port COM1: $^E\n";
exit 1;
}
# Set parameters
$portCom1->user_msg(ON);
$portCom1->baudrate(115200) || die "bad baudrate";
$portCom1->databits(8) || die "bad databits";
$portCom1->parity('none') || die "bad parity";
$portCom1->stopbits(1) || die "bad stopbits";
# After new, must check for failure
$portCom1->write_settings || undef $portCom1;
print "Can't change Device_Control_Block: $^E\n" unless ($portCom1);
print "write_settings done\n";
# Before using start, restart, or tie
$portCom1->save($Configuration_File_Name)
|| warn "Can't save $Configuration_File_Name: $^E\n";
my ($BlockingFlags, $InBytes, $OutBytes, $LatchErrorFlags) = $portCom1->status
|| warn "could not get port status\n";
print "The value of \$BlockingFlags is $BlockingFlags\n";
$portCom1->lookclear;
undef $portCom1;
$portCom1 = tie (*FH, 'Win32::SerialPort', $Configuration_File_Name)
|| die "Can't tie: $^E\n"; ## TIEHANDLE ##
open (CASEFILE, "<$drvCaseFile")||die "$!\nCannot open case file $drvCaseFile\n";
my @comLineArr = <CASEFILE>;
close (CASEFILE);
print "@comLineArr\n";
open (REPORTFILE, ">>$drvTestReport")
||die "$!\nCannot write to report file $drvCaseFile\n";
print (REPORTFILE " This Is The Driver Test Report\n");
foreach my $comLineOrg (@comLineArr)
{
my $startTime = time;
chomp ($comLineOrg);
print "The command line is: $comLineOrg!\n\n";
print (REPORTFILE "\n\n========== Test case:$comLineOrg ==========\n");
@comLine = split (//, $comLineOrg); #Get the character one by one
# sleep 1;
# print "Run to here1\n";
INPUTCMD:
sleep 1;
print "The command by character is: @comLine!\n\n";
foreach my $comChar (@comLine)
{
syswrite (FH, $comChar); ## WRITE ##
usleep (10000);
}
$enterChar = "\r";
syswrite (FH, $enterChar, length($enterChar), 0); ## WRITE ##
my @com1OutArr;
for (my $i = 0;;$i++)
{
$lineFromCom1 = <FH>;
if ($lineFromCom1 ne "")
{
print "The line from COM1 $i: $lineFromCom1";
open (REPORTFILE, ">>$drvTestReport") ||die "$!\nCannot write to report file $drvCaseFile\n";
print (REPORTFILE "$lineFromCom1");
chomp ($lineFromCom1);
chop ($lineFromCom1);
goto INPUTCMD if($lineFromCom1 =~ "Invalid command");
@com1OutArr = split (/>/, $lineFromCom1);
}
my $curTime = time;
my $testPeriod = $curTime - $startTime;
last if (($lineFromCom1 eq $comLineOrg)
||($lineFromCom1 =~ "ss")
# ||($com1OutArr[$#com1OutArr] eq $comLineOrg)
||($com1OutArr[$#com1OutArr] =~ "End of mp3.")
||(300 <= $testPeriod));
}
}
close (REPORTFILE);
close FH || warn "Close failed\n"; ## CLOSE ##
undef $portCom1;
untie *FH; ## DESTROY ##
先有这么多了,就是不能总保证传输的正确性
自己觉得延时的确定和恰当比较关键
否则,不停的读写com1,就会传输出错。
呵呵,不过各位不用串口的,可以不看咯
继续学习!^_^
[ 本帖最后由 Chennysky 于 2006-1-13 14:21 编辑 ] |
|