ProgrammingThis 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.
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.
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
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, 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.