Using Serial on Python / Win7
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.
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
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.
You need to be running python.exe
in elevated mode. Right click and click Run as administrator
精彩评论