Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No possible way to set dtr and rts state during method open. #729

Open
gitcnd opened this issue Jan 16, 2024 · 4 comments
Open

No possible way to set dtr and rts state during method open. #729

gitcnd opened this issue Jan 16, 2024 · 4 comments

Comments

@gitcnd
Copy link

gitcnd commented Jan 16, 2024

This works, and properly sets the pin state:-

import serial
ser = serial.Serial()
ser.dtr=0
ser.rts=0
ser.port='/dev/ttyS9'
ser.baudrate=115200
ser.rtscts=0
ser.dsrdtr=0
ser.open() # Works

However, the below does not work, and is the closest possible method doing the same as the above, except, it does not allow any users to set dtr/rts state (both default to 1 on windows, which generates unwanted MCU resets when trying to connect to arduino/esp32/etc devices)

ser = serial.Serial('/dev/ttyS9', baudrate=115200, interCharTimeout=1, rtscts=0, dsrdtr=0) # fails (sets pins unwantedly)

The above method needs to add "dtr" and "rts" kwargs to allow users to specify the initial state for those pins.

@fluffy
Copy link

fluffy commented Jan 21, 2024

In the above code, where you set the ser.dtr etc, when I look at the output of the DTR and RTS pins on a max connected to a CH340 USB to serial device, I still see the DTR and RTS go high for 20 ms when the port opens before they get forced low. So the problem might be bigger than just missing in the open function call.

@gitcnd
Copy link
Author

gitcnd commented Jan 22, 2024

Try a second time - I notice the first call when you reboot your PC (or connect a new device) does that. From then on, it behaves.
I'm using WSL1 on Windows 10N on a RoG laptop, and CH340USB ESP32CAM - the above fix solved all my rebooting and lockup problems.

I notice the code for mpremote and pyboard on micropython already implements the same workaround when they detect windows (with a comment about ES32 and ESP8266 RTS issues), so it appears to also have solved the problem for other people on other devices as well.

On a philosophical topic instead now though: since there does exist dtr and rts state-setting "methods", it does seem to be an obvious oversight that someone forgot to allow those to be set inside the constructor. The code which opens the port has to make a decision about whether "high" or "low" should be the initial state for those pins - but without any way for us to tell it.

@fluffy
Copy link

fluffy commented Jan 23, 2024

I get what you are saying about constructor has to choose something. I'm looking at the posix code that is used on mac, linux, unix. I'm not how different the windows is as I don't have an easy way to test that. I could be wrong but it looks like the code has to open a socket, then it can call an ioctl on that socket to set the values of RTS / DTR. As soon as the sockets opens, the lines seem to go high. I can't see any way of setting them before the open call. The ioctl can then set them low or high. When the socket is closed, it looks like the default behavior is to set them low but it might be the case that if the "HUPCL" ( no hangup on close ) options is set correctly that they can stay high after the socket closes. This would keep them from going low again when the device gets opened again.

I know on the ESP32, most people use a two transistor circuit for this. You can see it in the thread on https://forum.micropython.org/viewtopic.php?t=4607

Anyways, just poking around on this and trying to sort it out and I hope there is some way to control the RTS and DTR when the open happens but no idea what it is.

@gitcnd
Copy link
Author

gitcnd commented Jan 23, 2024

I'm in Ubuntu under WSL1 on Windows, so I wonder if my experience is different to plain linux ?

Can you explain what you mean by "socket" ? (Did you mean "Device" instead?). Here's what perplexity thinks:

https://www.perplexity.ai/search/C-code-in-4ELrwOKlQ1yLK42leua5Dw?s=c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants