ARM40-A5应用——GPRS模块ppp拨号上网

ARM40-A5应用——GPRS模块ppp拨号上网

2018.6.17

版权声明:本文为博主原创文章,允许转载。

  ARM40部署提要:
  (1)根据3.1节、3.2节、3.3节方法一,建立各个文件;
  (2)串口/dev/ttyS5连接GPRS-ppp模块;
  (3)source /etc/profile 或直接重启ARM40;
  (4)按4.2节第4步测试。
  (5)按5节第5步配置网关。

一、GPRS模块简介

  RIT-GPRS-22BP型GPRS模块(以下简称GPRS模块),是一款工业级的四频段GSM/GPRS无线模块。具有2路RS232串口,实现串口数据与GPRS网络双向透明传输,设置简单,传输稳定。 也可工作于ppp模式,实现ARM40-A5的拨号上网。
  供电电压5~24VDC,功耗低,工作温度-35°~80°,具有50mm X 32mm X 20mm的超小尺寸,贴片式SIM卡或内嵌式Micro SIM卡,适合各种工业场合应用。
  GPRS模块内部采用Quectel公司的M26,更多GPRS详细数据可参考M26的手册。

二、AT指令测试

  (1)将ARM40的串口(例如/dev/ttyS5)与GPRS模块的TX/RX串口连接;
  (2)查看串口当前的参数,包括波特率、数据位等;
    stty -F /dev/ttyS5 -a
  (3)设置串口波特率为115200,8位数据模式;
    stty -F /dev/ttyS5 ispeed 115200 ospeed 115200 cs8
  (4)设置串口为接收模式;
    cat /dev/ttyS5 &
  (5)AT指令测试(仅示例3条,更多AT指令见《Quectel_M26_AT命令手册_V1.1.pdf》):

发:echo -e "ATHZ\r" > /dev/ttyS5                //发ATHZ指令,复位GPRS模块
收:OK                                           //约8s后复位完成,收到回复OK
发:echo -e "AT\r" > /dev/ttyS5                  //发AT指令
收:OK                                           //收到回复OK
发:echo -e "AT+IPR=115200; &W\r" > /dev/ttyS5   //设置M26的波特率固定为115200
收:OK                                           // 收到回复OK

  (6)测试完毕后,需 fg 后 Ctrl+C,释放 /dev/ttyS5,否则后续的测试可能无法使用 /dev/ttyS5。

三、pppd 拨号上网需要的文件

3.1、脚本文件

 Linux下用 pppd 拨号上网需要一些脚本文件,罗列如下。这些文件的权限应为755.

/etc/ppp/peers/M26-gprs
/etc/ppp/peers/M26-chat-connect
/etc/ppp/peers/M26-chat-disconnect
/etc/ppp/ip-up
/etc/ppp/ppp-off

 这些文件的内容见附录。

 在断网重连文件 /etc/ppp/pon-M26-auto.sh 中会调用到 /etc/ppp/peers/M26-gprs 文件。
 在脚本 /etc/ppp/peers/M26-gprs 中会调用 /etc/ppp/peers/M26-chat-connect 文件和 /etc/ppp/peers/M26-chat-disconnect 文件,在执行时会调用到 /etc/ppp/ip-up 文件 。
 若要关闭pppd进程,需执行 killall pppd,或执行 /etc/ppp/ppp-off 。

3.2、断网重连文件

/etc/ppp/pon-M26-auto.sh #权限应为755,文件内容见附录
chmod 755 /etc/ppp/pon-M26-auto.sh

3.3、上电自启动文件

  方法一:修改 /etc/inittab 和 /etc/profile 文件;推荐使用方法一。
  方法一会在控制台打印pppd的log,方法二不打印。

logintty=$(tty|grep -c "console")  
if [ $logintty -eq 1 ]; then    # ssh,telnet can't get into this line
    /etc/ppp/pon-M26-auto.sh &
fi

 修改 /etc/inittab中的
console::respawn:/sbin/getty -L console 0 vt100 # ENERIC_SERIAL
#console::respawn:-/bin/sh
 为:
#console::respawn:/sbin/getty -L console 0 vt100 # GENERIC_SERIAL
console::respawn:-/bin/sh

  方法二:修改 /etc/init.d/rcS 文件。
  在 /etc/init.d/rcS 文件的最后添加:
  /etc/ppp/pon-M26-auto.sh &

四、测试

4.1、pppd拨号上网

  建立 M26-gprs、M26-chat-connect、M26-chat-disconnect、ip-up 这4个脚本文件后,即可开始测试:
  (1)若第二步中的 cat /dev/ttyS5 & 仍在运行,则需要 fg 后 Ctrl+C,释放 /dev/ttyS5。
  在 M26-gprs 中配置的 /dev/ttyS5 115200,表示pppd会使用/dev/ttyS5,并且波特率为115200;若使用了其它串口,则需要修改 M26-gprs 中的 /dev/ttyS5 为相应的串口。
  (2)pppd call M26-gprs & // 打印信息较长,见附录的 nohup.out 文件
  (3)如果运行没什么问题,ifconfig 可以看到ppp0了。
  (4)ping -I ppp0 8.8.8.8 或 ping -I ppp0 223.5.5.5 // ping一下试试
  (5)killall pppd,或执行 /etc/ppp/ppp-off 。 // 关闭 pppd

4.2、断线后自动重连

  移动通信公司会每隔一段时间(例如3个小时)即重新分配IP,这会导致GPRS模块断线,因此需要断线重连功能。
  文件 pon-M26-auto.sh 的作用是GPRS断线后自动重连,测试如下:
  (6)killall pppd,或执行 /etc/ppp/ppp-off 关闭 pppd。
  (7)/etc/ppp/pon-M26-auto.sh &  //参考《Linux 技巧:让进程在后台可靠运行的几种方法》
  等待约20s后:
  (8)ping -I ppp0 8.8.8.8 或 ping -I ppp0 223.5.5.5 // ping一下试试
  killall pppd,或执行 /etc/ppp/ppp-off 关闭 pppd,模拟断线。等待约40s后,ps观察pppd是否又运行了,测试看 ping 是否又可以了:
  (9)ping -I ppp0 8.8.8.8 或 ping -I ppp0 223.5.5.5 // ping一下试试

4.3、上电自启动

  有如下两种方法,推荐使用方法一。
  方法一会在控制台打印pppd的log,方法二不打印。
  方法一和方法二的测试:

  重新上电后,ps 观察pon-M26-auto.sh和pppd是否上电自启动。
  可能需要等候约30s待M26处于工作状态后,再ping。
  (10)ping -I ppp0 8.8.8.8 或 ping -I ppp0 223.5.5.5 // ping一下试试

4.4、更长时间的断线自动重连测试

  在更长时间的断线自动重连测试中,可以把 log 信息存放在 /var/log/gprs.out 文件中,空闲的时候去查看log信息即可。
  ① 对应4.3中的方法一:
  将 /etc/profile 中的 /etc/ppp/pon-M26-auto.sh & 修改为:
    /etc/ppp/pon-M26-auto.sh >/var/log/gprs.out &
  ② 对应4.3中的方法二:
  将 /etc/init.d/rcS 文件中的 /etc/ppp/pon-M26-auto.sh & 修改为:
    /etc/ppp/pon-M26-auto.sh >/var/log/gprs.out &
  随着时间的延续,gprs.out 文件会不断的增大,因此在最终版本中,不应这样做。
  静置72小时,或更长的时间,通过 gprs.out 文件获知断线后自动重连的信息。

4.5、log信息

  若调试全部完成后,不便存储 gprs.out信息,也不希望在控制台看到log信息。
  ① 对应4.3中的方法一:
  将 /etc/profile 中的 /etc/ppp/pon-M26-auto.sh & 修改为:
    /etc/ppp/pon-M26-auto.sh >/dev/null &
   ② 对应4.3中的方法二:
  将 /etc/init.d/rcS 中的 /etc/ppp/pon-M26-auto.sh & 修改为:
    /etc/ppp/pon-M26-auto.sh >/dev/null &

五、常见问题

  (1)硬件
  串口连线不正确:
    第二步的AT指令测试,即可获知串口连线是否正确。
  电源功率不够:
    M26在发射信号时刻,峰值电流较高;使用12V2A的电源,电源功率是足够的。
  天线不良:
    可通过AT+CSQ获知天线信号是否不良; pppd call M26-gprs & ,在控制器可以看到 AT+CSQ的信息。
  未装SIM卡或SIM卡欠费:
    可通过AT+CPIN获知SIM的信息。
  (2)脚本文件
  脚本文件存放位置不对:
    存放位置需严格与本文相同。
  脚本文件的权限不对:
    需 chmod 755 脚本文件,修改脚本文件的权限为755。
  在复制粘贴的过程中脚本文件字符格式有变:
    例如分号”;”经过复制粘贴,可能变成问号”?”;或者英文字符变成了中文字符;此类错误需在调试过程中,根据报错提示进行修正。
  使用的串口并非/dev/ttyS5:
    需要根据实际情况修改脚本中的串口。
  修改inittab、profile、rcS后,立即重新上电:
    Linux中修改文件后不会立即保存,需要sync 或者使用reboot 重启。
  (3)DNS配置
  ip-up中会配置DNS,如果使用自定义的DNS,可修改如下两句:
echo nameserver 223.5.5.5 > /etc/resolv.conf
echo nameserver 223.6.6.6 >> /etc/resolv.conf
  例如修改为阿里云DNS:
#echo nameserver $DNS1 > /etc/resolv.conf
#echo nameserver $DNS2 >> /etc/resolv.conf
echo nameserver 223.5.5.5 > /etc/resolv.conf
echo nameserver 223.6.6.6 >> /etc/resolv.conf
  (4)诊断信息
  如果在前台运行 pppd,诊断信息输出将显示在屏幕上。如果在后台运行 pppd,诊断信息输出将发送到 /etc/ppp/connect-errors。
  在嵌入式设备中,向flash写log信息,往往有风险,所以在/etc/ppp/pon-EC20-auto.sh 中增加 ln -s /dev/console /etc/ppp/connect-errors,从而将诊断信息转到控制台显示。
  参考pppd源码的 #define _PATH_CONNERRS _ROOT_PATH “/etc/ppp/connect-errors”.
  (5)网关
  若曾经使用eth0、eth1连过网络,则可能设置过网关,例如:
ifconfig eth0 192.168.1.5
route add default gw 192.168.1.1
  再使用ppp0联网时,因之前设置的网关仍旧存在,此时ping 服务器 ping 47.98.140.167 ,可能会报如下错误:
PING 47.98.140.167 (47.98.140.167): 56 data bytes
ping: sendto: Network is unreachable
  需再将ppp0设置为默认路由:
route add default dev ppp0
  再试:
# ping 47.98.140.167
PING 47.98.140.167 (47.98.140.167): 56 data bytes
64 bytes from 47.98.140.167: seq=0 ttl=52 time=978.932 ms
64 bytes from 47.98.140.167: seq=1 ttl=52 time=571.387 ms
^C
— 47.98.140.167 ping statistics —
3 packets transmitted, 2 packets received, 33% packet loss
round-trip min/avg/max = 571.387/775.159/978.932 ms

  或者:
  ① 在/etc/init.d/rcS中增加:

route del -net default
route add -net default netmask 0.0.0.0 gw 192.168.0.1 eth0
#route add -net 192.168.6.0/24 gw 192.168.6.1 eth1
route add -net default netmask 0.0.0.0 gw 192.168.254.254 ppp0

重启ARM40,,eth0和ppp0可连接外网,eth1可以用于调试。

参考文章:

  《Quectel_M26_硬件设计手册_V1.1》
  《Quectel_M26_AT命令手册_V1.1.pdf》
  《Linux 下 ppp 拨号.pdf》
  The Linux GPRS HOWTO
  https://ernstagn.home.xs4all.nl/GPRS-HOWTO/GPRS-HOWTO.html#toc8
  3G and GPRS modems with pppd
  https://wiki.archlinux.org/index.php/3G_and_GPRS_modems_with_pppd
  Linux PPP HOWTO
  http://www.tldp.org/HOWTO/PPP-HOWTO/
  《在 Oracle® Solaris 11.3 中管理使用 UUCP和 PPP 的串行网络》 第 5 部分 串行网络主题
  https://docs.oracle.com/cd/E19253-01/819-7059/modemtm-1/index.html
  《linux系统管理技术手册》 第二版,第12章
  Linux 技巧:让进程在后台可靠运行的几种方法
  荟聚计划:共商 共建 共享 Grant

附:

  (1)脚本 /etc/ppp/peers/M26-gprs

# /etc/ppp/peers/M26-gprs
# Usage:root> pppd call M26-gprs &
#Modem path, like /dev/ttyUSB3,/dev/ttyACM0, depend on your module, default path is /dev/ttyUSB3
/dev/ttyS5 115200
#Insert the username and password for authentication, default user and password are test
user "test" password "test"
# The chat script, customize your APN in this file
connect 'chat -s -v -f /etc/ppp/peers/M26-chat-connect'
# The close script
disconnect 'chat -s -v -f /etc/ppp/peers/M26-chat-disconnect'
# Hide password in debug messages
hide-password
# The phone is not required to authenticate
noauth
# Debug info from pppd
debug
# If you want to use the HSDPA link as your gateway
defaultroute
# pppd must not propose any IP address to the peer
noipdefault
# No ppp compression
novj
novjccomp
noccp
ipcp-accept-local
ipcp-accept-remote
local
persist
#lcp-echo-failure 20
#lcp-echo-interval 300
# For sanity, keep a lock on the serial line
lock
#modem
dump
nodetach
# Hardware flow control
nocrtscts
remotename M26ppp
ipparam M26ppp
ipcp-max-failure 30
# Ask the peer for up to 2 DNS server addresses
usepeerdns

  (2)脚本 /etc/ppp/peers/M26-chat-connect

# /etc/ppp/peers/M26-chat-connect
ABORT "BUSY"
ABORT "NO CARRIER"
ABORT "NO DIALTONE"
ABORT "ERROR"
ABORT "NO ANSWER"
TIMEOUT 60
"" AT~~~
OK AT+IPR=115200
OK ATE0
OK ATI;+CSQ;+CPIN?;+COPS?;+CGREG?
# Insert the APN provided by your network operator, default apn is cmnet
OK AT+CGDCONT=1,"IP","cmnet"
OK ATD*99#
CONNECT

  (3)脚本 /etc/ppp/peers/M26-chat-disconnect

# /etc/ppp/peers/M26-chat-disconnect
ABORT "ERROR"
ABORT "NO DIALTONE"
SAY "\nSending break to the modem\n"
"" +++
"" +++
"" +++
SAY "\nGoodbay\n"

  (4)脚本 /etc/ppp/ip-up

if [ -f /etc/ppp/resolv.conf ]; then
        cp /etc/ppp/resolv.conf /etc/resolv.conf
elif [ -f /var/run/ppp/resolv.conf ]; then
        cp /var/run/ppp/resolv.conf /etc/resolv.conf
else
#        echo nameserver $DNS1 > /etc/resolv.conf
#        echo nameserver $DNS2 >> /etc/resolv.conf
        echo nameserver 223.5.5.5 > /etc/resolv.conf
        echo nameserver 223.6.6.6 >> /etc/resolv.conf
fi

  (5)脚本 /etc/ppp/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

  (6)脚本 /etc/ppp/pon-M26-auto.sh

#!/bin/sh

dns1="223.5.5.5"            #aliyun dns
dns2="223.6.6.6"            #aliyun dns

if [ ! -e /etc/ppp/connect-errors ]; then 
        ln -s /dev/console /etc/ppp/connect-errors
fi

while true
do
        procnum=`ps -ef|grep "pppd"|grep -v grep|wc -l`
        if [ $procnum -eq 0 ]; then
                pppd call M26-gprs &
        fi
        sleep 60

        ping -s 1 -c 1 -w 50 -I ppp0 $dns1 >/dev/null    #ping dns1,1 byte,1 count,timeout is 50s
        if [ "$?" != "0" ]; then                         #ping request timed out
                ping -s 1 -c 2 -w 50 -I ppp0 $dns2       #ping dns2,1 byte,2 count,timeout is 50s
                if [ "$?" != "0" ]; then                 #ping request timed out
                        killall pppd                     #kill pppd
                fi
        fi
done

  (7)nohup.out 文件
  nohup pppd call M26-gprs 2>&1 & 的打印信息存入 nohup.out 文件。

pppd options in effect:                                                                                 
debug # (from /etc/ppp/peers/M26-gprs)                                                        
nodetach # (from /etc/ppp/peers/M26-gprs)                                                
persist # (from /etc/ppp/peers/M26-gprs)                                                        
dump # (from /etc/ppp/peers/M26-gprs)                                                        
noauth # (from /etc/ppp/peers/M26-gprs)                                                        
user test # (from /etc/ppp/peers/M26-gprs)                                                
password ?????? # (from /etc/ppp/peers/M26-gprs)                                                
remotename M26ppp # (from /etc/ppp/peers/M26-gprs)                                        
/dev/ttyS5 # (from /etc/ppp/peers/M26-gprs)                                                
115200 # (from /etc/ppp/peers/M26-gprs)                                                        
lock # (from /etc/ppp/peers/M26-gprs)                                                        
connect chat -s -v -f /etc/ppp/peers/M26-chat-connect # (from /etc/ppp/peers/M26-gprs)        
disconnect chat -s -v -f /etc/ppp/peers/M26-chat-disconnect # (from /etc/ppp/peers/M26-gprs)
nocrtscts # (from /etc/ppp/peers/M26-gprs)                              
local # (from /etc/ppp/peers/M26-gprs)                
hide-password # (from /etc/ppp/peers/M26-gprs)                    
novj # (from /etc/ppp/peers/M26-gprs)                            
novjccomp # (from /etc/ppp/peers/M26-gprs)                    
ipcp-accept-local # (from /etc/ppp/peers/M26-gprs)            
ipcp-accept-remote # (from /etc/ppp/peers/M26-gprs)                
ipparam M26ppp # (from /etc/ppp/peers/M26-gprs)                        
noipdefault # (from /etc/ppp/peers/M26-gprs)                              
ipcp-max-failure 30 # (from /etc/ppp/peers/M26-gprs)                      
defaultroute # (from /etc/ppp/peers/M26-gprs)                              
usepeerdns # (from /etc/ppp/peers/M26-gprs)                              
noccp # (from /etc/ppp/peers/M26-gprs)                                      
abort on (BUSY)                                                                       
abort on (NO CARRIER)                                                                        
abort on (NO DIALTONE)                                                                       
abort on (ERROR)                                                                             
abort on (NO ANSWER)                                                                         
timeout set to 30 seconds                                                                    
send (ATHZ^M)                                                                                
expect (OK)                                                                           
^@^M                                                                                  
OK                                                                                    
 -- got it  

send (AT+IPR=115200^M)                                                                                  
expect (OK)                                                                                             
^M                                                                                                      
^M                                                                                                      
OK                                                                                                      
 -- got it  

send (ATE0^M)                                                   
expect (OK)                                                     
^M                                                              
^M                                                              
OK                                                              
 -- got it                                                                                      

send (ATI;+CSQ;+CPIN?;+COPS?;+CGREG?^M)                                                                 
expect (OK)                                                                                             
^M                                                                                                      
^M                                                                                    
Quectel_Ltd^M                                           
Quectel_M26^M                                                               
Revision: M26FBR03A03-TTS^M                                                 
^M                                                                          
+CSQ: 22,0^M                                                                
^M                                                                              
+CPIN: READY^M                                                                  
^M                                                                                    
+COPS: 0^M                                                                            
^M                                                                                    
+CGREG: 0,2^M                                                                         
^M                                                                                    
OK                                                                                    
 -- got it                                                                                   

send (AT+CGDCONT=1,"IP","cmnet"^M)                                                           
expect (OK)                                                                                  
^M                                                                                           
^M                                                                                           
OK                                                                                    
 -- got it 

send (ATD*99#^M)                                                
expect (CONNECT)                                                
^M                                                              
^M                                                              
CONNECT                                                         
 -- got it                                                                                      

Script chat -s -v -f /etc/ppp/peers/M26-chat-connect finished (pid 2857), status = 0x0                  
Serial connection established.                                                                          
using channel 51                                                                                        
Using interface ppp0                                    
Connect: ppp0 <--> /dev/ttyS5                           
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xe0e51e23> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x1 <asyncmap 0xa0000> <auth pap> <pcomp> <accomp>]
sent [LCP ConfAck id=0x1 <asyncmap 0xa0000> <auth pap> <pcomp> <accomp>]
rcvd [LCP ConfNak id=0x1 <asyncmap 0xa0000>]                    
sent [LCP ConfReq id=0x2 <asyncmap 0xa0000> <magic 0xe0e51e23> <pcomp> <accomp>]
rcvd [LCP ConfAck id=0x2 <asyncmap 0xa0000> <magic 0xe0e51e23> <pcomp> <accomp>]
sent [PAP AuthReq id=0x1 user="test" password=<hidden>]                               
rcvd [PAP AuthAck id=0x1 ""]                                                          
PAP authentication succeeded                                                          
sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns2 0.0.0.0>]         
rcvd [IPCP ConfReq id=0x1 <addr 192.168.254.254>]                                     
sent [IPCP ConfAck id=0x1 <addr 192.168.254.254>]                                     
rcvd [IPCP ConfNak id=0x1 <addr 10.68.62.121> <ms-dns1 112.65.184.255> <ms-dns2 210.22.84.3>]
sent [IPCP ConfReq id=0x2 <addr 10.68.62.121> <ms-dns1 112.65.184.255> <ms-dns2 210.22.84.3>]
rcvd [IPCP ConfAck id=0x2 <addr 10.68.62.121> <ms-dns1 112.65.184.255> <ms-dns2 210.22.84.3>]
not replacing existing default route via 192.168.1.1                            
local IP address 10.68.62.121                                                  
remote IP address 192.168.254.254                                               
primary DNS address 112.65.184.255                                            
secondary DNS address 210.22.84.3                                               
Script /etc/ppp/ip-up started (pid 2862)                                        
Script /etc/ppp/ip-up finished (pid 2862), status = 0x0

猜你喜欢

转载自blog.csdn.net/vonchn/article/details/81902953