I have a Python script that writes a string test to the Arduino serial port. If the arduino receives the test string, it should reply with a string ok and LED 13 should like up..
Problem: When the Arduino Serial Monitor is used to write test to serial port, Arduino replies with ok as expected and the LED #13 lights up.
However when the Python script writes test to the same serial port, nothing happens. Arduino does not reply to serial port and the LED #13 does not light up.
Any ideas how Python script can be fixed to get the ok response from Arduino and LED 13 to light up?
Arduino Sketch
int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
while(Serial.available() == 0) { }
if(Serial.readString() == "test\r\n") {
Serial.print("ok\r\n");
digitalWrite(ledPin, HIGH);
}
readString = ""; // Clear recieved buffer
delay(100);
}
Python Script
port = 'COM5'
ser = serial.Serial(
port=port,
baudrate=9600,
timeout=5
)
serial.write("test\r\n")
response = serial.readline()
print response