串口双向透传python小程序
import serial
import threadingdef serial_forward(source, dest):while True:data = source.read()dest.write(data)def main():# 串口1设置serial_port1 = serial.Serial('COM9', 9600, timeout=1)# 串口2设置serial_port2 = serial.Serial('COM30', 9600, timeout=1)# 创建线程实现双向透传t1 = threading.Thread(target=serial_forward, args=(serial_port1, serial_port2))t2 = threading.Thread(target=serial_forward, args=(serial_port2, serial_port1))t1.start()t2.start()t1.join()t2.join()if __name__ == "__main__":main()