arm中实现pppd连接GPRS上网的相关笔记,含GPRS自动拨号脚本(真正的实时监控,断线自动重拨)

 笔记1:

 

在嵌入式Linux下GPRS上网方案

硬/软件环境

基于S3C2410的嵌入式系统,COM1连接PC,COM2连接SIM300 GPRS模块。
该系统运行在Linux 2.6.14操作系统下,使用ppp套件通过SIM300进行PPP拨号。

让Linux内核支持PPP

进入Linux内核目录,执行#make menuconfig
Network Device Support à
       <*> PPP (point-to-point protocol) support
       [*]   PPP multilink support
       <*> PPP support for async serial ports
       <*> PPP support for sync tty ports
       <*> SLIP (serial line) support
       [*]   CSLIP compressed headers

ppp套件安装

?         下载ppp:ftp://ftp.samba.org/pub/ppp ×最新版本为2.4.4
?         将ppp-2.4.4.tar.gz解压至目录
×这里默认ppp源码目录为$(PPP)
              #tar zxvf ppp-2.4.4.tar.gz
?         然后交叉编译ppp:
              #cd $(PPP)
#./configure
#make CC=/usr/local/arm/3.4.1/bin/arm-linux-gcc ×这里指定交叉编译器
?         将ppp套件安装至嵌入式系统中:
×这里默认可执行文件在嵌入式系统下的目录为$(EMB_BIN)
#cp $(PPP)/chat/chat $(EMB_BIN)
#cp $(PPP)/pppd/pppd $(EMB_BIN)
#cp $(PPP)/pppdump/pppdump $(EMB_BIN)
#cp $(PPP)/pppstats/pppstats $(EMB_BIN)
              ×这里默认嵌入式系统的etc目录为$(EMB_ETC)
              #mkdir $(EMB_ETC)/ppp
              #cp $(PPP)/etc.ppp/* $(EMB_ETC)/ppp

ppp套件配置

$(EMB_BIN)/dial-on.sh (GPRS启动脚本)

#!/bin/sh
 
#define dial_on function
dial_on()
{
       #test if pppd is running
       pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
       if [ $pppd_stat -gt 0 ]
       then
              echo "ppp connection's already started."
       else
              #close ethernet interface
              ifconfig eth0 down
             
              #ppp start
              pppd modem /dev/ttyS1 57600 nocrtscts lock connect "chat -v -f /etc/ppp/gprs-connect" user "" noauth debug defaultroute
              # pppd配置说明:
              # ttyS1:连接GPRS模块SIM300的串口
              # 57600:GPRS的拨号速率
              # nocrtscts:无流控
              # lock:锁定设备
              # connect “chat –v –f /etc/ppp/gprs-connect”:GPRS连接脚本文件
              # user “”:用户名,这里是无
              # noauth:无需认证
              # debug:输出调试信息
              # defaultroute:此拨号连接作为默认路由
              echo "ppp is starting..."
       fi
}
 
#dial on gprs
dial_on
 
#wait for ppp's init
sleep 5
 
pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
if [ $pppd_stat -eq 0 ]
then
       echo "trying 2nd time to call ppp"
       dial_on
      
       sleep 5
fi
 
pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
if [ $pppd_stat -eq 0 ]
then
       echo "pppd error!"
       echo "please check pppd's config files"
fi
 
#open ethernet interface
ifconfig eth0 up
 
#end

$(EMB_BIN)/dial-off.sh (关闭GPRS连接脚本)

#!/bin/sh
 
#get pppd's pid
pid=`pidof pppd`
 
#if pppd process is running
if [ -n $pid ]
then
       #kill pppd
       kill $pid
      
       #open the ethernet interface
       ifconfig eth0 up
      
       echo "ppp connection is closed."
else
       echo "ppp connection isn't existed."
fi
 
#end

$(EMB_ETC)/ppp/gprs-connect (GPRS连接配置文件)

#GPRS连接超时设置
TIMEOUT      60
#若MODEM遇到BUSY、ERROR、NO CARRIER等信息时,停止拨号
ABORT   "BUSY"
ABORT   "ERROR"
ABORT   "NO CARRIER"
#外送“AT”指令
'' AT
#当得到“OK”回应时,外送AT+CGDCONT=1,"IP","CMNET"命令
"OK" "AT+CGDCONT=1,/042IP/042,/042CMNET/042"
#当得到“OK”回应时,外送ATDT*99***1#命令
"OK" "ATDT*99***1#"
#当得到“CONNECT”回应时,拨号结束,程序退出
"CONNECT"

$(EMB_ETC)/ppp/pap-secrets (GPRS认证配置文件)

# Secrets for authentication using PAP
# client    server     secret                    IP addresses
''      *     ''      *

说明

(1)       还需要在$(EMB_ETC)/ppp目录下创建指向$(EMB_ETC)/resolv.conf的链接,用于指定PPP连接的DNS。
(2)       在ppp连接时,需要关闭eth连接。在脚本中已经设置好了,首先关闭eth连接,然后进行ppp连接,在ppp连接完成时,再开启eth连接。
(3)       最好在系统中开启syslogd进程,这样在/var/log/messages文件中会记录GPRS进行拨号的DEBUG信息,便于调试。
(4)       运行拨号脚本后,可以使用#ifconfig查看PPP连接信息。

 

 

笔记2:

 

arm上成功实现ppp拨号的脚本:

 

ppp-on:

 

#!/bin/sh
pppd modem -d -detach lock /dev/ttySAC0 19200 kdebug 4 file /etc/ppp/options crtscts noipdefault netmask 255.255.255.0 defaultroute connect /etc/ppp/chat-script 

 


ppp-off:

 

#!/bin/sh
######################################################################
#
# Determine the device to be terminated.
#
if [ "$1" = "" ]; then
 DEVICE=ppp0
else
 DEVICE=$1
fi

######################################################################
#
# If the ppp0 pid file is present then the program is running. Stop it.
if [ -r /var/run/$DEVICE.pid ]; then
        kill -INT `cat /var/run/$DEVICE.pid`
#
# If the kill did not work then there is no process running for this
# pid. It may also mean that the lock file will be left. You may wish
# to delete the lock file at the same time.
        if [ ! "$?" = "0" ]; then
                rm -f /var/run/$DEVICE.pid
                echo "ERROR: Removed stale pid file"
                exit 1
        fi
#
# Success. Let pppd clean up its own junk.
        echo "PPP link to $DEVICE terminated."
        exit 0
fi
#
# The ppp process is not running for ppp0
echo "ERROR: PPP link is not active on $DEVICE"
exit 1


chat-script:

 

#!/bin/sh
exec chat -v /
TIMEOUT 5 /
ABORT   "BUSY"  /
ABORT   "ERROR"  /
ABORT   "NO CARRIER" /
'' /rAT   /
OK  'AT+CGDCONT=1,"IP","CMNET"' /
OK  'ATDT*99***1#'  /
CONNECT ''  /

 


设置DNS的resove.conf:

 

nameserver 211.136.20.203
nameserver 211.136.17.107

 

 

笔记3:

 

GPRS自动拨号脚本(真正的实时监控,断线自动重拨):

http://www.tvrofans.org/viewthread.php?tid=8117&extra=&page=1

 

开机自动运行,实时监控,断线自动重拨
把文件传到DM里,设置文件属性为755,然后把启动路径加到init文件里即可
原设置为5秒去检测一次,是以1字节去PING
#!/bin/sh
#请把dns1,dns2修改成拼得通的DNS,开机自动运行,实时监控,断线自动重拨
dns1="211.95.193.97"
dns2="211.136.20.203"
sleep 8
#/bin/pppd call gprs-siem &
sleep 12
while true
do
       ping -s 1 -c 1 $dns1    #去PING第一个DNS
       if [ "$?" != "0" ]      #假如PING不通
       then

           ping -s 1 -c 2 $dns2  #去PING第二个DNS
           if [ "$?" != "0" ]     #假如PING不通
           then  
              killall pppd         #结束PPPD进程
              pppd call gprs-siem & #再去拨号
              sleep 12               #等待12秒
              sleep 5            #如果是PING  DNS2通的话就直接等待5秒
           fi  
       else
              sleep 5            #如果是PING  DNS1通的话就直接等待5秒(一般要设置多长时间去PING请改这里)
       fi              
done


大家会问这样一直PING下去担心流量问题,浪费一些流量是垦定的,
不过我们是以1个字节去PING 加上返回的值一共是9个字节,也就是说5秒用9个字节
D1 U% ]& i
一个小时用9*12*60是一个小时6480字节=6。328125K
也就是说这样一个小时加6.33K的流量
大家还是担心的话可以改一下脚本,比如改60秒去PING一次啦,等等,都能有效去省流量!!

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值