5

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()
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • post some code as there is no way to ascertain your process without it. – KevinDTimm Jan 14 '10 at 09:55
  • 1
    Note that you need the `\\.\COMxx` thing instead of just `COMx` only when the port number is 10 or greater (but therefore, obviously, use it all the time). It's better to use forward slashes, however, as they avoid problems with backslash escapes. Either that, or use raw strings with `r''`. – Peter Hansen Jan 14 '10 at 11:35

7 Answers7

11

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.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • no, same trouble - opening the port causes an "access denied" error –  Jan 14 '10 at 10:21
  • 6
    ok - thanks, that helped. i could easily control my pump from hyperterminal. obviously the port was already open when initialized. closing it (self.ser.close()) before opening solved the problem. –  Jan 14 '10 at 11:12
8

.close() before I called the .open() worked for me as well

Madmartigan
  • 392
  • 1
  • 3
  • 7
  • 4
    To those coming here for an answer - this is _not_ the answer you are looking for. The reason this works is because (as pointed out elsewhere) `.open()` is not needed, the port is opened when the serial instance is created. So closing it after you've created the port simply stops the error that would arise when you (incorrectly) open it again. – SiHa Jul 25 '14 at 11:19
5

No need to call .open if you already pass the serial port name/number in the constructor!

David
  • 51
  • 1
  • 1
3

When you execute

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

the serial port is already open. On Windows, you get an error if you run .open() if the port is already open, but you don't on Linux. That's where the discrepancy is.

Cory Cross
  • 31
  • 1
1

Calling .close() before opening the port solved a problem that was driving me nuts!

I had it working on another machine running vista 64-bit using com0com virtual ports, not a hitch.

I was trying to work on the exact same scripts on my Windows 7 box - nada - XP Mode same thing. Access Denied or couldn't find the port (when changing up how the port was addressed). HyperTerminal, of course, recognized and worked with everything flawlessly.

4 hours later I find this little nugget and now everything is humming along just fine.

It's a good thing I enjoy programming...GAH!

Crazy Joe Malloy
  • 834
  • 6
  • 14
0

Be sure the port is not open by any other program. (That's worked for me)

forzagreen
  • 2,509
  • 30
  • 38
0

I had a similar problem when i was trying to get accelerometer values from TI Chronos. In the device manager i just disabled the COM port and enabled it again. Worked

Jay
  • 1,392
  • 7
  • 17
  • 44