1、操作串口需要调用pyserial库
import serial
串口设置:
ser=serial.Serial(COM4) ser.baudrate=115200 ser.timeout=0.5 ser.open()
串口读取
msg=ser.readline() #表示读取一行,并赋值给msg变量
串口写入
ser.write(('AT'+' ').encode()) #可以像串口发送AT和换行
串口通信,向串口发送组合键ctrl+z,先 找到组合键的ASCII码值,然后转换成十六进制,直接写入,例ctrl+z对应 acii码的十进制1A,
ser.write(char(0x1A)).encode())
实际应用到我这边项目的实现如下:
from Args import Args cmds =[ # 单链路TCP,发送数据 #1)发送数据,以ctrl+z发送 Args(request='AT+QIOPEN="TCP","110.80.1.51",12001',regex=r'^AT+QIOPEN="TCP","110.80.1.51",12001 OK CONNECT OK $',stop_character="CONNECT OK",timeout=60,can_skip=True,delay=1), Args(request='AT+QISEND',regex=r' >',can_skip=True,delay=1), #向串口发送组合键ctrl+z的思路就是找组合键的ASCII码值,然后转换成十六进制,直接写入 Args(request="01234567895452asdfghjklzxcvbnmqwertyuiop{}".format(chr(0x1A),regex=r'^ SEND OK 01234567895452asdfghjklzxcvbnmqwertyuiop $',without_enter=True,delay=1)), ] if __name__ == '__main__'or True: from task import Task Task(cmds).execute()