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(). http://stackoverflow.com/questions/4235512/pyserial-works-fine-in-python-interpreter-but-not-standalone http://stackoverflow.com/questions/1618141/pyserial-problem-with-arduino-works-with-the-python-shell-but-not-in-a-program/4941880#4941880

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 http://arduino.stackexchange.com/questions/16220/pyserial-serial-write-doesnt-work.

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

And: http://arduino.stackexchange.com/questions/16215/make-arduino-tell-when-the-python-program-stops-reading-from-the-serial-crash

Also on this forum: http://forum.arduino.cc/index.php?topic=348533.0

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