I'm using Python to talk to my Arduino using the pyserial module. For some reason, the Arduino isn't resetting when I make a new connection. However, it still resets like it's supposed to when I connect to it with minicom, the problem is only with Python. (I don't have a working Arduino IDE so I don't know what happens with its serial monitor.) The test program on my Arduino just continually increments an integer and sends it to the serial connection. My Python script is equally simple:
s = serial.Serial('/dev/ttyUSB0', 115200)
for x in range(0, 10):
Whenever I run minicom, it resets the program and I see 1, 2, 3, 4, 5, etc. but that doesn't happen with my script. Has anyone ever seen this before?
This isn't terribly surprising. Resetting happens when the DTR line is pulsed high. There is no guarantee that this happens with pyserial (and clearly from your observations, it doesn't happen). Nor does it have to. The only "standard" use of a serial port involves the Tx (transmit) and Rx (receive) lines. The DTR, DSR, DCD, etc. lines have been used for various purposes and without any consistency.
My guess: minicom thinks its a good idea to set DTR high when opening the serial port, pyserial doesn't bother.
Ahh, okay. I wonder if there's any way to convince pyserial that it should do that. Probably not, huh? Oh well... I guess I just need to make my real program work with it even though I was previously assuming it would reset every time. Shouldn't be a big deal. Thanks for the info!
Well... Resetting the Arduino upon connection is something not every one would enjoy. Hence doing it automatically is not something I would preach.
If you want to do that:
from serial import Serial
from time import sleep
s = Serial('/dev/ttyUSB0', 115200)
s.setDTR(False) # Drop DTR
sleep(0.022) # Read somewhere that 22ms is what the UI does.
s.setDTR(True) # UP the DTR back
If it does not work switch the setDTR lines (make it go True than False).
Also the 22ms may be increased.
Sweet, that worked. Thanks! I agree that having it automatically reset isn't necessarily a good thing but after I found that out, I started working under the assumption that it would always happen. I'm actually very glad to see that you can do whichever one you want with pyserial.