LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2021, 02:38 PM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Python: suppressing DTR when opening serial port


Good day

I'm using Arduino for embedded applications and communicate over serial ports (e.g. ttyUSB0). With a number of Arduinos, the problem is that they reset when the serial port is opened because DTR is asserted; this is by design.

First attempts to suppress DTR were not quite successful but with some googling I got to below code; the only problem is that the very first time that the serial port is opened, DTR is still asserted.

How do I modify the below code that this does not happen? A link to some web page will be fine, modified code as well

Thanks in advance

Code:
import sys
import serial
import termios

# print some python info for fun
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)

# define the port
port = '/dev/ttyUSB0'
resetPort = False

if resetPort == False:
# to be able to suppress DTR, we need this
  f = open(port)
  attrs = termios.tcgetattr(f)
  attrs[2] = attrs[2] & ~termios.HUPCL
  termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
  f.close()
else:
  f = open(port)
  attrs = termios.tcgetattr(f)
  attrs[2] = attrs[2] | termios.HUPCL
  termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
  f.close()


with serial.Serial() as ser:
# setup serial port
  ser.baudrate = 57600
  ser.port = port
  ser.rtscts = False            # not setting to false prevents communication
  ser.dsrdtr = resetPort        # determines if Arduino resets or not
  ser.timeout = 2

# print settings
  print (ser.name)
  print (ser)

# open serial port
  ser.open()

# read initial Arduino message
  s = ser.read(20)
  print(s)

# send some data to be echoed
# b is needed for python3
  ser.write(b'hello world')
  s = ser.read(20)
  print(s)

ser.close
print('done')
Notes:
  1. If I understand man pages correctly, the same problem will occur in C/C++ programs.
  2. I'm absolutely green when it comes to Python
  3. I've written similar code in C# (Windows) which successfully suppresses DTR when the serial port is opened
 
Old 05-04-2021, 04:15 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,519

Rep: Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095
https://raspberrypi.stackexchange.co...b0/31298#31298
 
Old 05-04-2021, 11:59 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,170
Blog Entries: 1

Rep: Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536Reputation: 1536
You mean you want a computer-computer connection, using serial line? Check this link: http://www.cadxservices.com/guides/rs232.htm
The images at the bottom shows how to wire to cable.
 
Old 05-05-2021, 01:21 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by teckk View Post
Thanks, but that is basically what the code that I provided is And the first time that you open the connection, it will do assertion of DTR; after that, it works as expected.
 
Old 05-05-2021, 01:42 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by NevemTeve View Post
You mean you want a computer-computer connection, using serial line? Check this link: http://www.cadxservices.com/guides/rs232.htm
The images at the bottom shows how to wire to cable.
Thanks, the cable is a USB cable Maybe ttyUSB0 should have given it away

The PC (USB host) is connected to an embedded 'computer' (USB slave) via a standard USB cable; the embedded 'computer' has a USB-to-TTL chip which converts the USB to TTL level TX, RX and DTR for communication; the latter forces a reset of the microcontroller on the embedded 'computer' when asserted.
 
Old 05-05-2021, 08:06 AM   #6
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,519

Rep: Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095Reputation: 1095
I don't have termios installed. I don't have the hardware that you have. That is going to be really hard/impossible for someone else to test. There are threads on stackexchange from people that say they solved it. That is probably the best anyone can point you to.

If you solve this let the forum know how.
 
Old 05-05-2021, 08:29 AM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,063

Rep: Reputation: Disabled
Quote:
Originally Posted by Wim Sturkenboom View Post
The PC (USB host) is connected to an embedded 'computer' (USB slave) via a standard USB cable; the embedded 'computer' has a USB-to-TTL chip
Which chip? What does lsusb report?
 
Old 05-05-2021, 01:23 PM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks for the follow ups / additional replies

The chip will probably not matter; testing when I started the thread was with
Code:
Bus 002 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC
Not tested yet:
Code:
Bus 004 Device 002: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 004 Device 003: ID 2a03:0043 dog hunter AG Arduino Uno Rev3
The solution should work with all of those and any other USB-to-TTL converter (e.g. Prolific CP2102 is another one but I don't have it at hand).

I'm currently distro-shopping and testing different distros so yesterday's setup is gone; need to go through the pip installs again
 
Old 05-06-2021, 05:53 AM   #9
mimorek
Member
 
Registered: Feb 2013
Distribution: Debian (jessie)
Posts: 42

Rep: Reputation: Disabled
I don't understand your problem, but
this:
Quote:
Originally Posted by Wim Sturkenboom View Post
Code:
ser.close
needs to be this:
Code:
ser.close()
 
Old 05-06-2021, 12:14 PM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks minorek, I've fixed it.


Some further observations/research:

With the given code, the CH340 will always assert DTR, the others in reply #8 only once.

For others that encounter this, a possible 'fix' is described at https://unix.stackexchange.com/quest...en-for-cdc-acm but it requires a compilation of the module(s). Maybe I will have the guts one day
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening a serial port without asserting DTR hraftery Programming 2 04-30-2013 09:11 PM
serial port R/W with DTR and RTS andreski Programming 1 12-06-2007 10:57 AM
Serial line RTS and DTR setting zhanghao Linux - Hardware 1 02-15-2006 08:43 AM
Serial line RTS and DTR setting zhanghao Linux - General 5 02-14-2006 04:34 PM
Serial Port : How do I raise DTR ttumelty Programming 3 08-26-2005 04:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration