编写了一个基于python的串口通信,在ubuntu系统上通过串口来控制单片机,现在遇到的问题是运行python代码后,串口可以正常连接,但是指令没办法从PC端发到单片机端口,给单片机手动复位下后,指令就可以发送到单片机了,然后单片机正常控制电机。端口为ttyUSB0,端口权限也给了,实在想不明白什么问题,求大佬们解决一下。代码如下所示。 import serial import threading # 导入线程包 import time import sys #串口初始化 data_ser = serial.Serial() data_ser.port='ttyUSB0' data_ser.baudrate=115200 data_ser.bytesize=8 data_ser.stopbits=1 data_ser.parity="N"#奇偶校验位 data_ser.setDTR(False) data_ser.setRTS(False) try: data_ser.open() print(f"成功打开串口") except serial.SerialException as e: print(f"无法打开串口: {e}") sys.exit(1) stop_event = threading.Event() def get_data(): while not stop_event.is_set(): # 使用 Event 判断线程是否需要停止 try: data_count = data_ser.inWaiting() if data_count != 0: recv = data_ser.read(data_ser.in_waiting).decode("gbk", errors="ignore") time.sleep(1) print(recv) process_received_data(recv) # 在这里调用处理函数 except Exception as e: print(f"读取串口数据时出错: {e}") time.sleep(0.1) data_thread = threading.Thread(target=get_data) def process_received_data(recv): # 假设 recv 是类似 "start:middle:end" 的形式 start_marker = 'MaiChong2的值为:' end_marker = 'MaiChong3' # 找到起始和结束标记的位置 start_index = recv.find(start_marker) end_index = recv.find(end_marker) if start_index != -1 and end_index != -1: # 从起始标记后到结束标记前的位置截取字符串 middle_part = recv[start_index + len(start_marker):end_index].strip() print(middle_part) else: print(f"Start or End marker not found") data_thread.start() angle1 = 'abcd700e1500f1000g0h250i0' + 'j0k0l0\r\n' data_ser.write(angle1.encode('utf-8')) # 等待一段时间,让线程运行 time.sleep(5) 相同的程序将端口ttyUSB0换为COM11后,在windows系统上可以正常运行,不用手动复位。
Keil uVision5软件在使用时出现上栏许多重要功能都用不了的情况怎么办?"具体情况" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241211/055c9693014963b96d56cce327dff11d.png)