I need to communicate with an Arduino. When I use serial.readline()
to read what Arduino has to say it works fine. But when I useserial.write('something')
doesn't seem to do anything.
Interesting thing is that if I write the same code in the console or command-line, it works as expected...
Using Python 2.7.
Python code:
import serial
import time
arduinoSerialData = serial.Serial('com6', 9600)
time.sleep(1)
while True:
arduinoSerialData.write('AAA')
arduinoSerialData.flush()
sleep(0.5)
Arduino code:
void loop() {
if (Serial.readString().startsWith("AAA")) {
digitalWrite(7, LOW);
} else {
digitalWrite(7, HIGH);
}
delay(500);
Again, Python code runs fine from the console, so no idea why this happens.
1 Answer 1
Closely related to To know the state of USB (Serial) connection (connected or not connected)
I would not be using Serial.readString()
personally. How do you know where the string starts and ends? Just check for three "A" in a row.
Like this:
const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds
const byte LED = 7;
void setup ()
{
Serial.begin (115200);
while (!Serial) ; // wait for Serial to become active
Serial.println ("Starting");
pinMode (LED, OUTPUT);
} // end of setup
unsigned long lastMessage;
unsigned int countOfA;
void processInput ()
{
while (Serial.available ())
{
char c = Serial.read ();
if (c == 'A')
countOfA++;
else
countOfA = 0;
// do something with the data
} // end of while loop
} // end of processInput
void loop ()
{
if (Serial.available ())
{
lastMessage = millis (); // remember when we last got input
processInput (); // now handle the input
}
if (millis () - lastMessage >= MESSAGE_TIMEOUT)
{
countOfA = 0;
}
// warn if no keepalive received
if (countOfA >= 3)
digitalWrite(LED, LOW);
else
digitalWrite(LED, HIGH);
} // end of loop
Note that this will fail (ie. it will light the LED) if it gets something other than "A" which would include newlines, etc.
What it does it count the number of times it gets the letter "A". If it gets something else it resets the count. If 10 seconds elapse without receiving something, the count resets.
-
It doesn't work though.Leo Ervin– Leo Ervin2015年09月20日 22:06:40 +00:00Commented Sep 20, 2015 at 22:06
-
Well, it does, because I tested it. :P The words "doesn't work" do have a rather enraging quality (we hear them so often). Does the LED stay on? Stay off? Not go off after you type "AAA"? Not go on after a time-out? What Arduino are you using? Try to give details here.2015年09月20日 22:14:31 +00:00Commented Sep 20, 2015 at 22:14
-
Did you try from the console, or executed a py file? It works only from the console for some reason, like few others have experienced as well. I'm using a Nano.Leo Ervin– Leo Ervin2015年09月20日 22:15:38 +00:00Commented Sep 20, 2015 at 22:15
-
What console? I am using the Arduino serial monitor. Make sure you get the baud rate right, I always use 115200 which is what I had in the code in my answer. I don't know why people limit themselves to 9600 baud.2015年09月20日 22:17:54 +00:00Commented Sep 20, 2015 at 22:17
-
The Python console. Issue is with Python, not ArduinoLeo Ervin– Leo Ervin2015年09月20日 22:19:06 +00:00Commented Sep 20, 2015 at 22:19
Pyserial serial.write() doesn't work
but you follow up by saying itruns fine from the console
. So, the serial.write does in fact work. Try to stop using the words "doesn't work" and describe in more detail what is happening.readString()
can't reliably get all three AAAs in one catch - you have to send them at the right moment - otherwise it will read only first or last part or something else...