python serial

Hello,

I use a minimlist code to communicate between arduino and python. The principle is to trigger specific responses depending of stimuli present in other program using Python.
I use serial port but program cratch after the third stimuli.

My python code is
import serial
ser = serial.Serial ("COM5", 9600)
ser.write ('1')
ser.close

A part of my arduino is :
if (sensordroit>=5){
if (Serial.available()){
Keyboard.press ('b');
Keyboard.release('b');
delay (250);
analogWrite (motorPindroit, 250);
delay (250);
analogWrite (motorPindroit, OUTPUT);
Serial.flush();
}
else{
Keyboard.press ('b');
Keyboard.release('b');
I think the problem comes from on the serial port, but....?
Do you have ideas?
Thank you
GT

I specify: crach arrive after 3 presentations...

This

ser = serial.Serial ("COM5", 9600)
ser.write ('1')
ser.close

is likely to cause a problem because the Arduino will reset every time the serial port is opened. The reset process takes a few seconds so it probably won't be listening for the data. And, of course, you may not want it to reset.

Your PC program should open the serial port and keep it open until it is finished with the Arduino.

Have a look at this Python - Arduino demo

...R

Thank you :smiley: