免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 6907 | 回复: 30
打印 上一主题 下一主题

Perl 如何实现串行通信啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-06 15:46 |只看该作者 |倒序浏览
有人说直接 将 com1 当成文件就可以了
有人建议用 Win32::SerialPort ( 基于 Win32API::CommPort )

各位有什么好建议?有没有相关的源代码供参考一下?谢谢

open(FH,">>COM1") or die "$!\nCant't create";
print FH `dir`;

虽然没有报错,可是我从哪儿查看结果呢?
怎么可以用串口实现 超级终端 的功能,将多个命令顺序执行呢?

谢谢各位

论坛徽章:
0
2 [报告]
发表于 2006-01-09 09:45 |只看该作者
我下载了 Win32::SerialPort ,可是在调用该模块时就出错呢
下载的模块有错,该咱办呢?

D:\PerlDev\Exercise>perl exSerialPort.pl
Can't locate loadable object for module Win32::API in @INC (@INC contains: c:/Pe
rl/lib c:/Perl/site/lib .) at c:/Perl/site/lib/Win32API/CommPort.pm line 5
Compilation failed in require at c:/Perl/site/lib/Win32API/CommPort.pm line 5.
BEGIN failed--compilation aborted at c:/Perl/site/lib/Win32API/CommPort.pm line
5.
Compilation failed in require at c:/Perl/site/lib/SerialPort.pm line 4.
BEGIN failed--compilation aborted at c:/Perl/site/lib/SerialPort.pm line 4.
Compilation failed in require at exSerialPort.pl line 6.
BEGIN failed--compilation aborted at exSerialPort.pl line 6.

论坛徽章:
0
3 [报告]
发表于 2006-01-09 10:55 |只看该作者
#c:/Perl/site/lib/Win32API/CommPort.pm
package Win32API::CommPort;

use Win32;
use Win32::API 0.01;

我的API.pm 究竟应放在哪儿呢?怎么总找不到Win32::API ?

论坛徽章:
0
4 [报告]
发表于 2006-01-09 15:23 |只看该作者
ppm>install Win32-API

就可以了,直接复制到相应目录不一定成功的哟

笨了

论坛徽章:
0
5 [报告]
发表于 2006-01-11 16:46 |只看该作者
呵呵,经过几天的调试,可以通信了,可是需要延时
我的数据不能及时传给 串口,只能逐个字符传输,然后延时
否则就会丢失数据,得不到需要的结果了

读数据时也是这样
如何才可以将所有数据无丢失的读出呢?
继续努力中!
加油哦

论坛徽章:
0
6 [报告]
发表于 2006-01-13 11:08 |只看该作者

回复 5楼 Chennysky 的帖子

看你一个人,兄弟我来陪一程!
虽然解答不了你的问题,但是可以在你的问题中学习!

论坛徽章:
0
7 [报告]
发表于 2006-01-13 13:30 |只看该作者

谢谢!

#!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 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2006-01-13 20:07 |只看该作者
ft呀,这年头连脚本语言也可以读写串口呢

论坛徽章:
0
9 [报告]
发表于 2006-01-16 10:58 |只看该作者

就是就是

不怕做不到,就怕想不到啊!呵呵

对了,目前我还只能传送字符串数据
如何传送 文件呢? 我现在急需要传送文件,各位有什么好建议?
感激!!

论坛徽章:
0
10 [报告]
发表于 2006-01-16 11:35 |只看该作者
其实如果有一个 传送 文件的 命令行程序,我将其嵌入到我的perl程序中也可以的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP