我正在尝试向用于从我们的监控系统发送 SMS 消息的脚本添加一些错误检测。大多数时候它就像一个魅力,但在极少数情况下它会停止发送消息,我们不知道为什么。
我们无法重现该错误,所以我想知道是否有人遇到过类似的问题。下面是发送消息的代码,下面是我可能能够捕捉到错误的想法。
这是发送消息的代码:
sub sendsms {
my ($number,$msg) = @_;
my $store = undef;
my $status = undef;
$msg =~ tr/\\[\]_'^~\{\}\|/\/\(\)\-"??\(\)!/; # \ -> /, [ -> (, ] -> ), ...., strippa út [ ] _ ' ^ ~ { } | \
$msg =~ tr/\xe1\xe9\xed\xf3\xfa\xfd\xf0\xfe\xe6\xf6\xc1\xc9\xcd\xd3\xda\xdd\xd0\xde\xc6\xd6/aeiouydtaoAEIOUYDTAO/;
for (my $i = 0; $i < length($msg); $i++) {
substr($msg,$i,1) = '_' if (ord(substr($msg,$i,1)) < 0x20 || ord(substr($msg,$i,1)) > 0x7f);
}
my $s = substr($msg,0,$maxlen);
RETRY: for (my $t = 0; $t < 5; $t++) {
eval {
put('AT+CMGD='.($next_store+1), 'OK');
put('AT+CSCA="'.$sca.'",145', 'OK');
put('AT+CMGF=1', 'OK');
put('AT+CMGW="'.$number.'",145', '>');
put($s, '>');
my $a = put("\x{1a}", undef);
print "a = '$a'\n" if ($verbose);
if ($a =~ /\+CMGW: ([0-9]*)/) {
$store = $1;
last RETRY;
}
};
if ($@) {
print "ERROR: attempt $t: $@\n";
}
sleep 2*($t+1);
}
if (defined $store) {
print "Message store $store\n" if ($verbose);
put('AT+CMSS='.$store.',"'.$number.'",145',undef);
$next_store = (($store - 1) + 1) % $stores;
}
else {
die "Message not stored";
}
}
放函数:
sub put {
my ($cmd,$expect) = @_;
print "Sending command '$cmd' expecting '$expect'\n" if ($verbose);
$modem->atsend($cmd."\r\n") || die "FAILED send\n";
my $a = $modem->answer();
die "Failed '$cmd' expected '$expect' got '$a'\n" if (defined $expect && !($a =~ /$expect/));
return $a;
}
就像我之前说的,这在 99% 的时间里都有效。错误似乎是消息被写入商店但从未发送过。
我的想法:
eval {'$status = AT+CPAS'};
if (defined $status and ($status == '+CPAS: 1' or $status == '+CPAS: 2')){
print "Modem returned an error status: ".$status;
flush_stores();
sendsms('+xxxxxxxxxx', 'There is an error in the 3G modem');
}
冲洗功能:
sub flush_stores {
for (my $i = 0; $i < $stores; $i++) {
put('AT+CMGD='.($i+1), 'OK');
}
}
这行得通吗?
如果没有,会怎样?
吉斯利