尝试在 WinXP 上使用 pyserial 打开串行端口 ->“拒绝访问"

问题描述

我正在尝试使用 python 和 pyserial 通过串行端口将数据发送到 hplc 泵.我在 linux(gentoo 衍生产品)下测试了电缆和泵,尽管它是 root 用户,但它工作得很好.现在我必须在 WinXP 机器上使用代码,在尝试打开端口时总是出现拒绝访问"错误(我将参数调整为 COMxx 而不是 ttySxx,找到了端口).我试过电脑的串口,还有一个USB2Serial适配器.我听说 WinXP 在尝试使用自写代码解决某些端口时非常严格.有没有比安装 linux 更简单的解决方法?

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

->这里我设置了串口

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

->这里发生错误

    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()


解决方案

尝试打开端口为\.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.

相关文章