搜索
bottom↓
回复: 6

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

[复制链接]

出0入0汤圆

发表于 2020-11-17 17:27:10 | 显示全部楼层 |阅读模式
串口拨号
解压ppp程序包,交叉编译
  1. #tar -xzvf ppp-2.4.5.tar.gz
  2. #cd ./ppp-2.4.5
  3. #./config
  4. #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
  1. #example configuration for a dialup connection authenticated with PAP or CHAP
  2. #
  3. # This is the default configuration used by pon(1) and poff(1).
  4. # See the manual page pppd(8) for information on all the options.
  5. # MUST CHANGE: replace myusername@realm with the PPP login name given to
  6. # your by your provider.
  7. # There should be a matching entry with the password in /etc/ppp/pap-secrets
  8. # and/or /etc/ppp/chap-secrets.
  9. #user "myusername@realm"
  10. # Serial device to which the modem is connected.
  11. # ttyname 串口端口,若非以”/“开头,则会自动补充”/dev/“
  12. /dev/ttyS6
  13. #/dev/ttyUSB2
  14. #NO硬件流控
  15. nocrtscts
  16. # 前台运行
  17. nodetach
  18. # speed 串口波特率
  19. # Speed of the serial line.
  20. #115200
  21. 9600
  22. #hide-password
  23. remotename cmnet
  24. ipparam cmnet
  25. debug
  26. user "cmnet"
  27. 0.0.0.0:0.0.0.0
  28. local
  29. lock
  30. dump
  31. # Assumes that your IP address is allocated dynamically by the ISP.
  32. # 由ISP提供IP
  33. noipdefault
  34. # Try to get the name server addresses from the ISP.
  35. usepeerdns
  36. # Use this connection as the default route.
  37. #defaultroute
  38. # Makes pppd "dial again" when the connection is lost.
  39. persist
  40. # Do not ask the remote to authenticate.
  41. noauth

复制代码


修改拨号脚本 /etc/chatscripts/pap
  1. #you can use this script unmodified to connect to sites which allow
  2. # authentication via PAP, CHAP and similar protocols.
  3. # This script can be shared among different pppd peer configurations.
  4. # To use it, add something like this to your /etc/ppp/peers/ file:
  5. #
  6. # connect "/usr/sbin/chat -v -f /etc/chatscripts/pap -T PHONE-NUMBER"
  7. # user YOUR-USERNAME-IN-PAP-SECRETS
  8. # noauth
  9. # Uncomment the following line to see the connect speed.
  10. # It will be logged to stderr or to the file specified with the -r chat option.
  11. #REPORT                CONNECT
  12. TIMEOUT                15
  13. ABORT                "BUSY"
  14. ABORT                "DELAYED"
  15. ABORT                "ERROR"
  16. ABORT                "NO DIALTONE"
  17. ABORT                "NO CARRIER"
  18. TIMEOUT                40

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

复制代码


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

串口通信测试
打开两个终端
cat /dev/ttyS*
另一个终端发送AT命令
echo “AT” > /dev/ttyS*

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

串口拨号
pppd call provider &
  1. # pppd call provider &
  2. pppd options in effect:
  3. debug                # (from /etc/ppp/peers/provider)
  4. nodetach                # (from /etc/ppp/peers/provider)
  5. persist                # (from /etc/ppp/peers/provider)
  6. dump                # (from /etc/ppp/peers/provider)
  7. noauth                # (from /etc/ppp/peers/provider)
  8. user cmnet                # (from /etc/ppp/peers/provider)
  9. remotename cmnet                # (from /etc/ppp/peers/provider)
  10. /dev/ttyUSB2                # (from /etc/ppp/peers/provider)
  11. 9600                # (from /etc/ppp/peers/provider)
  12. lock                # (from /etc/ppp/peers/provider)
  13. connect /usr/sbin/chat -s -v -f /etc/chatscripts/pap                # (from /etc/ppp/peers/provider)
  14. nocrtscts                # (from /etc/ppp/peers/provider)
  15. local                # (from /etc/ppp/peers/provider)
  16. asyncmap 0                # (from /etc/ppp/options)
  17. lcp-echo-failure 4                # (from /etc/ppp/options)
  18. lcp-echo-interval 30                # (from /etc/ppp/options)
  19. hide-password                # (from /etc/ppp/options)
  20. ipparam cmnet                # (from /etc/ppp/peers/provider)
  21. noipdefault                # (from /etc/ppp/peers/provider)
  22. usepeerdns                # (from /etc/ppp/peers/provider)
  23. :                # (from /etc/ppp/peers/provider)
  24. noipx                # (from /etc/ppp/options)
  25. timeout set to 15 seconds
  26. abort on (BUSY)
  27. abort on (DELAYED)
  28. abort on (ERROR)
  29. abort on (NO DIALTONE)
  30. abort on (NO CARRIER)
  31. timeout set to 40 seconds
  32. send (AT^M)
  33. expect (OK)
  34. AT^M^M
  35. OK
  36. -- got it

  37. send (AT+CSQ^M)
  38. expect (OK)
  39. ^M
  40. AT+CSQ^M^M
  41. +CSQ: 25,99^M
  42. OK
  43. -- got it

  44. send (ATDT*99#^M)
  45. expect (CONNECT)
  46. ^M
  47. ATDT*99#^M^M
  48. CONNECT
  49. -- got it

  50. Script /usr/sbin/chat -s -v -f /etc/chatscripts/pap finished (pid 1448), status = 0x0
  51. Serial connection established.
  52. using channel 1
  53. Using interface ppp0
  54. Connect: ppp0 <--> /dev/ttyUSB2
  55. sent [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xdb90a4ab> <pcomp> <accomp>]
  56. rcvd [LCP ConfReq id=0x0 <asyncmap 0x0> <auth pap> <magic 0xc1e41549> <pcomp> <accomp>]
  57. No auth is possible
  58. sent [LCP ConfRej id=0x0 <auth pap>]
  59. rcvd [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xdb90a4ab> <pcomp> <accomp>]
  60. rcvd [LCP ConfReq id=0x1 <asyncmap 0x0> <magic 0xc1e41549> <pcomp> <accomp>]
  61. sent [LCP ConfAck id=0x1 <asyncmap 0x0> <magic 0xc1e41549> <pcomp> <accomp>]
  62. sent [LCP EchoReq id=0x0 magic=0xdb90a4ab]
  63. sent [CCP ConfReq id=0x1 <deflate 15> <deflate(old#) 15> <bsd v1 15>]
  64. 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>]
  65. rcvd [LCP DiscReq id=0x2 magic=0xc1e41549]
  66. rcvd [LCP EchoRep id=0x0 magic=0xc1e41549 db 90 a4 ab]
  67. rcvd [LCP ProtRej id=0x3 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
  68. Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
  69. rcvd [IPCP ConfReq id=0x0 <addr 10.0.0.1>]
  70. sent [IPCP ConfAck id=0x0 <addr 10.0.0.1>]
  71. rcvd [IPCP ConfRej id=0x1 <compress VJ 0f 01>]
  72. sent [IPCP ConfReq id=0x2 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns2 0.0.0.0>]
  73. rcvd [IPCP ConfNak id=0x2 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
  74. sent [IPCP ConfReq id=0x3 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
  75. rcvd [IPCP ConfAck id=0x3 <addr 100.109.117.176> <ms-dns1 211.136.17.107> <ms-dns2 211.136.20.203>]
  76. local  IP address 100.109.117.176
  77. remote IP address 10.0.0.1
  78. primary   DNS address 211.136.17.107
  79. secondary DNS address 211.136.20.203
  80. Script /etc/ppp/ip-up started (pid 1453)

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

复制代码


#ifconfig
  1. # ifconfig
  2. docker0   Link encap:Ethernet  HWaddr 02:42:28:F3:6C:11  
  3.           inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
  4.           UP BROADCAST MULTICAST  MTU:1500  Metric:1
  5.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  6.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  7.           collisions:0 txqueuelen:0
  8.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

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

  18. lo        Link encap:Local Loopback  
  19.           inet addr:127.0.0.1  Mask:255.0.0.0
  20.           inet6 addr: ::1/128 Scope:Host
  21.           UP LOOPBACK RUNNING  MTU:65536  Metric:1
  22.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  23.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  24.           collisions:0 txqueuelen:0
  25.           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

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


复制代码


可以看到ppp0网卡

添加路由
#route add default dev ppp0
联网测试
#ping 8.8.8.8
  1. PING 8.8.8.8 (8.8.8.8): 56 data bytes
  2. 64 bytes from 8.8.8.8: seq=0 ttl=50 time=44.137 ms
  3. 64 bytes from 8.8.8.8: seq=1 ttl=50 time=41.198 ms
  4. 64 bytes from 8.8.8.8: seq=2 ttl=50 time=47.619 ms
  5. 64 bytes from 8.8.8.8: seq=3 ttl=50 time=40.627 ms
  6. 64 bytes from 8.8.8.8: seq=4 ttl=50 time=46.058 ms
复制代码


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*
  1. /dev/ttyUSB0  /dev/ttyUSB1  /dev/ttyUSB2  /dev/ttyUSB3
复制代码


可以看到模块驱动加载成功,枚举出4个ttyUSB设备,查看模块手册:
ttyUSB0 专用数据业务端口
ttyUSB1 GPS
ttyUSB2 AT指令端口
ttyUSB3 诊断口
修改配置文件 /etc/ppp/peers/provider
  1. /dev/ttyS6
  2. #/dev/ttyUSB2
复制代码

修改为:
  1. #/dev/ttyS6
  2. /dev/ttyUSB2
复制代码


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

如果ls /dev/ttyUSB*看不到设备,可以执行lsusb
  1. # lsusb
  2. Bus 002 Device 003: ID 2949:7401
  3. Bus 002 Device 001: ID 1d6b:0002
  4. Bus 004 Device 001: ID 1d6b:0001
复制代码

查阅模块手册,VID和PID为2949:7401,可以看到已经识别到模块。
手动添加U转串驱动:
  1. sudo echo 2949 7401 > /sys/bus/usb-serial/drivers/option1/new_id
复制代码


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

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

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出100入101汤圆

发表于 2020-11-17 17:52:25 来自手机 | 显示全部楼层
高大上,厉害

出0入0汤圆

发表于 2020-11-17 19:44:11 | 显示全部楼层
请教下出错重新拨号是怎么处理的

出0入0汤圆

发表于 2020-11-17 20:10:44 | 显示全部楼层
4G模块有的提供USB网卡的驱动,这和PPP拨号相比,有什么差异?

出0入0汤圆

发表于 2020-11-18 05:27:38 来自手机 | 显示全部楼层
用modemmanager比裸ppp方便和稳定

出0入0汤圆

 楼主| 发表于 2020-11-18 10:55:00 | 显示全部楼层
qsun 发表于 2020-11-18 05:27
用modemmanager比裸ppp方便和稳定

  等有时间研究一下

出0入89汤圆

发表于 2020-11-18 11:08:13 | 显示全部楼层
jim166783 发表于 2020-11-17 19:44
请教下出错重新拨号是怎么处理的

我们以前是定时监测那个网卡,发现没有,重新拨号,多次不成功后,重启系统,或者重启网卡
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-24 23:53

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表