Trying to open a serial port with pyserial on WinXP -> "Access denied"

user250608 picture user250608 · Jan 14, 2010 · Viewed 37.4k times · Source

I'm trying to send data to an hplc pump via the serial port using python and pyserial. I tested the cable and the pump under linux (a gentoo derivative), where it worked perfectly, albeit as root. Now i have to use the code on a WinXP machine, where i always get an "Access denied" error when trying to open the port (i adjusted the parameters to COMxx instead of ttySxx, the port is found). I tried the serial port of the computer, as well as a USB2Serial adapter. I heard that WinXP was quite restrictive when it comes to trying to address some port with self written code. Is there a simpler workaround for this problem than installing linux?

# -*- coding: utf-8 -*-

import sys
import time
import serial
from threading import Thread

"""
usage: cmdCapture workDirectory pictureTime pressureTime
"""

print 'successful import is successful'

workDir=sys.argv[1]
pressureThresh=float(sys.argv[3])

class doCapture(Thread):
def __init__ (self, workDir, pressureThresh):
    Thread.__init__(self)

    self.workDir=workDir
    self.pressureThresh=pressureThresh
    self.pressureTimer=0

-> here i set the serial port

    self.ser=serial.Serial(port='\\.\COM1', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

-> here the error happens

    self.ser.open()

def getPressure(self):
    self.ser.write('PR')
    return self.ser.read(size=8), timer.timer()

def run(self):
    self.pressureTimer=time.timer()
    while 1:
        if self.pressureTimer<=(time.timer()-self.pressureTime):
            self.p=getPressure()
            print self.p

myCapture=doCapture(workDir, pressureThresh)
myCapture.start()

Answer

kgiannakakis picture kgiannakakis · Jan 14, 2010

Try opening the port as \\.\COMxx

Also make sure that the port isn't already open from another application. I recommend that you use Hyperterminal to see if the port is open.