From 3eea74f6ceb585f420ff3e1a075aaf114ebbb50e Mon Sep 17 00:00:00 2001 From: "jayceon.fu" Date: Sat, 22 Jul 2023 14:26:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?code=20(cellular):=20=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=9C=82=E7=AA=9D=E6=97=A0=E7=BA=BF=E7=BD=91=E5=8D=A1=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E6=96=87=E6=A1=A3=E4=B8=AD=E7=9A=84=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 固件版本: N/A 是否需要文案翻译: 是 --- .../example_network_exception_handle.py | 66 ++++++++ ...mple_socket_activate_default_nic_no_apn.py | 36 +++++ ...ple_socket_activate_default_nic_set_apn.py | 68 ++++++++ ...le_socket_activate_multiple_nic_set_apn.py | 132 +++++++++++++++ ..._manually_activate_multiple_nic_set_apn.py | 150 ++++++++++++++++++ ...ocket_manually_activate_one_nic_set_apn.py | 101 ++++++++++++ 6 files changed, 553 insertions(+) create mode 100644 network-comm/nic/cellular/example_network_exception_handle.py create mode 100644 network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py create mode 100644 network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py create mode 100644 network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py create mode 100644 network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py create mode 100644 network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py diff --git a/network-comm/nic/cellular/example_network_exception_handle.py b/network-comm/nic/cellular/example_network_exception_handle.py new file mode 100644 index 0000000..7154670 --- /dev/null +++ b/network-comm/nic/cellular/example_network_exception_handle.py @@ -0,0 +1,66 @@ +import net +import dataCall +import _thread +import osTimer +import utime +import sys_bus + + +CFUN_SWITCH_TOPIC = 'cfun_switch_timer' +NETWORK_EVENT_TOPIC = 'network_event' + +network_monitor = None + +class NetworkConnectState: + DISCONNECT = 0 + CONNECT = 1 + + +class NetworkExceptionService: + def __init__(self): + self.timer_period = 1000 * 60 * 2 # 2 * 60s + self.__cfun_timer = osTimer() + + sys_bus.subscribe(CFUN_SWITCH_TOPIC, self.__cfun_switch_timer_handle) + sys_bus.subscribe(NETWORK_EVENT_TOPIC, self.__network_event_handle) + + def enable(self): + dataCall.setCallback(self.__network_event_callback) + + def __cfun_switch_timer_callback(self, args): + print('cfun switch timer.') + sys_bus.publish(CFUN_SWITCH_TOPIC, 0) + + def __cfun_switch_timer_handle(self, topic, msg): + print('recv event form cfun_switch_timer.') + net.setModemFun(0) + utime.sleep(5) + net.setModemFun(1) + + def __network_event_callback(self, args): + print('The state of the network connection has changed.') + sys_bus.publish(NETWORK_EVENT_TOPIC, args) + + def __network_event_handle(self, topic, msg): + print('recv event form network_event_callback.') + profile_id = msg[0] + conn_state = msg[1] + if conn_state == NetworkConnectState.DISCONNECT: + print('The network connection has been disconnected.') + print('start cfun_switch_timer.') + self.__cfun_timer.start(self.timer_period, 1, self.__cfun_switch_timer_callback) + elif conn_state == NetworkConnectState.CONNECT: + print('The network connection has been connected.') + self.__cfun_timer.stop() + else: + print('unknown state value:{}.'.format(conn_state)) + + +def main(): + global network_monitor + network_monitor = NetworkExceptionService() + network_monitor.enable() + + +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 new file mode 100644 index 0000000..41d1dee --- /dev/null +++ b/network-comm/nic/cellular/example_socket_activate_default_nic_no_apn.py @@ -0,0 +1,36 @@ +import checkNet +import usocket + + +def main(): + stage, state = checkNet.waitNetworkReady(20) + if stage == 3 and state == 1: + print('Network connected successfully.') + # 创建一个socket对象 + sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) + # 解析域名 + try: + sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] + except Exception: + print('Domain name resolution failed.') + sock.close() + return + # 建立连接 + sock.connect(sockaddr) + # 向服务端发送消息 + 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)) + + # 接收服务端消息 + data = sock.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + + # 关闭连接 + sock.close() + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() 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 new file mode 100644 index 0000000..54a1829 --- /dev/null +++ b/network-comm/nic/cellular/example_socket_activate_default_nic_set_apn.py @@ -0,0 +1,68 @@ +import checkNet +import usocket +import dataCall +from misc import Power + +# 用户需要配置的APN信息,根据实际情况修改 +usrCfg = {'apn': '3gnet', 'username': '', 'password': ''} + + +def checkAPN(): + # 获取第一路网卡的APN信息,确认当前使用的是否是用户指定的APN + pdpCtx = dataCall.getPDPContext(1) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # 如果不是用户需要的APN,使用如下方式配置 + 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.') + # 重启后按照配置的信息进行拨号 + 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.') + # 创建一个socket对象 + sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) + # 解析域名 + try: + sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] + except Exception: + print('Domain name resolution failed.') + sock.close() + return + # 建立连接 + sock.connect(sockaddr) + # 向服务端发送消息 + 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)) + + # 接收服务端消息 + data = sock.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + + # 关闭连接 + sock.close() + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() 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 new file mode 100644 index 0000000..be6ded4 --- /dev/null +++ b/network-comm/nic/cellular/example_socket_activate_multiple_nic_set_apn.py @@ -0,0 +1,132 @@ +import checkNet +import usocket +import dataCall +from misc import Power + + +# 用户需要配置的APN信息,根据实际情况修改 +usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} +usrCfg2 = {'profileID': 2, 'apn': '3gwap', 'username': '', 'password': ''} + + +def checkAPN(usrCfg, reboot=False): + if type(usrCfg) != dict: + print("Error: Input is not a dictionary.") + return False + + print('Check the APN configuration of the {} network card.'.format(usrCfg['profileID'])) + # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # 如果不是用户需要的APN,使用如下方式配置 + ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) + if ret == 0: + print('APN configuration successful.') + # 重启后按照配置的信息进行拨号 + if reboot: + print('Ready to restart to make APN take effect.') + print('Please re-execute this program after restarting.') + Power.powerRestart() + else: + return True + 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(): + # 使能第一路网卡开机自动激活功能 + dataCall.setAutoActivate(1, 1) + # 使能第一路网卡自动重连功能 + dataCall.setAutoConnect(1, 1) + # 使能第二路网卡开机自动激活功能 + dataCall.setAutoActivate(2, 1) + # 使能第二路网卡自动重连功能 + dataCall.setAutoConnect(2, 1) + + # 检查第一路网卡的APN配置,暂时不重启 + checkpass = checkAPN(usrCfg1, reboot=False) + if not checkpass: + return + # 检查第二路网卡的APN配置,配置后重启 + checkpass = checkAPN(usrCfg2, reboot=True) + if not checkpass: + return + + stage, state = checkNet.waitNetworkReady(20) + if stage == 3 and state == 1: + print('Network connected successfully.') + # 分别获取第一路和第二路网卡的IP地址信息 + ret1 = dataCall.getInfo(usrCfg1['profileID'], 0) + ret2 = dataCall.getInfo(usrCfg2['profileID'], 0) + print('NIC{}:{}'.format(usrCfg1['profileID'], ret1)) + print('NIC{}:{}'.format(usrCfg2['profileID'], ret2)) + ip_nic1 = None + ip_nic2 = None + if ret1 == -1 or ret1[2][2] == '0.0.0.0': + print("Error: Failed to get the IP of the NIC{}.".format(usrCfg1['profileID'])) + return + else: + ip_nic1 = ret1[2][2] + if ret2 == -1 or ret2[2][2] == '0.0.0.0': + print("Error: Failed to get the IP of the NIC{}.".format(usrCfg2['profileID'])) + return + else: + ip_nic2 = ret2[2][2] + print('NIC{} IP:{}'.format(usrCfg1['profileID'], ip_nic1)) + print('NIC{} ip:{}'.format(usrCfg2['profileID'], ip_nic2)) + + print('---------------sock1 test-----------------') + # 创建socket对象 + sock1 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) + # 解析域名 + try: + sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] + except Exception: + print('Domain name resolution failed.') + sock1.close() + return + # 建立连接 + sock1.connect(sockaddr) + # 向服务端发送消息 + 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)) + # 接收服务端消息 + data = sock1.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + # 关闭连接 + 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和端口仅作示例参考 + server_addr = ('220.180.239.212', 8305) + # 建立连接 + sock2.connect(server_addr) + # 向服务器发送消息 + ret = sock2.send('test data.') + print('send {} bytes'.format(ret)) + # 接收服务端消息 + try: + data = sock2.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + except Exception: + print('No reply from server.') + # 关闭连接 + sock2.close() + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() 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 new file mode 100644 index 0000000..e3b9f8b --- /dev/null +++ b/network-comm/nic/cellular/example_socket_manually_activate_multiple_nic_set_apn.py @@ -0,0 +1,150 @@ +import checkNet +import usocket +import dataCall +import utime +from misc import Power + +# 用户需要配置的APN信息,根据实际情况修改 +usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} +usrCfg2 = {'profileID': 2, 'apn': '3gwap', 'username': '', 'password': ''} + +''' +关闭开机自动拨号功能 +返回值表示是否需要重启设备 +True - 需要重启 +False - 不需要重启 +''' +def disableAutoActivate(): + need_reboot = False + if "datacall_config.json" not in uos.listdir('/usr'): + # 关闭第一路网卡开机自动激活功能 + dataCall.setAutoActivate(1, 0) + # 建议用户使能网卡自动重连功能 + dataCall.setAutoConnect(1, 1) + need_reboot = True + return need_reboot + +''' +为蜂窝无线网卡配置APN参数 +返回值表示是否需要重启设备 +True - 需要重启 +False - 不需要重启 +''' +def cfgAPN(usrCfg): + need_reboot = False + print('Check the APN configuration of the NIC{}.'.format(usrCfg['profileID'])) + # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # 如果不是用户需要的APN,使用如下方式配置 + ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) + if ret == 0: + print('APN configuration successful.') + need_reboot = True + else: + raise ValueError("APN configuration failed.") + else: + need_reboot = False + print('The APN is correct and no configuration is required') + else: + raise ValueError("Failed to get PDP Context.") + return need_reboot + + +def main(): + need_reboot1 = disableAutoActivate() + need_reboot2 = cfgAPN(usrCfg1) + need_reboot3 = cfgAPN(usrCfg2) + if need_reboot1 or need_reboot2 or need_reboot3: + print('Ready to restart.') + print('Please re-execute this program after restarting.') + Power.powerRestart() + utime.sleep(2) + + # 手动激活蜂窝无线网卡 + print('Prepare to activate the NIC{}.'.format(usrCfg1['profileID'])) + ret = dataCall.activate(usrCfg1['profileID']) + if ret == -1: + print('NIC activation failed.') + return + print('Prepare to activate the NIC{}.'.format(usrCfg2['profileID'])) + ret = dataCall.activate(usrCfg2['profileID']) + if ret == -1: + print('NIC activation failed.') + return + + stage, state = checkNet.waitNetworkReady(10) + if stage == 3 and state == 1: + print('Network connected successfully.') + # 分别获取第一路和第二路网卡的IP地址信息 + ret1 = dataCall.getInfo(usrCfg1['profileID'], 0) + ret2 = dataCall.getInfo(usrCfg2['profileID'], 0) + print('NIC{}:{}'.format(usrCfg1['profileID'], ret1)) + print('NIC{}:{}'.format(usrCfg2['profileID'], ret2)) + ip_nic1 = None + ip_nic2 = None + if ret1 == -1 or ret1[2][2] == '0.0.0.0': + print("Error: Failed to get the IP of the NIC{}.".format(usrCfg1['profileID'])) + return + else: + ip_nic1 = ret1[2][2] + if ret2 == -1 or ret2[2][2] == '0.0.0.0': + print("Error: Failed to get the IP of the NIC{}.".format(usrCfg2['profileID'])) + return + else: + ip_nic2 = ret2[2][2] + print('NIC{} IP:{}'.format(usrCfg1['profileID'], ip_nic1)) + print('NIC{} ip:{}'.format(usrCfg2['profileID'], ip_nic2)) + + print('---------------sock1 test-----------------') + # 创建socket对象 + sock1 = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) + # 解析域名 + try: + sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] + except Exception: + print('Domain name resolution failed.') + sock1.close() + return + # 建立连接 + sock1.connect(sockaddr) + # 向服务端发送消息 + 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)) + # 接收服务端消息 + data = sock1.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + # 关闭连接 + 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和端口仅作示例参考 + server_addr = ('220.180.239.212', 8305) + # 建立连接 + sock2.connect(server_addr) + # 向服务器发送消息 + ret = sock2.send('test data.') + print('send {} bytes'.format(ret)) + # 接收服务端消息 + try: + data = sock2.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + except Exception: + print('No reply from server.') + # 关闭连接 + sock2.close() + + # 用户根据需要决定是否需要在通信结束对网卡进行去激活 + # dataCall.deactivate(usrCfg1['profileID']) + # dataCall.deactivate(usrCfg2['profileID']) + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() 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 new file mode 100644 index 0000000..26f836a --- /dev/null +++ b/network-comm/nic/cellular/example_socket_manually_activate_one_nic_set_apn.py @@ -0,0 +1,101 @@ +import checkNet +import usocket +import dataCall +import utime +from misc import Power + +# 用户需要配置的APN信息,根据实际情况修改 +usrCfg1 = {'profileID': 1, 'apn': '3gnet', 'username': '', 'password': ''} + +''' +环境配置: +1.关闭开机自动拨号功能 +2.APN配置 +''' +def cfgEnv(usrCfg): + errcode = 0 + need_reboot1 = False + need_reboot2 = False + if "datacall_config.json" not in uos.listdir('/usr'): + # 关闭第一路网卡开机自动激活功能 + dataCall.setAutoActivate(usrCfg['profileID'], 0) + # 建议用户使能网卡自动重连功能 + dataCall.setAutoConnect(usrCfg['profileID'], 1) + need_reboot1 = True + + print('Check the APN configuration.') + # 获取网卡的APN信息,确认当前使用的是否是用户指定的APN + pdpCtx = dataCall.getPDPContext(usrCfg['profileID']) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # 如果不是用户需要的APN,使用如下方式配置 + ret = dataCall.setPDPContext(usrCfg['profileID'], 0, usrCfg['apn'], usrCfg['username'], usrCfg['password'], 0) + if ret == 0: + print('APN configuration successful.') + need_reboot2 = True + else: + print('APN configuration failed.') + errcode = -1 + else: + print('The APN is correct and no configuration is required') + errcode = 0 + else: + print('Failed to get PDP Context.') + errcode = -1 + # 重启使配置生效 + if need_reboot1 or need_reboot2: + print('Ready to restart.') + print('Please re-execute this program after restarting.') + Power.powerRestart() + utime.sleep(2) + return errcode + + +def main(): + if cfgEnv(usrCfg1) == -1: + return + + # 手动激活蜂窝无线网卡 + print('Prepare to activate the NIC{}.'.format(usrCfg1['profileID'])) + ret = dataCall.activate(usrCfg1['profileID']) + if ret == -1: + print('NIC activation failed.') + return + + stage, state = checkNet.waitNetworkReady(10) + if stage == 3 and state == 1: + print('Network connected successfully.') + # 获取第一路网卡的IP地址等信息 + ret = dataCall.getInfo(usrCfg1['profileID'], 0) + print('NIC{}:{}'.format(usrCfg1['profileID'], ret)) + + print('---------------sock test-----------------') + # 创建socket对象 + sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) + # 解析域名 + try: + sockaddr = usocket.getaddrinfo('python.quectel.com', 80)[0][-1] + except Exception: + print('Domain name resolution failed.') + sock.close() + return + # 建立连接 + sock.connect(sockaddr) + # 向服务端发送消息 + 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)) + # 接收服务端消息 + data = sock.recv(256) + print('recv {} bytes:'.format(len(data))) + print(data.decode()) + # 关闭连接 + sock.close() + + # 用户根据需要决定是否需要在通信结束对网卡进行去激活 + # dataCall.deactivate(usrCfg1['profileID']) + else: + print('Network connected failed, stage={}, state={}'.format(stage, state)) + + +if __name__ == '__main__': + main() From b1969ba73df0c7a193fbfb23706812303cbe9186 Mon Sep 17 00:00:00 2001 From: "jayceon.fu" Date: Fri, 20 Oct 2023 16:43:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8B=A8=E5=8F=B7?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E6=96=87=E6=A1=A3=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=AB=A0=E8=8A=82=E4=B8=ADapn=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=BE=8B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...xample_network_exception_handle_set_apn.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 network-comm/nic/cellular/example_network_exception_handle_set_apn.py 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..c89a85b --- /dev/null +++ b/network-comm/nic/cellular/example_network_exception_handle_set_apn.py @@ -0,0 +1,46 @@ +import checkNet +import usocket +import dataCall +from misc import Power + +# 用户需要配置的APN信息,根据实际情况修改 +usrCfg = {'apn': '3gnet', 'username': '', 'password': ''} + +def checkAPN(): + # 获取第一路网卡的APN信息,确认当前使用的是否是用户指定的APN + pdpCtx = dataCall.getPDPContext(1) + if pdpCtx != -1: + if pdpCtx[1] != usrCfg['apn']: + # 如果不是用户需要的APN,使用如下方式配置 + 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.') + # 重启后按照配置的信息进行拨号 + 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 From a5d082026ccfabe6012ecdb8c6f00f09efb77b2c Mon Sep 17 00:00:00 2001 From: "jayceon.fu" Date: Thu, 22 Feb 2024 14:27:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E8=A6=81=E6=B1=82=E5=B0=86=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=B3=A8=E9=87=8A=E9=83=BD=E6=8D=A2=E6=88=90?= =?UTF-8?q?=E8=8B=B1=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...xample_network_exception_handle_set_apn.py | 22 ++++++-- ...mple_socket_activate_default_nic_no_apn.py | 12 ++--- ...ple_socket_activate_default_nic_set_apn.py | 20 +++---- ...le_socket_activate_multiple_nic_set_apn.py | 44 +++++++-------- ..._manually_activate_multiple_nic_set_apn.py | 54 +++++++++---------- ...ocket_manually_activate_one_nic_set_apn.py | 36 ++++++------- 6 files changed, 101 insertions(+), 87 deletions(-) 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 index c89a85b..4bd7c2d 100644 --- a/network-comm/nic/cellular/example_network_exception_handle_set_apn.py +++ b/network-comm/nic/cellular/example_network_exception_handle_set_apn.py @@ -1,22 +1,36 @@ +# 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 -# 用户需要配置的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.') 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))