从代码中禁用 pyserial 中的 DTR [英] Disable DTR in pyserial from code

查看:32
本文介绍了从代码中禁用 pyserial 中的 DTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pyserial 将数据发送到 arduino.但是当我打开 COM 端口时,它会将 DTR 设置为低电平并重置电路板.但是,我有我的 arduino 代码设置,因此我必须通过按住两个按钮 1 秒钟将其置于串行接收模式.如果可能,我宁愿不必在 arduino 启动时进行串行输入.

I'm trying to use pyserial to send data to an arduino. But when I open the COM port it sets DTR low and resets the board. However, I have my arduino code setup such that I have to put it into serial receive mode by holding two buttons for 1 second. I would rather not have to do the serial input on boot of the arduino, if possible.

显然您可以修改serialWin32.py文件,更改读取的行:

Apparently you can modify serialWin32.py file, changing the line that reads:

self._dtrState = win32.DTR_CONTROL_ENABLE

到:

self._dtrState = win32.DTR_CONTROL_DISABLE

但是,有没有办法直接在我的 python 脚本中禁用它?我还需要为所有系统执行此操作.我宁愿不强迫人们仅仅为了使用这个脚本而改变他们的基本串行配置.

But, is there a way to just disable this directly in my python script? I also need to do this for all systems. I would rather not force people to change their base serial config just to use this script.

串口打开如下:

 com = serial.Serial(port, baud, timeout=1);

更新: 最后,我找到了一个适合我的设置的解决方案.由于我不需要一直处理串行数据,只有当我将设备置于串行接收模式时,我才找到了一种方法来禁用 arduino 本身的串行连接重置.

Update: In the end I found a solution that works fine for my setup. Since I didn't need to do Serial data all the time, only when I put the device into serial receive mode I found a way to disable the reset on serial connect from the arduino itself.

许多帖子都说您可以通过在 5V 和重置之间放置一个 ~100 欧姆的电阻来禁用 DTR 重置.但我不希望这成为永久性的事情.因此,我在 PD5 和复位之间放置了一个电阻.然后,在软件中:

Many posts have said you can disable DTR reset by placing a ~100 Ohm resistor between 5V and reset. But I didn't want this to be a permanent thing. So, instead I placed a resistor between PD5 and reset. Then, in software:

void setup() {
    //.......
    DDRD &= ~(_BV(PD5)); //Set PD5 as input initially
    PORTD |= (_BV(PD5)); //Set high
    //.......
}

inline void setResetDisable(bool state)
{
  if(state)
    DDRD |= (_BV(PD5)); //Set PD5 as output to put 5V on reset line
  else
    DDRD &= ~(_BV(PD5)); //set back to input mode
}

所以现在,当我想进入串行模式时,我调用 setResetDisable(true),它会在 100 欧姆电阻和复位引脚上产生 5V 电压,防止 DTR 将其拉低并复位芯片:)然后,当我离开串行模式时,我只调用 setResetDisable(false),这样芯片就可以正常编程了.

So now, when I want to be in serial mode, I call setResetDisable(true) which throws 5V on that 100 ohm resistor and the reset pin, preventing DTR from pulling it low and resetting the chip :) I then just call setResetDisable(false) when I leave serial mode so the chip can be programmed as normal.

推荐答案

应该能够在打开端口之前禁用 DTR,像这样:

You ought to be able to disable DTR before opening the port, like this:

com = serial.Serial()
com.port = port
com.baudrate = baud
com.timeout = 1
com.setDTR(False)
com.open()

但是,在 Windows 上当前版本的 pyserial (2.6) 上执行此操作会引发以下异常:

However, doing so on the current release of pyserial (2.6) on Windows throws the following exception:

..., line 315, in setDTR
ValueError: Attempting to use a port that is already open

这似乎是一个错误,已在 2011 年 12 月 29 日的最新版本的源代码 SVN 修订版 445 中修复(参见 http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialwin32.py?view=log) 带注释:

This seems to be a bug which is fixed in the latest version of the source, SVN revision 445 on 29th December 2011 (see http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialwin32.py?view=log) with comment:

在 Win32 上打开之前允许 setRTS、setDTR(设置初始状态),文档更新

allow setRTS, setDTR before opening on Win32 (to set initial state), doc update

看起来它可能刚刚错过了 2.6 版本(于 2011 年 11 月 2 日上传,请参阅:https:///pypi.python.org/pypi/pyserial).

Which looks like it may have just missed the 2.6 release (uploaded on 2nd November 2011 see: https://pypi.python.org/pypi/pyserial).

此外,查看用于 POSIX 的 setDTR() 的当前实现(在 serialposix.py) 看起来此错误未修复,如果端口未开放,因此跨平台解决方案看起来不太可能.

Furthermore, looking at the current implementation of setDTR() for POSIX (in serialposix.py) it looks like this bug is not fixed and an exception is thrown if the port is not open, so a cross-platform solution looks unlikely.

这篇关于从代码中禁用 pyserial 中的 DTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆