A-A+

arm中实现pppd连接GPRS上网的相关笔记,含GPRS自动拨号脚本(实

2015年11月25日 站长资讯 暂无评论

笔记1:

在嵌入式Linux下GPRS上网方案

硬/软件环境

基于S3C2410的嵌入式系统,COM1连接PC,COM2连接SIM300 GPRS模块。

该系统运行在Linux 2.6.14操作系统下,使用ppp套件通过SIM300进行PPP拨号。

让Linux内核支持PPP

进入Linux内核目录,执行#make menuconfig

  1. Network Device Support à  
  2.        <*> PPP (point-to-point protocol) support  
  3.        [*]   PPP multilink support  
  4.        <*> PPP support for async serial ports  
  5.        <*> PPP support for sync tty ports  
  6.        <*> SLIP (serial line) support  
  7.        [*]   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套件配置

  1. $(EMB_BIN)/dial-on.sh (GPRS启动脚本)  
  2. #!/bin/sh  
  3.    
  4. #define dial_on function  
  5. dial_on()  
  6. {  
  7.        #test if pppd is running  
  8.        pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`  
  9.        if [ $pppd_stat -gt 0 ]  
  10.        then  
  11.               echo "ppp connection's already started."  
  12.        else  
  13.               #close ethernet interface  
  14.               ifconfig eth0 down  
  15.                 
  16.               #ppp start  
  17.               pppd modem /dev/ttyS1 57600 nocrtscts lock connect "chat -v -f /etc/ppp/gprs-connect" user "" noauth debug defaultroute  
  18.               # pppd配置说明:  
  19.               # ttyS1:连接GPRS模块SIM300的串口  
  20.               # 57600:GPRS的拨号速率  
  21.               # nocrtscts:无流控  
  22.               # lock:锁定设备  
  23.               # connect “chat –v –f /etc/ppp/gprs-connect”:GPRS连接脚本文件   
  24.               # user “”:用户名,这里是无  
  25.               # noauth:无需认证  
  26.               # debug:输出调试信息  
  27.               # defaultroute:此拨号连接作为默认路由  
  28.               echo "ppp is starting..."  
  29.        fi  
  30. }  
  31.    
  32. #dial on gprs  
  33. dial_on  
  34.    
  35. #wait for ppp's init  
  36. sleep 5  
  37.    
  38. pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`  
  39. if [ $pppd_stat -eq 0 ]  
  40. then  
  41.        echo "trying 2nd time to call ppp"  
  42.        dial_on  
  43.          
  44.        sleep 5  
  45. fi  
  46.    
  47. pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`  
  48. if [ $pppd_stat -eq 0 ]  
  49. then  
  50.        echo "pppd error!"  
  51.        echo "please check pppd's config files"  
  52. fi  
  53.    
  54. #open ethernet interface  
  55. ifconfig eth0 up  
  56.    
  57. #end  
  58. $(EMB_BIN)/dial-off.sh (关闭GPRS连接脚本)  
  59. #!/bin/sh  
  60.    
  61. #get pppd's pid  
  62. pid=`pidof pppd`  
  63.    
  64. #if pppd process is running  
  65. if [ -n $pid ]  
  66. then  
  67.        #kill pppd  
  68.        kill $pid  
  69.          
  70.        #open the ethernet interface   
  71.        ifconfig eth0 up  
  72.          
  73.        echo "ppp connection is closed."   
  74. else  
  75.        echo "ppp connection isn't existed."  
  76. fi  
  77.    
  78. #end  
  79. $(EMB_ETC)/ppp/gprs-connect (GPRS连接配置文件)  
  80. #GPRS连接超时设置  
  81. TIMEOUT      60  
  82. #若MODEM遇到BUSY、ERROR、NO CARRIER等信息时,停止拨号  
  83. ABORT   "BUSY"  
  84. ABORT   "ERROR"  
  85. ABORT   "NO CARRIER"  
  86. #外送“AT”指令  
  87. '' AT  
  88. #当得到“OK”回应时,外送AT+CGDCONT=1,"IP","CMNET"命令  
  89. "OK" "AT+CGDCONT=1,/042IP/042,/042CMNET/042"  
  90. #当得到“OK”回应时,外送ATDT*99***1#命令  
  91. "OK" "ATDT*99***1#"  
  92. #当得到“CONNECT”回应时,拨号结束,程序退出  
  93. "CONNECT"  
  94. $(EMB_ETC)/ppp/pap-secrets (GPRS认证配置文件)  
  95. # Secrets for authentication using PAP  
  96. # client    server     secret                    IP addresses   
  97. ''      *     ''      *  

说明:

(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拨号的脚本:

  1. ppp-on:  
  2.    
  3. #!/bin/sh  
  4. 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   
  5.    
  6. ppp-off:  
  7.    
  8. #!/bin/sh  
  9. ######################################################################  
  10. #  
  11. # Determine the device to be terminated.  
  12. #  
  13. if [ "$1" = "" ]; then  
  14.  DEVICE=ppp0  
  15. else  
  16.  DEVICE=$1  
  17. fi  
  18. ######################################################################  
  19. #  
  20. # If the ppp0 pid file is present then the program is running. Stop it.  
  21. if [ -r /var/run/$DEVICE.pid ]; then  
  22.         kill -INT `cat /var/run/$DEVICE.pid`  
  23. #  
  24. # If the kill did not work then there is no process running for this  
  25. # pid. It may also mean that the lock file will be left. You may wish  
  26. # to delete the lock file at the same time.  
  27.         if [ ! "$?" = "0" ]; then  
  28.                 rm -f /var/run/$DEVICE.pid  
  29.   
  30.   
  31.                 echo "ERROR: Removed stale pid file"  
  32.                 exit 1  
  33.         fi  
  34. #  
  35. # Success. Let pppd clean up its own junk.  
  36.         echo "PPP link to $DEVICE terminated."  
  37.         exit 0  
  38. fi  
  39. #  
  40. # The ppp process is not running for ppp0  
  41. echo "ERROR: PPP link is not active on $DEVICE"  
  42. exit 1  
  43. chat-script:  
  44.    
  45. #!/bin/sh  
  46. exec chat -v /  
  47. TIMEOUT 5 /  
  48. ABORT   "BUSY"  /  
  49. ABORT   "ERROR"  /  
  50. ABORT   "NO CARRIER" /  
  51. '' /rAT   /  
  52. OK  'AT+CGDCONT=1,"IP","CMNET"' /  
  53. OK  'ATDT*99***1#'  /  
  54. CONNECT ''  /  

设置DNS的resove.conf:

nameserver 211.136.20.203

nameserver 211.136.17.107

笔记3:

GPRS自动拨号脚本(真正的实时监控,断线自动重拨):
http://www.xiaoxiongboke.com

开机自动运行,实时监控,断线自动重拨,把文件传到DM里,设置文件属性为755,然后把启动路径加到init文件里即可,原设置为5秒去检测一次,是以1字节去PING

  1. #!/bin/sh  
  2. #请把dns1,dns2修改成拼得通的DNS,开机自动运行,实时监控,断线自动重拨  
  3. dns1="211.95.193.97"  
  4. dns2="211.136.20.203"  
  5. sleep 8  
  6. #/bin/pppd call gprs-siem &  
  7. sleep 12  
  8. while true  
  9. do  
  10.        ping -s 1 -c 1 $dns1    #去PING第一个DNS  
  11.        if [ "$?" != "0" ]      #假如PING不通   
  12.        then  
  13.            ping -s 1 -c 2 $dns2  #去PING第二个DNS  
  14.            if [ "$?" != "0" ]     #假如PING不通   
  15.            then     
  16.               killall pppd         #结束PPPD进程  
  17.               pppd call gprs-siem & #再去拨号  
  18.               sleep 12               #等待12秒  
  19.               sleep 5            #如果是PING  DNS2通的话就直接等待5秒   
  20.            fi     
  21.        else  
  22.               sleep 5            #如果是PING  DNS1通的话就直接等待5秒(一般要设置多长时间去PING请改这里)  
  23.        fi                 
  24. done  

大家会问这样一直PING下去担心流量问题,浪费一些流量是垦定的,不过我们是以1个字节去PING 加上返回的值一共是9个字节,也就是说5秒用9个字节

D1 U% ]& i

一个小时用9*12*60是一个小时6480字节=6。328125K

也就是说这样一个小时加6.33K的流量

大家还是担心的话可以改一下脚本,比如改60秒去PING一次啦,等等,都能有效去省流量!!

标签:

给我留言