当前位置:   article > 正文

在ARM-LINUX平台实现4G模块PPP拨号上网(串口/USB)_pppd 串口通信

pppd 串口通信

串口拨号

#1.解压ppp程序包,交叉编译

#tar -xzvf ppp-2.4.5.tar.gz
#cd ./ppp-2.4.5
#./config
#make CC=arm-linux-gcc

内核配置

Device Drivers —>Network device support—> <*> PPP (point-to-point protocol) support

ppp安装

将交叉编译好的pppd、chat应用复制到开发平台下的/usr/sbin/下。将ppp、chatscripts两个文件夹复制到开发平台下的/etc/目录下。

PPP配置

修改配置文件 /etc/ppp/peers/provider

#example configuration for a dialup connection authenticated with PAP or CHAP
#
# This is the default configuration used by pon(1) and poff(1).
# See the manual page pppd(8) for information on all the options.
# MUST CHANGE: replace myusername@realm with the PPP login name given to
# your by your provider.
# There should be a matching entry with the password in /etc/ppp/pap-secrets
# and/or /etc/ppp/chap-secrets.
#user "myusername@realm"
# Serial device to which the modem is connected.
# ttyname 串口端口,若非以”/“开头,则会自动补充”/dev/“
/dev/ttyS6
#/dev/ttyUSB2
#NO硬件流控
nocrtscts
# 前台运行
nodetach
# speed 串口波特率
# Speed of the serial line.
#115200
9600
#hide-password
remotename cmnet
ipparam cmnet
debug
user "cmnet"
0.0.0.0:0.0.0.0
local 
lock
dump
# Assumes that your IP address is allocated dynamically by the ISP.
# 由ISP提供IP
noipdefault
# Try to get the name server addresses from the ISP.
usepeerdns
# Use this connection as the default route.
#defaultroute
# Makes pppd "dial again" when the connection is lost.
persist
# Do not ask the remote to authenticate.
noauth

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

修改拨号脚本 /etc/chatscripts/pap

#you can use this script unmodified to connect to sites which allow
# authentication via PAP, CHAP and similar protocols.
# This script can be shared among different pppd peer configurations.
# To use it, add something like this to your /etc/ppp/peers/ file:
#
# connect "/usr/sbin/chat -v -f /etc/chatscripts/pap -T PHONE-NUMBER"
# user YOUR-USERNAME-IN-PAP-SECRETS
# noauth
# Uncomment the following line to see the connect speed.
# It will be logged to stderr or to the file specified with the -r chat option.
#REPORT		CONNECT
TIMEOUT		15
ABORT		"BUSY"
ABORT		"DELAYED"
ABORT		"ERROR"
ABORT		"NO DIALTONE"
ABORT		"NO CARRIER"
TIMEOUT		40

''		AT
OK 		AT+CSQ
#  s模块APN设置报错,默认接入CMNET,注释掉此命令
#OK 		AT+CGDCONT=1,"IP","CMNET"
 OK		ATDT*99#
CONNECT

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

修改pap权限
chmod +x /etc/chatscripts/pap

串口通信测试

打开两个终端
cat /dev/ttyS*
另一个终端发送AT命令
echo “AT” > /dev/ttyS*

可以cat到 AT OK,通信成功。若通信失败请用模块默认波特率。

串口拨号

pppd call provider &

# pppd call provider &
pppd options in effect:
debug		# (from /etc/ppp/peers/provider)
nodetach		# (from /etc/ppp/peers/provider)
persist		# (from /etc/ppp/peers/provider)
dump		# (from /etc/ppp/peers/provider)
noauth		# (from /etc/ppp/peers/provider)
user cmnet		# (from /etc/ppp/peers/provider)
remotename cmnet		# (from /etc/ppp/peers/provider)
/dev/ttyUSB2		# (from /etc/ppp/peers/provider)
9600		# (from /etc/ppp/peers/provider)
lock		# (from /etc/ppp/peers/provider)
connect /usr/sbin/chat -s -v -f /etc/chatscripts/pap		# (from /etc/ppp/peers/provider)
nocrtscts		# (from /etc/ppp/peers/provider)
local		# (from /etc/ppp/peers/provider)
asyncmap 0		# (from /etc/ppp/options)
lcp-echo-failure 4		# (from /etc/ppp/options)
lcp-echo-interval 30		# (from /etc/ppp/options)
hide-password		# (from /etc/ppp/options)
ipparam cmnet		# (from /etc/ppp/peers/provider)
noipdefault		# (from /etc/ppp/peers/provider)
usepeerdns		# (from /etc/ppp/peers/provider)
:		# (from /etc/ppp/peers/provider)
noipx		# (from /etc/ppp/options)
timeout set to 15 seconds
abort on (BUSY)
abort on (DELAYED)
abort on (ERROR)
abort on (NO DIALTONE)
abort on (NO CARRIER)
timeout set to 40 seconds
send (AT^M)
expect (OK)
AT^M^M
OK
 -- got it

send (AT+CSQ^M)
expect (OK)
^M
AT+CSQ^M^M
+CSQ: 25,99^M
OK
 -- got it

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

Script /usr/sbin/chat -s -v -f /etc/chatscripts/pap finished (pid 1448), status = 0x0
Serial connection established.
using channel 1
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB2
sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xdb90a4ab> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x0 <asyncmap 0x0> <auth pap> <magic 0xc1e41549> <pcomp> <accomp>]
No auth is possible
sent [LCP ConfRej id=0x0 <auth pap>]
rcvd [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xdb90a4ab> <pcomp> <accomp>]
rcvd [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xc1e41549> <pcomp> <accomp>]
sent [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xc1e41549> <pcomp> <accomp>]
sent [LCP EchoReq id=0x0 magic=0xdb90a4ab]
sent [CCP ConfReq id=0x1 <deflate 15> <deflate(old#) 15> <bsd v1 15>]
sent [IPCP ConfReq id=0x1 <compress VJ 0f 01> <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns2 0.0.0.0>]
rcvd [LCP DiscReq id=0x2 magic=0xc1e41549]
rcvd [LCP EchoRep id=0x0 magic=0xc1e41549 db 90 a4 ab]
rcvd [LCP ProtRej id=0x3 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
rcvd [IPCP ConfReq id=0x0 <addr 10.0.0.1>]
sent [IPCP ConfAck id=0x0 <addr 10.0.0.1>]
rcvd [IPCP ConfRej id=0x1 <compress VJ 0f 01>]
sent [IPCP ConfReq id=0x2 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns2 0.0.0.0>]
rcvd [IPCP ConfNak id=0x2 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
sent [IPCP ConfReq id=0x3 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
rcvd [IPCP ConfAck id=0x3 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
local  IP address 100.109.117.176
remote IP address 10.0.0.1
primary   DNS address 211.136.17.107
secondary DNS address 211.136.20.203
Script /etc/ppp/ip-up started (pid 1453)

Script /etc/ppp/ip-up finished (pid 1453), status = 0x0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

#ifconfig

# ifconfig
docker0   Link encap:Ethernet  HWaddr 02:42:28:F3:6C:11  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          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:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth0      Link encap:Ethernet  HWaddr FE:C4:84:AF:B7:CE  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::fcc4:84ff:feaf:b7ce/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:628 errors:0 dropped:0 overruns:0 frame:0
          TX packets:466 errors:4 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:57929 (56.5 KiB)  TX bytes:78074 (76.2 KiB)
          Interrupt:117 

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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ppp0      Link encap:Point-to-Point Protocol  
          inet addr:100.109.117.176  P-t-P:10.0.0.1  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:64 (64.0 B)  TX bytes:97 (97.0 B)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

可以看到ppp0网卡

添加路由
#route add default dev ppp0
联网测试
#ping 8.8.8.8

PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=50 time=44.137 ms
64 bytes from 8.8.8.8: seq=1 ttl=50 time=41.198 ms
64 bytes from 8.8.8.8: seq=2 ttl=50 time=47.619 ms
64 bytes from 8.8.8.8: seq=3 ttl=50 time=40.627 ms
64 bytes from 8.8.8.8: seq=4 ttl=50 time=46.058 ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

ping域名如果失败,请添加DNS
从/etc/ppp/resolv.conf可以查看到当前运营商的DNS“nameserver 211.136.17.107”,将DNS"nameserver 211.136.17.107 # ppp0"添加到/etc/resolv.conf.重新拨号,done。

USB拨号

查看4G模块
ls /dev/ttyUSB*

/dev/ttyUSB0  /dev/ttyUSB1  /dev/ttyUSB2  /dev/ttyUSB3
  • 1

可以看到模块驱动加载成功,枚举出4个ttyUSB设备,查看模块手册:
ttyUSB0 专用数据业务端口
ttyUSB1 GPS
ttyUSB2 AT指令端口
ttyUSB3 诊断口

修改配置文件 /etc/ppp/peers/provider

/dev/ttyS6
#/dev/ttyUSB2
  • 1
  • 2

修改为

#/dev/ttyS6
/dev/ttyUSB2
  • 1
  • 2

拨号测试
拨号和网络配置部分同串口拨号一样。

如果ls /dev/ttyUSB*看不到设备,可以执行lsusb

# lsusb
Bus 002 Device 003: ID 2949:7401
Bus 002 Device 001: ID 1d6b:0002
Bus 004 Device 001: ID 1d6b:0001
  • 1
  • 2
  • 3
  • 4

查阅模块手册,VID和PID为2949:7401,可以看到已经识别到模块。
手动添加U转串驱动:

sudo echo 2949 7401 > /sys/bus/usb-serial/drivers/option1/new_id
  • 1

此时重新ls /dev/ttyUSB*即可看到设备。如果设备重启,需要重新手动添加U转串驱动。

拨号

后续步骤跟串口拨号相同。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/188908
推荐阅读
相关标签
  

闽ICP备14008679号