简体   繁体   English

Python串行导入时出错

[英]Error on Python serial import

When I try to import the serial I get the following error: 当我尝试导入序列时,我收到以下错误:

Traceback (most recent call last):
  File "C:\Documents and Settings\eduardo.pereira\workspace\thgspeak\tst.py", line 7, in <module>
    import serial
  File "C:\Python27\lib\site-packages\serial\__init__.py", line 27, in <module>
    from serial.serialwin32 import Serial
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 15, in <module>
    from serial import win32
  File "C:\Python27\lib\site-packages\serial\win32.py", line 182, in <module>
    CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
  File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'CancelIoEx' not found

I have installed the latest version of pySerial, Python 2.7 runing on a WinXP laptop. 我在WinXP笔记本电脑上安装了最新版本的pySerial,Python 2.7。 Tried everywhere and found no similar problem. 到处尝试,发现没有类似的问题。 Is there any solution for that? 那有什么解决方案吗? Thanks in advance... 提前致谢...

The version of pySerial that you're using is trying to call a function that's only available in Windows Vista, whereas you're running Windows XP. 您正在使用的pySerial版本正在尝试调用仅在Windows Vista中可用的功能 ,而您正在运行Windows XP。

It might be worth experimenting with using an older version of pySerial. 可能值得尝试使用旧版本的pySerial。

The code in question was added to pySerial on 3 May 2016 , so a version just prior to that might be a good start. 有问题的代码已于2016年5月3日添加到pySerial ,因此在此之前的版本可能是一个良好的开端。

Older versions seem unavailable. 旧版本似乎不可用。 But, this worked for me (assuming nanpy version 3.1.1): 但是,这对我有用(假设nanpy版本3.1.1):

  1. open file \\lib\\site-packages\\serial\\serialwin32.py 打开文件\\ lib \\ site-packages \\ serial \\ serialwin32.py
  2. delete methods _cancel_overlapped_io() , cancel_read() , cancel_write() in lines 436-455 nearly at the botton of the file 删除方法_cancel_overlapped_io()cancel_read()cancel_write()在第436-455行几乎在文件的botton
  3. change method _close() als follows: 更改方法_close()als如下:

(Python) (蟒蛇)

def _close(self):
    """internal close port helper"""
    if self._port_handle is not None:
        # Restore original timeout values:
        win32.SetCommTimeouts(self._port_handle, self._orgTimeouts)
        # Close COM-Port:
        if self._overlapped_read is not None:
            win32.CloseHandle(self._overlapped_read.hEvent)
            self._overlapped_read = None
        if self._overlapped_write is not None:
            win32.CloseHandle(self._overlapped_write.hEvent)
            self._overlapped_write = None
        win32.CloseHandle(self._port_handle)
        self._port_handle = None

Additionally, create a non-default serial connection when starting the communication, otherwise you'll be bound to some linux device: 另外,在开始通信时创建一个非默认的串行连接,否则你将被绑定到一些linux设备:

a = ArduinoApi(SerialManager("COM5:"))

for i in range(10):
    a.pinMode(13, a.OUTPUT)
    a.digitalWrite(13, a.HIGH)
    # etc.

And in serial\\win32.py comments 并在串口\\ win32.py评论

#CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
#CancelIoEx.restype = BOOL
#CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM