Python向Arduino 16x2 LCD发送字符串遇串口通信问题求助
Fixing Serial Communication for Arduino 16x2 LCD with Python
Let's tackle this serial communication issue step by step. First, your provided Arduino code cuts off mid-while loop, which is a big part of the problem. Let's fix that and build a robust setup to send strings from Python to your LCD.
Updated Arduino Code
This version handles serial data reception properly, includes timeout logic, and uses a delimiter to split text across the two LCD lines:
#include <LiquidCrystal.h> LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Use a pipe | as a delimiter to separate LCD line 1 and line 2 const char delimiter = '|'; String incomingString = ""; const unsigned long timeout = 10; // 10ms timeout for serial reception void setup() { lcd.begin(16, 2); Serial.begin(9600); lcd.print("Waiting for data"); } void loop() { incomingString = ""; unsigned long startTime = millis(); // Read serial data until timeout or newline (message end) while (millis() - startTime < timeout || Serial.available()) { if (Serial.available() > 0) { char c = Serial.read(); // Stop reading if we hit a newline (our message terminator) if (c == '\n') { break; } incomingString += c; startTime = millis(); // Reset timeout when new data arrives } } // If we received a valid string, process it for the LCD if (incomingString.length() > 0) { int delimiterIndex = incomingString.indexOf(delimiter); if (delimiterIndex != -1) { // Split the string into two lines String line1 = incomingString.substring(0, delimiterIndex); String line2 = incomingString.substring(delimiterIndex + 1); // Clear LCD and print the lines lcd.clear(); lcd.setCursor(0, 0); lcd.print(line1.substring(0,16)); // Limit to 16 chars for line 1 lcd.setCursor(0, 1); lcd.print(line2.substring(0,16)); // Limit to 16 chars for line 2 } else { // If no delimiter, print everything on line 1 lcd.clear(); lcd.setCursor(0, 0); lcd.print(incomingString.substring(0,16)); } } }
Python Serial Sender Code
First, install the pyserial library if you haven't already:
pip install pyserial
Then use this script to send text to the Arduino. Replace 'COM3' with your Arduino's serial port (on Linux/macOS this will be /dev/ttyACM0 or similar):
import serial import time # Configure serial port ser = serial.Serial( port='COM3', # Replace with your port baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1 ) time.sleep(2) # Wait for Arduino to initialize # Send a message: format is "Line1|Line2\n" message = "Hello from Python|LCD Test!\n" ser.write(message.encode()) print("Message sent!") ser.close()
Debugging Tips
- Check Serial Port: Verify the port in your Python script matches the one in Arduino IDE (Tools > Port).
- Avoid Port Conflicts: Close the Arduino IDE's serial monitor before running the Python script—only one program can access the serial port at a time.
- Test with Serial Monitor: First, send a string like
First Line|Second Line\nvia the Arduino serial monitor to confirm the LCD works correctly. - Limit String Length: The 16x2 LCD can only show 16 characters per line, so the code truncates longer strings to prevent overflow.
内容的提问来源于stack exchange,提问作者Enrico Martini
相关产品推荐
相关产品推荐

