在Python/Win7上使用串口
我正在尝试与串行 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是默认值,即使我尝试手动设置它们会引发相同的异常。
有人可以提供建议吗? 提前致谢。
I am trying to interface with a serial WWAN modem (for diagnostics /signal strength measurement purposes). This is via the onboard COM1 (115200-8-n-1) on my desktop, the connection works via PuTTY. I can script the AT commands using Python, but I am having a tough time getting it to open the serial port .
I have installed Python 2.7.1 and PySerial according to instructions. I am the local administrator of my machine and have run Python with and without admin privileges, but I get the following when I try to open the port:
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.
It is my understanding that 8-N-1 is the default, and even when I try to set them manually it throws the same exception.
Can anyone offer advice?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要调用
open
。构造函数已经做到了这一点,并且在 Windows 上,无论谁打开 COM 端口,都可以独占访问它,直到他们关闭它。关于第二个问题,看看 如何修复“[错误 6] 句柄无效。”使用 PySerial
You don't need to call
open
. The constructor already does that, and on Windows whoever opens a COM port has exclusive access to it until they close it.As to the second problem, take a look at How can I fix "[Error 6] The handle is invalid." with PySerial
以下是我消除
访问被拒绝错误
的方法:按Ctrl + Alt + Del
选择
pythonw.exe
并按结束进程在IDLE中重新运行您的应用程序
它应该在没有任何访问被拒绝的错误的情况下运行。
Here is what I do to eliminate
Access Denied Error
:Press Ctrl + Alt + Del
Select
pythonw.exe
and press End ProcessRe-Run your application in IDLE
It should be run without any access denied error.
您需要在提升模式下运行
python.exe
。右键单击并单击以管理员身份运行
You need to be running
python.exe
in elevated mode. Right click and clickRun as administrator