一 前记 使用cat1模块做产品的过程中,遇到了不少问题。其中很重要的一个就是怎么测试单个模块的好坏。这里笔者专门写了一个工具,来测试cat1模块的是否好用,这里做一个分享吧。 二 源码解析 这个是一个完全可以工作的程序,只需要改成你的模块的即可;
# -*- coding: utf-8 -*- import time import hashlib import threading from serial import Serial import serial.tools.list_ports import time #查找端口函数 def findPort(): ports = serial.tools.list_ports.comports() for each in ports: if 'Android' in each: com_android = each.split(' - ')[0] return com_android #发送AT指令函数 def at_send_cimi(serial): try: at_bsn = 'AT+CIMI' # 具体命令就不给出了。 serial.write(at_bsn.encode('utf-8') + b'\r') # 向端口发送指令必须二进制格式;末尾添加\r\n表示发送。 print('AT+CIMI SEND SUCCESS!') except Exception as e: print(e) def at_send_cmiot(serial): try: #at_bsn = 'AT+QICSGP=1,1,"CTNET","","",1' # 具体命令就不给出了。 #中国移动 at_bsn = 'AT+QICSGP=1,1,"CMIOT","","",1' # 具体命令就不给出了。 serial.write(at_bsn.encode('utf-8') + b'\r') # 向端口发送指令必须二进制格式;末尾添加\r\n表示发送。 print('AT+QICSGP SEND SUCCESS!') except Exception as e: print(e) def at_send_qiact(serial): try: at_bsn = 'AT+QIACT=1' # 具体命令就不给出了。 serial.write(at_bsn.encode('utf-8') + b'\r') # 向端口发送指令必须二进制格式;末尾添加\r\n表示发送。 print('AT+QIACT SEND SUCCESS!') except Exception as e: print(e) def at_send_tcprequest(serial): try: at_bsn = 'AT+QIOPEN=1,0,"TCP","47.103.67.239",8080,0,2' # 具体命令就不给出了。 serial.write(at_bsn.encode('utf-8') + b'\r') # 向端口发送指令必须二进制格式;末尾添加\r\n表示发送。 print('AT+tcp request SEND SUCCESS!') except Exception as e: print(e) def socket_send_data(serial): try: at_bsn = 'hello world test success' # 具体命令就不给出了。 serial.write(at_bsn.encode('utf-8') + b'\r') # 向端口发送指令必须二进制格式;末尾添加\r\n表示发送。 print('socket SEND data SUCCESS!') except Exception as e: print(e) def recv_msg(serial): # 接收消息(任务线程) global g_tcp_state while True: data = serial.read(128) if len(data) > 6: print("data2:",data[2]) if data[2] == 0x52 and data[3] == 0x44: print("ready cmd in") at_send_cimi(serial) time.sleep(0.1) #at_send_cmiot(serial) #osdelay(1000) #time.sleep(0.1) #at_send_qiact(serial) if len(data) > 30: if data[29] == 0x4f and data[30] == 0x4b: print("recv sim card success and send cmiot register") at_send_cmiot(serial) #at_send_qiact(serial) #time.sleep(0.1) #at_send_tcprequest(serial) if len(data) > 35: if data[32] == 0x4f and data[33] == 0x4b: print("recv cmiot register success and send qiat cmd") at_send_qiact(serial) if len(data) == 17: print("data13:",data[13]) if data[13] == 0x4f and data[14] == 0x4b: print("recv qiact ack success and send tcp connect") at_send_tcprequest(serial) if len(data) == 66: print("data61:",data[61]) print("data62:",data[62]) if data[61] == 0x73 and data[62] == 0x73: g_tcp_state = True print("tcp socket connected success") if g_tcp_state == True: socket_send_data(serial) time.sleep(0.1) #主程序 if __name__ == '__main__': g_tcp_state = False serial = serial.Serial('/dev/ttyUSB0', 115200, timeout=0.5) #/dev/ttyUSB0 if serial.isOpen() : print("open success") else : print("open failed") threading.Thread(target=recv_msg, args=(serial,)).start()三 总结 at指令在c代码中和python中是不同的,c代码中要针对逗号要添加隔离符,python中是不需要的。 还要就是回撤和换行,每个产品都不同,这个添加的时候要具体情况具体分析了。 标签:python,send,发送,cat1,bsn,串口,print,serial,data From: https://www.cnblogs.com/dylancao/p/17927833.html