Skip to main content

I need to communicate with an Arduino. DoingWhen I use serial.readline() to read what Arduino has to say it works fine. Doing But when I useserial.write('something') doesn't seem to do anything.

What is interestingInteresting 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.

I need to communicate with an Arduino. Doing serial.readline() to read what Arduino has to say works fine. Doing serial.write('something') doesn't seem to do anything.

What is interesting 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.

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.

Source Link
Leo Ervin
  • 127
  • 2
  • 3
  • 9

Pyserial serial.write() doesn't work

I need to communicate with an Arduino. Doing serial.readline() to read what Arduino has to say works fine. Doing serial.write('something') doesn't seem to do anything.

What is interesting 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.