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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-04-2021, 03:38 PM
|
#1
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
|
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: - If I understand man pages correctly, the same problem will occur in C/C++ programs.
- I'm absolutely green when it comes to Python
- I've written similar code in C# (Windows) which successfully suppresses DTR when the serial port is opened
|
|
|
05-04-2021, 05:15 PM
|
#2
|
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,332
|
|
|
|
05-05-2021, 12:59 AM
|
#3
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,955
|
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.
|
|
|
05-05-2021, 02:21 AM
|
#4
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
Original Poster
|
Quote:
Originally Posted by teckk
|
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.
|
|
|
05-05-2021, 02:42 AM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
Original Poster
|
Quote:
Originally Posted by NevemTeve
|
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.
|
|
|
05-05-2021, 09:06 AM
|
#6
|
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,332
|
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.
|
|
|
05-05-2021, 09:29 AM
|
#7
|
Senior Member
Registered: Jan 2012
Distribution: Slackware
Posts: 3,348
Rep:
|
Quote:
Originally Posted by Wim Sturkenboom
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?
|
|
|
05-05-2021, 02:23 PM
|
#8
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
Original Poster
|
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
|
|
|
05-06-2021, 06:53 AM
|
#9
|
Member
Registered: Feb 2013
Distribution: Debian (jessie)
Posts: 42
Rep:
|
I don't understand your problem, but
this:
Quote:
Originally Posted by Wim Sturkenboom
|
needs to be this:
|
|
|
05-06-2021, 01:14 PM
|
#10
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
Original Poster
|
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
|
|
|
All times are GMT -5. The time now is 05:55 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|