Skip to content

Commit b42739d

Browse files
Mr-FJCjayceon.fu
andauthored
增加拨号应用文档异常处理章节中设置apn的示例代码 (#10)
* code (cellular): 提交蜂窝无线网卡应用文档中的示例代码 固件版本: N/A 是否需要文案翻译: 是 * 增加拨号应用文档异常处理章节中apn配置例程 * 根据最新要求将示例代码中的注释都换成英文 --------- Co-authored-by: jayceon.fu <jayceon.fu@quectel.com>
1 parent f44d9f5 commit b42739d

6 files changed

+143
-83
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import checkNet
16+
import usocket
17+
import dataCall
18+
from misc import Power
19+
20+
# Configure the APN information according to your actual needs
21+
usrCfg = {'apn': '3gnet', 'username': '', 'password': ''}
22+
23+
def checkAPN():
24+
# Get the APN information of the first cellular NIC and check if the current one is the one you specified
25+
pdpCtx = dataCall.getPDPContext(1)
26+
if pdpCtx != -1:
27+
if pdpCtx[1] != usrCfg['apn']:
28+
# If it is not the APN you need, configure it as follows
29+
ret = dataCall.setPDPContext(1, 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0)
30+
if ret == 0:
31+
print('APN configuration successful. Ready to restart to make APN take effect.')
32+
print('Please re-execute this program after restarting.')
33+
# Make a data call according to the configured information after the module reboots
34+
Power.powerRestart()
35+
else:
36+
print('APN configuration failed.')
37+
return False
38+
else:
39+
print('The APN is correct and no configuration is required')
40+
return True
41+
else:
42+
print('Failed to get PDP Context.')
43+
return False
44+
45+
46+
def main():
47+
checkpass = checkAPN()
48+
if not checkpass:
49+
return
50+
51+
stage, state = checkNet.waitNetworkReady(20)
52+
if stage == 3 and state == 1:
53+
print('Network connected successfully.')
54+
# do something
55+
else:
56+
print('Network connected failed, stage={}, state={}'.format(stage, state))
57+
58+
59+
if __name__ == '__main__':
60+
main()

network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@ def main():
2020
stage, state = checkNet.waitNetworkReady(20)
2121
if stage == 3 and state == 1:
2222
print('Network connected successfully.')
23-
# 创建一个socket对象
23+
# Create a socket object
2424
sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
25-
# 解析域名
25+
# Resolve the domain name
2626
try:
2727
sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1]
2828
except Exception:
2929
print('Domain name resolution failed.')
3030
sock.close()
3131
return
32-
# 建立连接
32+
# Connect to the server
3333
sock.connect(sockaddr)
34-
# 向服务端发送消息
34+
# Send data to the server
3535
ret = sock.send('GET /News HTTP/1.1\r\nHost: python.quectel.com\r\nAccept-Encoding: deflate\r\nConnection: keep-alive\r\n\r\n')
3636
print('send {} bytes'.format(ret))
3737

38-
# 接收服务端消息
38+
# Receive data from the server
3939
data = sock.recv(256)
4040
print('recv {} bytes:'.format(len(data)))
4141
print(data.decode())
4242

43-
# 关闭连接
43+
# Close the connection
4444
sock.close()
4545
else:
4646
print('Network connected failed, stage={}, state={}'.format(stage, state))

network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
import dataCall
1818
from misc import Power
1919

20-
# 用户需要配置的APN信息,根据实际情况修改
20+
# Configure the APN information according to your actual needs
2121
usrCfg = {'apn': '3gnet', 'username': '', 'password': ''}
2222

2323

2424
def checkAPN():
25-
# 获取第一路网卡的APN信息,确认当前使用的是否是用户指定的APN
25+
# Get the APN information of the first cellular NIC and check if the current one is the one you specified
2626
pdpCtx = dataCall.getPDPContext(1)
2727
if pdpCtx != -1:
2828
if pdpCtx[1] != usrCfg['apn']:
29-
# 如果不是用户需要的APN,使用如下方式配置
29+
# If it is not the APN you need, configure it as follows
3030
ret = dataCall.setPDPContext(1, 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0)
3131
if ret == 0:
3232
print('APN configuration successful. Ready to restart to make APN take effect.')
3333
print('Please re-execute this program after restarting.')
34-
# 重启后按照配置的信息进行拨号
34+
# Make a data call according to the configured information after the module reboots
3535
Power.powerRestart()
3636
else:
3737
print('APN configuration failed.')
@@ -52,27 +52,27 @@ def main():
5252
stage, state = checkNet.waitNetworkReady(20)
5353
if stage == 3 and state == 1:
5454
print('Network connected successfully.')
55-
# 创建一个socket对象
55+
# Create a socket object
5656
sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
57-
# 解析域名
57+
# Resolve the domain name
5858
try:
5959
sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1]
6060
except Exception:
6161
print('Domain name resolution failed.')
6262
sock.close()
6363
return
64-
# 建立连接
64+
# Connect to the server
6565
sock.connect(sockaddr)
66-
# 向服务端发送消息
66+
# Send data to the server
6767
ret = sock.send('GET /News HTTP/1.1\r\nHost: python.quectel.com\r\nAccept-Encoding: deflate\r\nConnection: keep-alive\r\n\r\n')
6868
print('send {} bytes'.format(ret))
6969

70-
# 接收服务端消息
70+
# Receive data from the server
7171
data = sock.recv(256)
7272
print('recv {} bytes:'.format(len(data)))
7373
print(data.decode())
7474

75-
# 关闭连接
75+
# Close the connection
7676
sock.close()
7777
else:
7878
print('Network connected failed, stage={}, state={}'.format(stage, state))

network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from misc import Power
1919

2020

21-
# 用户需要配置的APN信息,根据实际情况修改
21+
# Configure the APN information according to your actual needs
2222
usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''}
2323
usrCfg2 = {'profileID': 2, 'apn': '3gwap', 'username': '', 'password': ''}
2424

@@ -29,15 +29,15 @@ def checkAPN(usrCfg, reboot=False):
2929
return False
3030

3131
print('Check the APN configuration of the {} network card.'.format(usrCfg['profileID']))
32-
# 获取网卡的APN信息,确认当前使用的是否是用户指定的APN
32+
# Get the APN information of the cellular NICs and check if the current one is the one you specified
3333
pdpCtx = dataCall.getPDPContext(usrCfg['profileID'])
3434
if pdpCtx != -1:
3535
if pdpCtx[1] != usrCfg['apn']:
36-
# 如果不是用户需要的APN,使用如下方式配置
36+
# If it is not the APN you need, configure it as follows
3737
ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0)
3838
if ret == 0:
3939
print('APN configuration successful.')
40-
# 重启后按照配置的信息进行拨号
40+
# Make a data call according to the configured information after the module reboots
4141
if reboot:
4242
print('Ready to restart to make APN take effect.')
4343
print('Please re-execute this program after restarting.')
@@ -56,28 +56,28 @@ def checkAPN(usrCfg, reboot=False):
5656

5757

5858
def main():
59-
# 使能第一路网卡开机自动激活功能
59+
# Enable automatic activation for NIC1
6060
dataCall.setAutoActivate(1, 1)
61-
# 使能第一路网卡自动重连功能
61+
# Enable automatic reconnection for NIC1
6262
dataCall.setAutoConnect(1, 1)
63-
# 使能第二路网卡开机自动激活功能
63+
# Enable automatic activation for NIC2
6464
dataCall.setAutoActivate(2, 1)
65-
# 使能第二路网卡自动重连功能
65+
# Enable automatic reconnection for NIC2
6666
dataCall.setAutoConnect(2, 1)
6767

68-
# 检查第一路网卡的APN配置,暂时不重启
68+
# Check the APN configuration of NIC1. Do not reboot now.
6969
checkpass = checkAPN(usrCfg1, reboot=False)
7070
if not checkpass:
7171
return
72-
# 检查第二路网卡的APN配置,配置后重启
72+
# Check the APN configuration of NIC2. Reboot the module after configuration.
7373
checkpass = checkAPN(usrCfg2, reboot=True)
7474
if not checkpass:
7575
return
7676

7777
stage, state = checkNet.waitNetworkReady(20)
7878
if stage == 3 and state == 1:
7979
print('Network connected successfully.')
80-
# 分别获取第一路和第二路网卡的IP地址信息
80+
# Get the IP addresses of NIC1 and NIC2
8181
ret1 = dataCall.getInfo(usrCfg1['profileID'], 0)
8282
ret2 = dataCall.getInfo(usrCfg2['profileID'], 0)
8383
print('NIC{}:{}'.format(usrCfg1['profileID'], ret1))
@@ -98,45 +98,45 @@ def main():
9898
print('NIC{} ip:{}'.format(usrCfg2['profileID'], ip_nic2))
9999

100100
print('---------------sock1 test-----------------')
101-
# 创建socket对象
101+
# Create a socket object
102102
sock1 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
103-
# 解析域名
103+
# Resolve the domain name
104104
try:
105105
sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1]
106106
except Exception:
107107
print('Domain name resolution failed.')
108108
sock1.close()
109109
return
110-
# 建立连接
110+
# Connect to the server
111111
sock1.connect(sockaddr)
112-
# 向服务端发送消息
112+
# Send data to the server
113113
ret = sock1.send('GET /News HTTP/1.1\r\nHost: python.quectel.com\r\nAccept-Encoding: deflate\r\nConnection: keep-alive\r\n\r\n')
114114
print('send {} bytes'.format(ret))
115-
# 接收服务端消息
115+
# Receive data from the server
116116
data = sock1.recv(256)
117117
print('recv {} bytes:'.format(len(data)))
118118
print(data.decode())
119-
# 关闭连接
119+
# Close the connection
120120
sock1.close()
121121
print('---------------sock2 test-----------------')
122122
sock2 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.TCP_CUSTOMIZE_PORT)
123123
sock2.bind((ip_nic2, 0))
124124
sock2.settimeout(10)
125-
# 服务器IP和端口,下面的IP和端口仅作示例参考
125+
# Configure server IP address and port number. The IP address and port number below are for example only
126126
server_addr = ('220.180.239.212', 8305)
127-
# 建立连接
127+
# Connect to the server
128128
sock2.connect(server_addr)
129-
# 向服务器发送消息
129+
# Send data to the server
130130
ret = sock2.send('test data.')
131131
print('send {} bytes'.format(ret))
132-
# 接收服务端消息
132+
# Receive data from the server
133133
try:
134134
data = sock2.recv(256)
135135
print('recv {} bytes:'.format(len(data)))
136136
print(data.decode())
137137
except Exception:
138138
print('No reply from server.')
139-
# 关闭连接
139+
# Close the connection
140140
sock2.close()
141141
else:
142142
print('Network connected failed, stage={}, state={}'.format(stage, state))

0 commit comments

Comments
 (0)