2

I have the following code:

import sys,serial
ser = serial.Serial()
ser.baudrate=57600
ser.port = sys.argv[1]
ser.dsrdtr = True
ser.open();
ser.setDTR(level=False)               

print ser.readline()

The thing is that my Arduino UNO receives a DTR and restarts, how can I disable this (in software)? My python code is running from a Mac mini with a usb connection to my UNO.

(I'm fully aware of this but hardware is not an option for me)

sraok
  • 595
  • 6
  • 23
Fredrik Erlandsson
  • 1,279
  • 1
  • 13
  • 22

1 Answers1

0

I'm having similar issues, but haven't been able to find a solution for a while. Looks like this is possible on Windows with some hackery, but the issue lies deeper on posix.

Ideally you should be able to setDTR before you open the connection. Like this:

import sys,serial
ser = serial.Serial()
ser.baudrate=57600
ser.port = sys.argv[1]
ser.dsrdtr = True
ser.setDTR(level=False)
ser.open();

print ser.readline()

But that throws a portNotOpenError in serialposix.py:

def setDTR(self,on=1):
    """set terminal status line"""
    if not self.fd: raise portNotOpenError
    if on:
        fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
    else:
        fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)

I took a dive into serialposix.py, and you'll see where the root issue lies. That self.fd defined above is actually:

self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK)

If you write a little script that opens your device using os.open(device, flags), you'll see it resets, even if you open it as read only with the flag os.O_RDONLY.

Digging deeper into the meaning of the os.open flags- we find that the open command actually wraps the unix command open(2). The man pages are here.

Let me know if you ever find a more satisfactory solution.

Community
  • 1
  • 1
Jono
  • 707
  • 2
  • 8
  • 17