召唤脚本达人! Perl 文件读写 [已解决]

sh/bash/dash/ksh/zsh等Shell脚本
回复
feisuzhu
帖子: 31
注册时间: 2008-02-06 13:24
送出感谢: 0
接收感谢: 0
联系:

召唤脚本达人! Perl 文件读写 [已解决]

#1

帖子 feisuzhu » 2009-02-08 22:56

看到网上的教程,说perl不能同时读写一个文件,所以就分别打开了两个
打开/dev/ttyX的时候没问题,但是以写方式打开/dev/ttyACM0(我的N73,作为猫连接上了) 的时候,就阻塞了(已经有一个打开的读句柄,不知道这个东西是不是叫句柄呢?)
有没有办法同时读写呢?

代码: 全选

#!/usr/bin/perl
#################################################
##              SMS Sender V0.01               ##
##        Written by Proton 2009-2-8 21:22:14  ##
#################################################

#For learning purpose
#Going to master perl

$tty="/dev/ttyACM0"; #tty used to communicate with my N73 ^_^

#$tty="/dev/tty8"; #for debug

if($#ARGV<1) #Showing help
{
	print <<EOF;
Proton's SMS Sender V0.01
Usage $0 [Phone number] [Text]
EOF
exit(0);
}

#Get info
($pn,@message) = @ARGV;
$message=join(",",@message);

die ("Incorrect phone number!\n") unless $pn =~ /^(\+86)?\d{11}$/;
$pn="+86$pn" unless $pn =~ /^\+86/;


#Begin sending message
open(TTY,$tty) || die("Unable to open $tty!\n");
open(TTYO,">$tty") || die("Unable to open write handle of $tty!\n"); ## 在这一句阻塞

do
{
	$r=<TTY>;
	print $r;
} while not ($r =~ /^OK$/); #wait for the OK state

print TTYO ("AT+CMGF=1\n");
sleep 0.2;
$r = <TTY>;
die('Error while sending instruction AT+CMGF=1') unless $r =~ /^OK$/;

print TTYO ("AT+CMGS=\"$pn\"\n");
sleep 0.5;
#die("Error while sending instruction AT+CMGS=\"$pn\""); #Don't how to handle it... no error recovery code for now.

print TTYO ("$message\x1A\n");
print "Message sent!\n";
sleep 0.5;
close(TTY);
close(TTYO);
feisuzhu
帖子: 31
注册时间: 2008-02-06 13:24
送出感谢: 0
接收感谢: 0
联系:

Re: 召唤脚本达人! Perl 文件读写 [已解决]

#2

帖子 feisuzhu » 2009-02-09 17:44

唉……果然是自己动手丰衣足食啊!
程序已经能用了,Intrepid Ibex + Nokia N73
不过只能发送英文

代码: 全选

#!/usr/bin/perl
#################################################
##              SMS Sender V0.01               ##
##        Written by Proton 2009-2-8 21:22:14  ##
#################################################

#For learning purpose
#Going to master perl

$tty="/dev/ttyACM0"; #tty used to communicate with my N73 ^_^

#$tty="fifo"; #for debug

if($#ARGV<1) #Showing help
{
	print <<EOF;
Proton's SMS Sender V0.01
Usage $0 [Phone number] [Text]
EOF
	exit(0);
}

#Get info
($pn,@message) = @ARGV;
$message=join(" ",@message);

#die ("Incorrect phone number!\n") unless $pn =~ /^(\+86)?\d{11}$/;
#$pn="+86$pn" unless $pn =~ /^\+86/;


#Begin sending message
open(TTY,"+>$tty") || die("Unable to open $tty!\n");
#open(TTYO,">$tty") || die("Unable to open write handle of $tty!\n"); ## 在这一句阻塞

#print "OPENED";

print TTY ("\rAT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0\r");

while ( not (($r=<TTY>) =~ /OK/)) {}

print TTY ("AT+CMGF=1\r");
while ( not (($r=<TTY>) =~ /OK/)) {}

print TTY ("AT+CMGS=\"$pn\"\r\r\r\r\r\r\r\r");

$r=<TTY>;

print TTY ("$message\x1A\r");
$r=<TTY>;
print TTY ("\rATZ\r");
$r=<TTY>;
print "Message sent!\n";
close(TTY);
附件
Screenshot.png
回复

回到 “Shell脚本”