serial_port = serial.Serial( port="/dev/ttyTHS1", #串口设备地址 baudrate=115200, #信号波特率 bytesize=serial.EIGHTBITS, #数据位 parity=serial.PARITY_NONE, #是否启用奇偶校验 stopbits=serial.STOPBITS_ONE, #停止位 ) # Wait a second to let the port initialize time.sleep(1)
try: # Send a simple header serial_port.write("UART Demonstration Program\r\n".encode()) whileTrue: if serial_port.inWaiting() > 0: data = serial_port.read() print(data) serial_port.write(data) # if we get a carriage return, add a line feed too # \r is a carriage return; \n is a line feed # This is to help the tty program on the other end # Windows is \r\n for carriage return, line feed # Macintosh and Linux use \n if data == "\r".encode(): # For Windows boxen on the other end serial_port.write("\n".encode())