diff --git a/network-comm/nic/cellular/example_network_exception_handle_set_apn.py b/network-comm/nic/cellular/example_network_exception_handle_set_apn.py new file mode 100644 index 0000000..4bd7c2d --- /dev/null +++ b/network-comm/nic/cellular/example_network_exception_handle_set_apn.py @@ -0,0 +1,60 @@ +# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import checkNet +import usocket +import dataCall +from misc import Power + +# Configure the APN information according to your actual needs +usrCfg = {'apn': '3gnet', 'username': '', 'password': ''} + +def checkAPN(): + # Get the APN information of the first cellular NIC and check if the current one is the one you specified + pdpCtx = dataCall.getPDPContext(1) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # If it is not the APN you need, configure it as follows + ret = dataCall.setPDPContext(1, 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) + if ret == 0: + print('APN configuration successful. Ready to restart to make APN take effect.') + print('Please re-execute this program after restarting.') + # Make a data call according to the configured information after the module reboots + Power.powerRestart() + else: + print('APN configuration failed.') + return False + else: + print('The APN is correct and no configuration is required') + return True + else: + print('Failed to get PDP Context.') + return False + + +def main(): + checkpass = checkAPN() + if not checkpass: + return + + stage, state = checkNet.waitNetworkReady(20) + if stage == 3 and state == 1: + print('Network connected successfully.') + # do something + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py b/network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py index 3ca3be9..bf42147 100644 --- a/network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py +++ b/network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py @@ -20,27 +20,27 @@ def main(): stage, state = checkNet.waitNetworkReady(20) if stage == 3 and state == 1: print('Network connected successfully.') - # 创建一个socket对象 + # Create a socket object sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) - # 解析域名 + # Resolve the domain name try: sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] except Exception: print('Domain name resolution failed.') sock.close() return - # 建立连接 + # Connect to the server sock.connect(sockaddr) - # 向服务端发送消息 + # Send data to the server 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') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server data = sock.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) - # 关闭连接 + # Close the connection sock.close() else: print('Network connected failed, stage={}, state={}'.format(stage, state)) diff --git a/network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py b/network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py index 19063fa..ef7c6c7 100644 --- a/network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py +++ b/network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py @@ -17,21 +17,21 @@ import dataCall from misc import Power -# 用户需要配置的APN信息,根据实际情况修改 +# Configure the APN information according to your actual needs usrCfg = {'apn': '3gnet', 'username': '', 'password': ''} def checkAPN(): - # 获取第一路网卡的APN信息,确认当前使用的是否是用户指定的APN + # Get the APN information of the first cellular NIC and check if the current one is the one you specified pdpCtx = dataCall.getPDPContext(1) if pdpCtx != -1: if pdpCtx[1] != usrCfg['apn']: - # 如果不是用户需要的APN,使用如下方式配置 + # If it is not the APN you need, configure it as follows ret = dataCall.setPDPContext(1, 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) if ret == 0: print('APN configuration successful. Ready to restart to make APN take effect.') print('Please re-execute this program after restarting.') - # 重启后按照配置的信息进行拨号 + # Make a data call according to the configured information after the module reboots Power.powerRestart() else: print('APN configuration failed.') @@ -52,27 +52,27 @@ def main(): stage, state = checkNet.waitNetworkReady(20) if stage == 3 and state == 1: print('Network connected successfully.') - # 创建一个socket对象 + # Create a socket object sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) - # 解析域名 + # Resolve the domain name try: sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] except Exception: print('Domain name resolution failed.') sock.close() return - # 建立连接 + # Connect to the server sock.connect(sockaddr) - # 向服务端发送消息 + # Send data to the server 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') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server data = sock.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) - # 关闭连接 + # Close the connection sock.close() else: print('Network connected failed, stage={}, state={}'.format(stage, state)) diff --git a/network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py b/network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py index 4a1ed91..7131a82 100644 --- a/network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py +++ b/network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py @@ -18,7 +18,7 @@ from misc import Power -# 用户需要配置的APN信息,根据实际情况修改 +# Configure the APN information according to your actual needs usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} usrCfg2 = {'profileID': 2, 'apn': '3gwap', 'username': '', 'password': ''} @@ -29,15 +29,15 @@ def checkAPN(usrCfg, reboot=False): return False print('Check the APN configuration of the {} network card.'.format(usrCfg['profileID'])) - # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + # Get the APN information of the cellular NICs and check if the current one is the one you specified pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) if pdpCtx != -1: if pdpCtx[1] != usrCfg['apn']: - # 如果不是用户需要的APN,使用如下方式配置 + # If it is not the APN you need, configure it as follows ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) if ret == 0: print('APN configuration successful.') - # 重启后按照配置的信息进行拨号 + # Make a data call according to the configured information after the module reboots if reboot: print('Ready to restart to make APN take effect.') print('Please re-execute this program after restarting.') @@ -56,20 +56,20 @@ def checkAPN(usrCfg, reboot=False): def main(): - # 使能第一路网卡开机自动激活功能 + # Enable automatic activation for NIC1 dataCall.setAutoActivate(1, 1) - # 使能第一路网卡自动重连功能 + # Enable automatic reconnection for NIC1 dataCall.setAutoConnect(1, 1) - # 使能第二路网卡开机自动激活功能 + # Enable automatic activation for NIC2 dataCall.setAutoActivate(2, 1) - # 使能第二路网卡自动重连功能 + # Enable automatic reconnection for NIC2 dataCall.setAutoConnect(2, 1) - # 检查第一路网卡的APN配置,暂时不重启 + # Check the APN configuration of NIC1. Do not reboot now. checkpass = checkAPN(usrCfg1, reboot=False) if not checkpass: return - # 检查第二路网卡的APN配置,配置后重启 + # Check the APN configuration of NIC2. Reboot the module after configuration. checkpass = checkAPN(usrCfg2, reboot=True) if not checkpass: return @@ -77,7 +77,7 @@ def main(): stage, state = checkNet.waitNetworkReady(20) if stage == 3 and state == 1: print('Network connected successfully.') - # 分别获取第一路和第二路网卡的IP地址信息 + # Get the IP addresses of NIC1 and NIC2 ret1 = dataCall.getInfo(usrCfg1['profileID'], 0) ret2 = dataCall.getInfo(usrCfg2['profileID'], 0) print('NIC{}:{}'.format(usrCfg1['profileID'], ret1)) @@ -98,45 +98,45 @@ def main(): print('NIC{} ip:{}'.format(usrCfg2['profileID'], ip_nic2)) print('---------------sock1 test-----------------') - # 创建socket对象 + # Create a socket object sock1 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) - # 解析域名 + # Resolve the domain name try: sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] except Exception: print('Domain name resolution failed.') sock1.close() return - # 建立连接 + # Connect to the server sock1.connect(sockaddr) - # 向服务端发送消息 + # Send data to the server 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') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server data = sock1.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) - # 关闭连接 + # Close the connection sock1.close() print('---------------sock2 test-----------------') sock2 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.TCP_CUSTOMIZE_PORT) sock2.bind((ip_nic2, 0)) sock2.settimeout(10) - # 服务器IP和端口,下面的IP和端口仅作示例参考 + # Configure server IP address and port number. The IP address and port number below are for example only server_addr = ('220.180.239.212', 8305) - # 建立连接 + # Connect to the server sock2.connect(server_addr) - # 向服务器发送消息 + # Send data to the server ret = sock2.send('test data.') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server try: data = sock2.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) except Exception: print('No reply from server.') - # 关闭连接 + # Close the connection sock2.close() else: print('Network connected failed, stage={}, state={}'.format(stage, state)) diff --git a/network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py b/network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py index 8a541d4..cdcda28 100644 --- a/network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py +++ b/network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py @@ -18,40 +18,40 @@ import utime from misc import Power -# 用户需要配置的APN信息,根据实际情况修改 +# Configure the APN information according to your actual needs usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} usrCfg2 = {'profileID': 2, 'apn': '3gwap', 'username': '', 'password': ''} ''' -关闭开机自动拨号功能 -返回值表示是否需要重启设备 -True - 需要重启 -False - 不需要重启 +Disable the feature of automatic data call at startup +The return value indicates whether the module needs rebooting +True - Need +False - Not need ''' def disableAutoActivate(): need_reboot = False if "datacall_config.json" not in uos.listdir('/usr'): - # 关闭第一路网卡开机自动激活功能 + # Disable automatic activation at startup for NIC1 dataCall.setAutoActivate(1, 0) - # 建议用户使能网卡自动重连功能 + # It is recommended to enable the automatic reconnection for the cellular NICs dataCall.setAutoConnect(1, 1) need_reboot = True return need_reboot ''' -为蜂窝无线网卡配置APN参数 -返回值表示是否需要重启设备 -True - 需要重启 -False - 不需要重启 +Configure APN parameters for the cellular NIC +The return value indicates whether the module needs rebooting +True - Need +False - Not need ''' def cfgAPN(usrCfg): need_reboot = False print('Check the APN configuration of the NIC{}.'.format(usrCfg['profileID'])) - # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + # Get the APN information of the cellular NICs and check if the current one is the one you specified pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) if pdpCtx != -1: if pdpCtx[1] != usrCfg['apn']: - # 如果不是用户需要的APN,使用如下方式配置 + # If it is not the APN you need, configure it as follows ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) if ret == 0: print('APN configuration successful.') @@ -76,7 +76,7 @@ def main(): Power.powerRestart() utime.sleep(2) - # 手动激活蜂窝无线网卡 + # Manually activate the cellular NICs print('Prepare to activate the NIC{}.'.format(usrCfg1['profileID'])) ret = dataCall.activate(usrCfg1['profileID']) if ret == -1: @@ -91,7 +91,7 @@ def main(): stage, state = checkNet.waitNetworkReady(10) if stage == 3 and state == 1: print('Network connected successfully.') - # 分别获取第一路和第二路网卡的IP地址信息 + # Get the IP address of NIC2 and NIC2 ret1 = dataCall.getInfo(usrCfg1['profileID'], 0) ret2 = dataCall.getInfo(usrCfg2['profileID'], 0) print('NIC{}:{}'.format(usrCfg1['profileID'], ret1)) @@ -112,48 +112,48 @@ def main(): print('NIC{} ip:{}'.format(usrCfg2['profileID'], ip_nic2)) print('---------------sock1 test-----------------') - # 创建socket对象 + # Create a socket object sock1 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) - # 解析域名 + # Resolve the domain name try: sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] except Exception: print('Domain name resolution failed.') sock1.close() return - # 建立连接 + # Connect to the server sock1.connect(sockaddr) - # 向服务端发送消息 + # Send data to the server 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') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server data = sock1.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) - # 关闭连接 + # Close the connection sock1.close() print('---------------sock2 test-----------------') sock2 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.TCP_CUSTOMIZE_PORT) sock2.bind((ip_nic2, 0)) sock2.settimeout(10) - # 服务器IP和端口,下面的IP和端口仅作示例参考 + # Configure server IP address and port number. The IP address and port number below are for example only server_addr = ('220.180.239.212', 8305) - # 建立连接 + # Connect to the server sock2.connect(server_addr) - # 向服务器发送消息 + # Send data to the server ret = sock2.send('test data.') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server try: data = sock2.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) except Exception: print('No reply from server.') - # 关闭连接 + # Close the connection sock2.close() - # 用户根据需要决定是否需要在通信结束对网卡进行去激活 + # ecide whether to deactivate the NIC after communication ends as needed # dataCall.deactivate(usrCfg1['profileID']) # dataCall.deactivate(usrCfg2['profileID']) else: diff --git a/network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py b/network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py index 9de21e5..7302654 100644 --- a/network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py +++ b/network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py @@ -18,31 +18,31 @@ import utime from misc import Power -# 用户需要配置的APN信息,根据实际情况修改 +# Configure the APN information according to your actual needs usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} ''' -环境配置: -1.关闭开机自动拨号功能 -2.APN配置 +Environment configuration: +1. Disable the feature of automatic data call at startup +2. Configure APN ''' def cfgEnv(usrCfg): errcode = 0 need_reboot1 = False need_reboot2 = False if "datacall_config.json" not in uos.listdir('/usr'): - # 关闭第一路网卡开机自动激活功能 + # Disable automatic activation at startup for the first cellular NIC dataCall.setAutoActivate(usrCfg['profileID'], 0) - # 建议用户使能网卡自动重连功能 + # It is recommended to enable the automatic reconnection for the cellular NIC dataCall.setAutoConnect(usrCfg['profileID'], 1) need_reboot1 = True print('Check the APN configuration.') - # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + # Get the APN information of the cellular NICs and check if the current one is the one you specified pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) if pdpCtx != -1: if pdpCtx[1] != usrCfg['apn']: - # 如果不是用户需要的APN,使用如下方式配置 + # If it is not the APN you need, configure it as follows ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) if ret == 0: print('APN configuration successful.') @@ -56,7 +56,7 @@ def cfgEnv(usrCfg): else: print('Failed to get PDP Context.') errcode = -1 - # 重启使配置生效 + # Reboot the module to make the configuration take effect if need_reboot1 or need_reboot2: print('Ready to restart.') print('Please re-execute this program after restarting.') @@ -69,7 +69,7 @@ def main(): if cfgEnv(usrCfg1) == -1: return - # 手动激活蜂窝无线网卡 + # Manually activate the cellular NIC print('Prepare to activate the NIC{}.'.format(usrCfg1['profileID'])) ret = dataCall.activate(usrCfg1['profileID']) if ret == -1: @@ -79,33 +79,33 @@ def main(): stage, state = checkNet.waitNetworkReady(10) if stage == 3 and state == 1: print('Network connected successfully.') - # 获取第一路网卡的IP地址等信息 + # Get the IP address and other information of the first NIC ret = dataCall.getInfo(usrCfg1['profileID'], 0) print('NIC{}:{}'.format(usrCfg1['profileID'], ret)) print('---------------sock test-----------------') - # 创建socket对象 + # Create a socket object sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) - # 解析域名 + # Resolve the domain name try: sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] except Exception: print('Domain name resolution failed.') sock.close() return - # 建立连接 + # Connect to the server sock.connect(sockaddr) - # 向服务端发送消息 + # Send data to the server 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') print('send {} bytes'.format(ret)) - # 接收服务端消息 + # Receive data from the server data = sock.recv(256) print('recv {} bytes:'.format(len(data))) print(data.decode()) - # 关闭连接 + # Close the connection sock.close() - # 用户根据需要决定是否需要在通信结束对网卡进行去激活 + # Decide whether to deactivate the NIC after communication ends as needed # dataCall.deactivate(usrCfg1['profileID']) else: print('Network connected failed, stage={}, state={}'.format(stage, state))