【问题标题】:Using Serial on Python / Win7在 Python / Win7 上使用串行
【发布时间】:2011-08-27 07:35:05
【问题描述】:

我正在尝试与串行 WWAN 调制解调器连接(用于诊断/信号强度测量目的)。这是通过我桌面上的板载 COM1 (115200-8-n-1) 进行的,连接通过 PuTTY 工作。我可以使用 Python 编写 AT 命令脚本,但我很难让它打开串口。

我已经按照说明安装了 Python 2.7.1 和 PySerial。我是我机器的本地管理员,并且在有和没有管理员权限的情况下运行 Python,但是当我尝试打开端口时得到以下信息:

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> s = serial.Serial(
...     port='COM1',
...     baudrate=115200
... )
>>> s.open()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 56, in open
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.W
inError()))
serial.serialutil.SerialException: could not open port COM1: [Error 5] Access is
 denied.

据我了解,8-N-1 是默认设置,即使我尝试手动设置它们也会引发相同的异常。

有人可以提供建议吗? 提前致谢。

【问题讨论】:

    标签: python windows-7 serial-port pyserial


    【解决方案1】:

    您无需致电open。构造函数已经这样做了,并且在 Windows 上打开 COM 端口的任何人都可以独占访问它,直到他们关闭它。

    关于第二个问题,请看How can I fix "[Error 6] The handle is invalid." with PySerial

    【讨论】:

    • 感谢@aix 的回复。我在新的提升 py 会话中尝试了不同的语法,现在得到一个稍微不同的错误:&gt;&gt; se = serial.Serial() &gt;&gt; se.baudrate = 115200 &gt;&gt; se.bytesize = 8 &gt;&gt; se.stopbits = 1 &gt;&gt; se.xonxoff = 0 &gt;&gt; se.rtscts = 0 &gt;&gt; se.port = 0 # = COM1 &gt;&gt; parity=serial.PARITY_NONE &gt;&gt; se.isOpen() False &gt;&gt; se.open() &gt;&gt; se.write('AT\r\n') Traceback (most recent call last): ... raise SerialException("WriteFile failed (%s)" % ctypes.WinError()) serial.serialutil.SerialException: WriteFile failed ([Error 6] The handle is invalid.) 有什么想法吗? PS对不起重新:格式
    • @user330600:您能否编辑您的问题以添加此新信息?代码/异常在评论中有点难以阅读。
    • 谢谢! 64 位解释器是一个严重的问题。切换到 32 位有效!
    【解决方案2】:

    这是我为消除Access Denied Error所做的:

    1. Ctrl + Alt + Del

    2. 选择pythonw.exe并按结束进程

    3. 在 IDLE 中重新运行您的应用程序

    它应该在没有任何拒绝访问错误的情况下运行。

    【讨论】:

      【解决方案3】:

      您需要在提升模式下运行python.exe。右键点击Run as administrator

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-03
        • 2016-05-04
        • 1970-01-01
        相关资源
        最近更新 更多