Reputation: 133
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)
Upvotes: 1
Views: 5359
Reputation: 11
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.
Upvotes: 1
Reputation: 51
Put a time.sleep(2)
line after you open the port to give the Arduino time to reboot.
Upvotes: 5
Reputation: 75
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
}
}
Upvotes: 0