PySerial issues (Or just Arduino Serial issue)

I'm trying to write something to Serial with Python and read it in Arduino so Arduino will know my Python program is still alive. But the Pyserial serial.write() function doesn't seem to work properly?
I saw some discussions about this, and possible solution being letting some time pass after starting the serial connection and doing the write().

I tried it.. but doesnt work very well still.

import serial
import time

arduinoSerialData = serial.Serial('com6', 9600)
time.sleep(1)
	
while True:
    arduinoSerialData.write('AAA')
    arduinoSerialData.flush()
    sleep(0.5)

And in Arduino I have this code:

void loop() {

    if (Serial.readString().startsWith("AAA")) {
        digitalWrite(7, LOW);
    } else {
        digitalWrite(7, HIGH);
    }
    
    delay(500);

I'm hoping I'm doing something wrong in the Arduino code and its not an PySerial oddity again.

But seeing how it works fine in the console and not when executing a file I dont know....

Also sorry about the previosu topic, I have a terrible fever and my brain was malfunctioning.

Cross-posted to python - Pyserial serial.write() doesn't work - Arduino Stack Exchange.

And: http://arduino.stackexchange.com/questions/16207/know-the-state-of-usb-serial-connection-coonected-or-not-connected

And: make Arduino tell when the Python program stops reading from the Serial (crash, etc) - Arduino Stack Exchange

Also on this forum: checking if Arduino is connected (serial USB) to a PC? - Programming Questions - Arduino Forum

Do the words "cross-posting" mean anything to you?

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Thread locked.

  • Moderator