在Python中创建串行对象时出现问题

2024-09-29 01:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个程序,用串行对象和一个Arduino装置通信。在类的init-方法中可以找到这段代码:

    try:
        self.rotor  = serial.Serial(port = "COM22", baudrate=115200, timeout = 0.1, writeTimeout = 1)
    except serial.SerialException, e:
        print "Error when connecting to collimator: ", e

当我运行它时,我得到以下错误消息:

^{pr2}$

我要求计算机打开COM22,它回答说不能打开COM1。什么 关于什么?Arduino装置插入COM22。在

我有另一个程序,我还没有自己编写,但它使用同一个类库。这个程序能用,但我不明白怎么用。是否有一些串行对象的初始化我没有做?在


Tags: 对象方法代码self程序initportserial
2条回答

从PySerial SVN trunk(http://svn.code.sf.net/p/pyserial/code/trunk/pyserial/serial/serialwin32.py)中Win32Serial对象的源代码:

def open(self):
    """\
    Open port with current settings. This may throw a SerialException
    if the port cannot be opened.
    """
    if self._port is None:
        raise SerialException("Port must be configured before it can be used.")
    if self._isOpen:
        raise SerialException("Port is already open.")
    # the "\\.\COMx" format is required for devices other than COM1-COM8
    # not all versions of windows seem to support this properly
    # so that the first few ports are used with the DOS device name
    port = self.portstr

所以把你的代码改成:

^{pr2}$

应该能正常工作。在

我后来发现错误源于错误定义的模块路径 用类定义。路径被定向到旧版本的 相同的文件。在

相关问题 更多 >