在Python/Win7上使用串口

发布于 2024-11-07 23:18:21 字数 958 浏览 3 评论 0原文

我正在尝试与串行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不是你的备胎 2024-11-14 23:18:21

您不需要调用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

遥远的她 2024-11-14 23:18:21

以下是我消除访问被拒绝错误的方法:

  1. Ctrl + Alt + Del

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

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

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

Here is what I do to eliminate Access Denied Error:

  1. Press Ctrl + Alt + Del

  2. Select pythonw.exe and press End Process

  3. Re-Run your application in IDLE

It should be run without any access denied error.

§普罗旺斯的薰衣草 2024-11-14 23:18:21

您需要在提升模式下运行python.exe。右键单击并单击以管理员身份运行

You need to be running python.exe in elevated mode. Right click and click Run as administrator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文