尝试在 WinXP 上使用 pyserial 打开串行端口 ->“拒绝访问" [英] Trying to open a serial port with pyserial on WinXP -> "Access denied"

查看:171
本文介绍了尝试在 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.

这篇关于尝试在 WinXP 上使用 pyserial 打开串行端口 ->“拒绝访问"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆