14

I'm connecting from Pi to Arduino-clone board. The problem is, that I need to disable DTR line, in order to prevent Arduino from resetting on connect.

I have read that on RPi is not possible to control DTR, DCD and other lines. Is it this true for RPi globally or only for GPIO pins used for serial communication?

If it's possible to disable DTR on USB port, how do you accomplish that?

Chetan Bhargava
  • 1,252
  • 3
  • 15
  • 29
jnovacho
  • 324
  • 2
  • 3
  • 9

5 Answers5

9

The properties of a USB serial converter have nothing to do with the hardware of the hosting system, but only with the USB serial chip itself, and the software stack of the hosting system.

The pi should be using stock Linux USB serial drivers.

Therefore, you can enable/disable the linkage of DTR to port open/close via the usual Linux method of clearing the hupcl setting as documented on the Arduino site and elsewhere:

stty -F /dev/ttyUSB0 -hupcl

Or replacing /dev/ttyUSB0 with whatever device file actually corresponds to your USB serial port (for example, the first Uno connected would likely be /dev/ttyACM0)

Even with regard to the PI's own native serial port, this behavior of DTR is ultimately under software control - anyone who argues otherwise is ignoring the fact that it is only the Linux driver, and not the hardware, which has any knowledge of the port being opened or closed. The actual port hardware can only tell that it is being read from or written to or reconfigured, none of which are actually synonymous with opening the serial device.

Chris Stratton
  • 993
  • 5
  • 11
6

@ChrisStrattons post describes how to use stty -F /dev/ttyUSB0 to avoid the hangup which results in a reset. Here is a snippet to do it in Python:

import termios

path = '/dev/ttyACM0'

# Disable reset after hangup
with open(path) as f:
    attrs = termios.tcgetattr(f)
    attrs[2] = attrs[2] & ~termios.HUPCL
    termios.tcsetattr(f, termios.TCSAFLUSH, attrs)

ser = serial.Serial(path, 9600)
# etc.

Note that the exact number may when replugging the USB cable, so I detect the path by globbing:

try:
    path = glob.glob('/dev/ttyACM*')[0]
except IndexError:
    # retry, error out, etc.
    pass
Lekensteyn
  • 1,521
  • 1
  • 15
  • 24
4

You can add a 120Ohm (or combination to make 120Ohm) resistor between RESET and 5V This will prevent reset completely. This is the least invasive as other solutions require either remove a resistor or capacitor from the board, it complicates uploads. Do not keep the resistor if you are programming. Remove it.

enter image description here

Leonardo boards do not reset even if DTR is triggered but the problem begins when you need to reset it remotely as sometimes it looses connection to Raspberry and you have to physically reset it.

Piotr Kula
  • 17,336
  • 6
  • 66
  • 105
0

If you use Seria and pySerial library, You can use :

ser = serial.Serial('/dev/ttyACM0', 9600, dsrdtr = True)

polo04
  • 1
-1

You can use PySerial. Here is an example on Python code:

port =serial.Serial(
    "/dev/ttyUSB0",
    baudrate=57600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    dsrdtr = False
    )

for more options check Pyserial.

hsantana8
  • 97
  • 1
  • 1
  • 6