0

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
    Your subject is Pyserial serial.write() doesn't work but you follow up by saying it runs 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. – Nick Gammon Sep 20 '15 at 22:16
  • 1
    Cross posted on Arduino Forum and a second time. – Nick Gammon Sep 20 '15 at 23:03
  • please append what you did that worked - so the steps you did in the 'console' - – Stefan Krüger Sep 21 '15 at 7:21
  • i think that your 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... – Stefan Krüger Sep 21 '15 at 19:49
  • I'm having a similar problem with my raspberry pi, where sometimes it will work, sometimes it won't. I'm trying to send the sys.argv array, minus the first element, and display it on a screen, which I thought would be simple, but sometimes it will work and sometimes not. Code on the pi: import serial import sys ser = serial.Serial('/dev/ttyACM0',9600) args = str(' '.join(sys.argv[1:])) args = args.replace("'","") ser.write("test".encode()) print(args) Code on the arduino: String incomingByte; #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); //l – voidUpdate Dec 12 '17 at 17:03
2

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 Sep 20 '15 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. – Nick Gammon Sep 20 '15 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 Sep 20 '15 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. – Nick Gammon Sep 20 '15 at 22:17
  • The Python console. Issue is with Python, not Arduino – Leo Ervin Sep 20 '15 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.