Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open the Arduino serial-port without it restarts

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()
like image 306
carlos urrego Avatar asked Sep 11 '25 23:09

carlos urrego


1 Answers

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!

like image 91
Jeff Avatar answered Sep 13 '25 13:09

Jeff