Using Serial on Python / Win7

折月煮酒 提交于 2019-11-28 12:10:05

问题


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.


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6032568/using-serial-on-python-win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!