1

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 time
import serial

# setup Arduino USB communication
try:
    arduinoSerialData = serial.Serial('com3', 9600)

except: # not connected/damaged
    pass

while True:
    if arduinoSerialData.inWaiting() > 0:
        arduinoSerialData.write('A')
        arduinoSerialData.flush()
        datastr = arduinoSerialData.readline()
        print datastr
        time.sleep(1)
19
  • this does not include any information that we would need to help you. Be more specific on what is not working and post any code that could help us. Commented Sep 20, 2015 at 19:21
  • did you try .flush() Commented Sep 20, 2015 at 19:23
  • Doesn't flush() freeze the program? My program has a main loop. I'll update the post with a code. Commented Sep 20, 2015 at 19:25
  • flush will clear the buffer Commented Sep 20, 2015 at 19:25
  • If you don't have flow-control disabled, then pyserial could be vainly waiting for CTS to be active. Commented Sep 20, 2015 at 19:28

3 Answers 3

5

Put a time.sleep(2) line after you open the port to give the Arduino time to reboot.

Sign up to request clarification or add additional context in comments.

Comments

1

I know I´m a little bit too late but today I had the same problem with Pyserial and an ESP32, a device sort of like an arduino. The solution was to give pyserial the same configuration as the serial interface in the ESP32:

esp = serial.Serial("COM10", 115200)
esp.parity=serial.PARITY_EVEN
esp.stopbits=serial.STOPBITS_ONE
esp.bytesize=serial.EIGHTBITS

I send the commands like this:

arr = [32,2,0,4] #Decimal
esp.write(arr)

This is the ESP32 configuration, in case you need to know:

uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_EVEN,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
    
uart_param_config(UART_PORT_NUM, &uart_config);

The ESP now echoes the command correctly I hope this can help.

Comments

0

try to add the timeout parameter in the Python script, then try to set your main arduino code in a while loop step1: python

 arduinoSerialData = serial.Serial('com3', 9600, 1)

replace the arduinoSerialData.flush() by:

 arduinoSerialData.flushInput()

step2: Arduino:

void loop(){
    while (Serial.available > 0){
            // your main code
    }
}

3 Comments

I also use the arduino to tell the state of 6 buttons, which I'd like to check every 0.5 seconds. WIll that be an issue with this solution?
Yeah, arduinoSerialData.readline() after write() kills the performance of the program...
try to clear the serial buffer after reading from the arduino, this will not kill the performance of the program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.