在Python / Win7上使用Serial

时间:2011-05-17 14:43:26

标签: python windows-7 serial-port pyserial

我正在尝试与串行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是默认值,即使我尝试手动设置它们也会抛出相同的异常。

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

3 个答案:

答案 0 :(得分:5)

您无需致电open。构造函数已经这样做了,在Windows上,无论谁打开COM端口,都可以对它进行独占访问,直到它们关闭它为止。

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

答案 1 :(得分:3)

以下是我要做的消除Access Denied Error

的方法
  1. Ctrl + Alt + Del

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

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

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

答案 2 :(得分:0)

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