0

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.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Tony Laidig
  • 1,048
  • 2
  • 11
  • 33

3 Answers3

6

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

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks for the response @aix. I tried a different syntax in a new elevated py session, and am now getting a slightly different error: `>> se = serial.Serial() >> se.baudrate = 115200 >> se.bytesize = 8 >> se.stopbits = 1 >> se.xonxoff = 0 >> se.rtscts = 0 >> se.port = 0 # = COM1 >> parity=serial.PARITY_NONE >> se.isOpen() False >> se.open() >> se.write('AT\r\n') Traceback (most recent call last): ... raise SerialException("WriteFile failed (%s)" % ctypes.WinError()) serial.serialutil.SerialException: WriteFile failed ([Error 6] The handle is invalid.)` Any ideas? PS Sorry re:format – Tony Laidig May 17 '11 at 15:02
  • @user330600: Could you edit your question to add this new info? The code/exception are a bit hard to read in a comment. – NPE May 17 '11 at 15:07
  • @user330600: Take a look at http://stackoverflow.com/questions/3028786/how-can-i-fix-error-6-the-handle-is-invalid-with-pyserial – NPE May 17 '11 at 15:08
  • Thanks! 64-bit interpreters are a serious issue. Switching to 32bit worked! – Tony Laidig May 17 '11 at 15:30
4

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
oyildirim
  • 51
  • 3
0

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

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91