从code禁用DTR在pyserial [英] Disable DTR in pyserial from code

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

问题描述

我试图用pyserial将数据发送到一个Arduino。但是,当我打开COM端口,它集DTR低,复位电路板。但是,我有我的Arduino code设置这样,我必须把它转换成串行按住两个按钮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(真),它抛出5V上100欧姆的电阻和复位引脚,preventing DTR从拉低,复位芯片:)
然后,我只是叫setResetDisable(假),当我离开串行模式使芯片可以编程为正常。

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

这似乎是固定源的最新版本中的错误,SVN版本445于2011年12月29日(见<一href=\"http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialwin32.py?view=log\">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:

允许setRTS,setDTR在Win32开幕之前(以设置初始状态),DOC更新

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

它看起来像它可能只是错过了2.6版本(载2日2011年11月参考: HTTPS://pypi.python。组织/ 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).

此外,看目前执行 setDTR()对POSIX(可在<一href=\"http://pyserial.svn.sourceforge.net/viewvc/pyserial/trunk/pyserial/serial/serialposix.py?revision=447&view=markup\">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.

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

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