I am writing a python script that communicates with Arduino. Every time I close the python script and I open it again the Arduino is restarted. That is because the serial port is reopened by the script on start, hence my Arduino restarts. Is there a way to open the serial port in the script without restarting the Arduino?
This is my Python code:
import time
import serial
from sys import argv
script,elemento,control =argv
arduino= serial.Serial('COM7',9600)
#while True:
time.sleep(1)
elemento=int(elemento)
control=int(control)
if (elemento>0) & (elemento<10):
print(elemento)
print(control)
if control == 1:
arduino.write(str(elemento))
time.sleep(0.5)
arduino.write(str(control))
elif control == 0:
arduino.write(str(elemento))
arduino.write(str(control))
arduino.close()
The Arduino is reset because the serial port open command is pulsing the DTR line. I have very little python experience, but this link shows dsrdtr as the ninth parameter. By putting a bool there you should be able to make it stop resetting. I'm not sure if you want 0 or 1 so you will have to experiment.
Also, depending on the specific board and your soldering abilities, there is normally a component you can remove from the board to stop the serial port from resetting the board. Be advise that this option makes it harder to upload new firmware since the bootloader uses the reset to start.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With