How to use pppd to dial up the Internet for supporting 4G modules

来自百问网嵌入式Linux wiki

pppoe拨号上网

4G模块连接开发板

4G模块连接方法请参考淘宝4G模块详情页,连接成功后内核会打印如下信息。
[root@imx6ull:~]# [10445.035851] usb 1-1.2: new high-speed USB device number 5 using ci_hdrc
[10445.216275] option 1-1.2:1.0: GSM modem (1-port) converter detected
[10445.231992] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB0
[10445.251340] option 1-1.2:1.1: GSM modem (1-port) converter detected
[10445.262084] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB1
[10445.281795] option 1-1.2:1.2: GSM modem (1-port) converter detected
[10445.300860] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB2
[10445.314912] option 1-1.2:1.3: GSM modem (1-port) converter detected
[10445.333545] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB3
此时将您的 联通SIM卡插入SIM卡槽。

执行pppd拨号上网

执行拨号上网命令

开发板连接好4G模块后使用如下指令进行拨号上网,系统内默认支持的指令为中国联通,如果你使用的是并非是联通卡,请修改/etc/ppp/gprs-connect-chat 文件内容为你SIM卡运营商的拨号上网指令,让后执行下面命令。
pppd call gprsdial &
执行后如果有类似如下打印信息,表明4G模块拨号联网成功。
rcvd [IPCP ConfNak id=0x2 <addr 10.136.179.50> <ms-dns1 120.80.80.80> <ms-dns2 221.5.88.88>]
sent [IPCP ConfReq id=0x3 <addr 10.136.179.50> <ms-dns1 120.80.80.80> <ms-dns2 221.5.88.88>]
rcvd [IPCP ConfAck id=0x3 <addr 10.136.179.50> <ms-dns1 120.80.80.80> <ms-dns2 221.5.88.88>]
Could not determine remote IP address: defaulting to 10.64.64.64
not replacing existing default route via 192.168.1.1
local  IP address 10.136.179.50
remote IP address 10.64.64.64
primary   DNS address 120.80.80.80
secondary DNS address 221.5.88.88

测试4G模块是否可以联网通信

使用ifconfig命令查看是否出现一个 ppp 开头的网络设备,如下所示,我们4G模块使用的网卡接口为ppp0 ,接下来使用此接口测试是否可以ping通外网。
[root@imx6ull:~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:01:3F:2D:3E:4D
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 00:01:1F:2D:3E:4D
          inet addr:192.168.1.21  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fdfd:3597:aa7a:0:201:1fff:fe2d:3e4d/64 Scope:Global
          inet6 addr: fe80::201:1fff:fe2d:3e4d/64 Scope:Link
          inet6 addr: fdfd:3597:aa7a::914/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:317 errors:0 dropped:0 overruns:0 frame:0
          TX packets:211 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:99472 (97.1 KiB)  TX bytes:27152 (26.5 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:36 errors:0 dropped:0 overruns:0 frame:0
          TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:5067 (4.9 KiB)  TX bytes:5067 (4.9 KiB)

ppp0      Link encap:Point-to-Point Protocol
          inet addr:10.136.179.50  P-t-P:10.64.64.64  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:5 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:62 (62.0 B)  TX bytes:101 (101.0 B)
使用ping -I ppp0 114.114.114.114命令来指定使用ppp0接口单独去ping百度DNS地址,看是否可以联通,如果可以则会有如下提示。
[root@imx6ull:~]# ping -I ppp0 114.114.114.114
PING 114.114.114.114 (114.114.114.114): 56 data bytes
64 bytes from 114.114.114.114: seq=0 ttl=249 time=133.297 ms
64 bytes from 114.114.114.114: seq=1 ttl=249 time=96.049 ms
^C
--- 114.114.114.114 ping statistics ---
10 packets transmitted, 2 packets received, 80% packet loss
round-trip min/avg/max = 96.049/114.673/133.297 ms
[root@imx6ull:~]#

常用指令

移动拨号

#Chat script for China Mobile, used SIMCOM sim4100 TD module.
TIMEOUT 15
ABORT "DELAYED"
ABORT "BUSY"
ABORT "ERROR"
ABORT "NO DIALTONE"
ABORT "NO CARRIER"
TIMEOUT 40
'' \rAT
OK ATS0=0
OK ATE0V1
OK AT+CGDCONT=1,"IP","CMNET"
OK AT+CGEQREQ=1,2,128,384,,,0,,,,,,
OK ATDT*99*1#
CONNECT

联通拨号

#Chat script for China Mobile, used SIMCOM sim4100 TD module.
TIMEOUT 15
ABORT "DELAYED"
ABORT "BUSY"
ABORT "ERROR"
ABORT "NO DIALTONE"
ABORT "NO CARRIER"
TIMEOUT 40
'' \rAT
OK ATS0=0
OK ATE0V1
OK AT+CGDCONT=1,"IP","3GNET"
OK AT+CGEQREQ=1,2,128,384,,,0,,,,,,
OK ATDT*99*1#
CONNECT

发送短信