Chinaunix首页 | 论坛 | 博客
  • 博客访问: 656670
  • 博文数量: 171
  • 博客积分: 2246
  • 博客等级: 大尉
  • 技术积分: 1574
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-31 11:45
文章分类

全部博文(171)

文章存档

2018年(3)

2017年(4)

2015年(1)

2014年(20)

2013年(57)

2012年(86)

分类: 嵌入式

2012-09-05 17:06:07

在嵌入式Linux下GPRS上网方案

笔记1

硬/软件环境

基于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: ×最新版本为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启动脚本)

  1. #!/bin/sh
  2. #define dial_on function
  3. dial_on()
  4. {
  5.        #test if pppd is running
  6.        pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
  7.        if [ $pppd_stat -gt 0 ]
  8.        then
  9.               echo "ppp connection's already started."
  10.        else
  11.               #close ethernet interface
  12.               ifconfig eth0 down
  13.               
  14.               #ppp start
  15.               pppd modem /dev/ttyS1 57600 nocrtscts lock connect "chat -v -f /etc/ppp/gprs-connect" user "" noauth debug defaultroute
  16.               # pppd配置说明:
  17.               # ttyS1:连接GPRS模块SIM300的串口
  18.               # 57600:GPRS的拨号速率
  19.               # nocrtscts:无流控
  20.               # lock:锁定设备
  21.               # connect “chat –v –f /etc/ppp/gprs-connect”:GPRS连接脚本文件
  22.               # user “”:用户名,这里是无
  23.               # noauth:无需认证
  24.               # debug:输出调试信息
  25.               # defaultroute:此拨号连接作为默认路由
  26.               echo "ppp is starting..."
  27.        fi
  28. }
  29.  
  30. #dial on gprs
  31. dial_on
  32. #wait for ppp's init
  33. sleep 5
  34. pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
  35. if [ $pppd_stat -eq 0 ]
  36. then
  37.        echo "trying 2nd time to call ppp"
  38.        dial_on
  39.        sleep 5
  40. fi
  41. pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
  42. if [ $pppd_stat -eq 0 ]
  43. then
  44.        echo "pppd error!"
  45.        echo "please check pppd's config files

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

  1. #!/bin/sh

  2. #get pppd's pid
  3. pid=`pidof pppd`
  4.  
  5. #if pppd process is running
  6. if [ -n $pid ]
  7. then
  8.        #kill pppd
  9.        kill $pid
  10.        
  11.        #open the ethernet interface
  12.        ifconfig eth0 up
  13.        
  14.        echo "ppp connection is closed."
  15. else
  16.        echo "ppp connection isn't existed.

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

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

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

  1. # Secrets for authentication using PAP
  2. # client server secret IP addresses
  3. '' * '' *

说明

(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:

  1. #!/bin/sh
  2. 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:

  1. #!/bin/sh
  2. ######################################################################
  3. #
  4. # Determine the device to be terminated.
  5. #
  6. if [ "$1" = "" ]; then
  7.  DEVICE=ppp0
  8. else
  9.  DEVICE=$1
  10. fi

  11. ######################################################################
  12. #
  13. # If the ppp0 pid file is present then the program is running. Stop it.
  14. if [ -r /var/run/$DEVICE.pid ]; then
  15.         kill -INT `cat /var/run/$DEVICE.pid`
  16. #
  17. # If the kill did not work then there is no process running for this
  18. # pid. It may also mean that the lock file will be left. You may wish
  19. # to delete the lock file at the same time.
  20.         if [ ! "$?" = "0" ]; then
  21.                 rm -f /var/run/$DEVICE.pid
  22.                 echo "ERROR: Removed stale pid file"
  23.                 exit 1
  24.         fi
  25. #
  26. # Success. Let pppd clean up its own junk.
  27.         echo "PPP link to $DEVICE terminated."
  28.         exit 0
  29. fi
  30. #
  31. # The ppp process is not running for ppp0
  32. echo "ERROR: PPP link is not active on $DEVICE"
  33. exit 1

chat-script:

  1. #!/bin/sh
  2. exec chat -v /
  3. TIMEOUT 5 /
  4. ABORT "BUSY" /
  5. ABORT "ERROR" /
  6. ABORT "NO CARRIER" /
  7. '' /rAT /
  8. OK 'AT+CGDCONT=1,"IP","CMNET"' /
  9. OK 'ATDT*99***1#' /
  10. CONNECT '' /

设置DNS的resove.conf:

  1. nameserver 211.136.20.203
  2. nameserver 211.136.17.107

笔记3:

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

开机自动运行,实时监控,断线自动重拨
把文件传到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一个DNS发现真正地掉线,它才会去重新拨号!!此版本经测试通过才发表。

  1.  
  2. **************************
  3. * *
  4. * The Gemini Project *
  5. * *
  6. **************************
  7. ! \' v1 Y# R2 P2 h: K
  8. welcome on your dreambox! - Kernel 2.6.9 (08:14:21).

  9. dreambox login: root

  10. BusyBox v1.01 (2007.10.23-19:23+0000) Built-in shell (ash)
  11. Enter 'help' for a list of built-in commands.

  12. root@dreambox:~> /bin/sh /var/etc/ppp/aa
  13. root@dreambox:~> AT 
  14. OK
  15. ATZ
  16. OK
  17. ATH
  18. OK
  19. ATE1
  20. OK
  21. AT+CGDCONT=1,"IP","cmnet"( f$ m0 V3 U' o
  22. OK
  23. ATD*99***1#
  24. CONNECT
  25. Serial connection established.
  26. using channel 2
  27. Using interface ppp0
  28. Connect: ppp0 <--> /dev/tts/0
  29. Warning - secret file /etc/ppp/pap-secrets has world and/or group access
  30. sent [LCP ConfReq id=0x1
  31. ]
  32. rcvd [LCP ConfAck id=0x1
  33. rcvd [LCP ConfReq id=0x3 <asyncmap 0xa0000> <pcomp> <accomp> <magic 0x2ab30537>
  34. <auth chap MD5>]
  35. sent [LCP ConfNak id=0x3 <auth pap>]
  36. rcvd [LCP ConfReq id=0x5 <asyncmap 0xa0000> <pcomp> <accomp>
  37. ]
  38. sent [LCP ConfAck id=0x5
  39. <auth pap>]
  40. Warning - secret file /etc/ppp/pap-secrets has world and/or group access
  41. sent [PAP AuthReq id=0x1 user="beeline" password=<hidden>]
  42. rcvd [PAP AuthAck id=0x1 ""]
  43. PAP authentication succeeded
  44. sent [CCP ConfReq id=0x1 <deflate 15> <deflate(old#) 15> <bsd v1 15>]
  45. sent [IPCP ConfReq id=0x1 ]
  46. rcvd [LCP ProtRej id=0x6 80 fd 01 01 00 0f 1a 04 78 00 18 04 78]
  47. sent [IPCP ConfReq id=0x1 ]
  48. sent [IPCP ConfReq id=0x1 ]
  49. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  50. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275420253.8 ms

  51. --- 211.95.193.97 ping statistics ---
  52. 1 packets transmitted, 1 packets received, 0% packet loss

  53. round-trip min/avg/max = 275420253.8/275420253.8/275420253.8 ms
  54. sent [IPCP ConfReq id=0x1 ]
  55. sent [IPCP ConfReq id=0x1 ]
  56. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  57. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275425386.3 ms

  58. --- 211.95.193.97 ping statistics ---
  59. 1 packets transmitted, 1 packets received, 0% packet loss9
  60. round-trip min/avg/max = 275425386.3/275425386.3/275425386.3 ms
  61. sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
  62. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  63. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275430517.7 ms

  64. --- 211.95.193.97 ping statistics ---
  65. 1 packets transmitted, 1 packets received, 0% packet loss
  66. round-trip min/avg/max = 275430517.7/275430517.7/275430517.7 ms
  67. sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
  68. sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
  69. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  70. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275435653.2 ms

  71. --- 211.95.193.97 ping statistics ---
  72. 1 packets transmitted, 1 packets received, 0% packet loss
  73. round-trip min/avg/max = 275435653.2/275435653.2/275435653.2 ms
  74. sent [IPCP ConfReq id=0x1 ]
  75. sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
  76. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  77. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275440784.9 ms

  78. --- 211.95.193.97 ping statistics ---
  79. 1 packets transmitted, 1 packets received, 0% packet loss
  80. round-trip min/avg/max = 275440784.9/275440784.9/275440784.9 ms
  81. IPCP: timeout sending Config-Requests
  82. sent [LCP TermReq id=0x2 "No network protocols running"]
  83. rcvd [LCP TermAck id=0x2 "No network protocols running"]
  84. Connection terminated.

  85. Sending break to the modem

  86. PDP context detached
  87. Serial link disconnected.
  88. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  89. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275445915.2 ms

  90. --- 211.95.193.97 ping statistics ---
  91. 1 packets transmitted, 1 packets received, 0% packet loss
  92. round-trip min/avg/max = 275445915.2/275445915.2/275445915.2 ms
  93. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  94. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275451048.9 ms

  95. --- 211.95.193.97 ping statistics ---
  96. 1 packets transmitted, 1 packets received, 0% packet loss
  97. round-trip min/avg/max = 275451048.9/275451048.9/275451048.9 ms
  98. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  99. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275456180.4 ms

  100. --- 211.95.193.97 ping statistics ---
  101. 1 packets transmitted, 1 packets received, 0% packet loss
  102. round-trip min/avg/max = 275456180.4/275456180.4/275456180.4 ms
  103. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  104. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275461314.4 ms

  105. --- 211.95.193.97 ping statistics ---
  106. 1 packets transmitted, 1 packets received, 0% packet loss
  107. round-trip min/avg/max = 275461314.4/275461314.4/275461314.4 m
  108. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  109. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275466446.2 ms
  110. --- 211.95.193.97 ping statistics ---
  111. 1 packets transmitted, 1 packets received, 0% packet loss
  112. round-trip min/avg/max = 275466446.2/275466446.2/275466446.2 ms
  113. PING 211.95.193.97 (211.95.193.97): 1 data bytes
  114. 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275471577.9 ms

  115. --- 211.95.193.97 ping statistics ---
  116. 1 packets transmitted, 1 packets received, 0% packet loss
  117. round-trip min/avg/max = 275471577.9/275471577.9/275471577.9 ms
  118. PING 211.95.193.97 (211.95.193.97): 1 data bytes

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

FROM:http://blog.csdn.net/seven407/article/details/4904877

阅读(5369) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~