好的,我想通了。我追溯到未能實例化打開COM端口(在Komodo IDE中調試非常有幫助)。它無法打開端口,雖然pyserial的包裝器(下圖)發現錯誤,但我的代碼沒有做任何事情。所以,當我試圖使用串行方法,即`ser.write()'(其中ser是我的端口實例)時,我得到了AttributeError。
端口未能打開,因爲另一端的盒子通信不正常。重置這個盒子(一個馬達控制器)解決了這個問題。
我用於PySerial的包裝看起來是內部代碼---簡短但有用。我在這裏分享它:
SerialIO:包裝爲PySerial
#----------------------------------------------------------------------
# serialIO: wrappers for pyserial module for exception handling, etc.
#----------------------------------------------------------------------
# depends on http://pyserial.sourceforge.net/
import serial
import sys
import time
##################################### serial handlers
# all serial commands except stop are filtererd through this
class serialIO():
def __init__(self,port, baudrate, timeout=0.0,echo=False,rtscts=False):
self.port = port
self.br = baudrate
self.to = timeout
self.echo = echo
self.rtscts = rtscts
self.stdout = False
self.ser = None
self.status = "OK"
self.reopen()
def reopen(self):
# first close it if it's already open
if self.ser:
try:
self.ser.close()
except serial.SerialException, v:
self.status = "error"
# do we want stdout (for debug)?
if self.port == "stdout":
self.stdout = True
self.ser = sys.stdout
else: # nope, actually open the prort
try: # xonxoff=True hoses binary polling of Faulhabers!
self.ser = serial.Serial(self.port, self.br,
timeout=self.to,
rtscts =self.rtscts,
xonxoff=False)
except serial.SerialException, v:
self.status = "error"
print str(v)
return(self.status)
def echo(self,str):
pass
# write the given string to the serial device, catching exceptions
def safewrite(self,string,flush=True):
try:
result = self.ser.write(string)
if flush:
if self.stdout:
sys.stdout.flush()
else:
self.ser.flushOutput()
except serial.SerialException, v:
print str(v)
return(-1)
return(result)
def REALLYflushInput(self):
""" because ser.flushInput() doesn't seem to work :/"""
if self.stdout:
return
self.ser.flushInput()
time.sleep(0.01)
#self.ser.flushInput()
readstr = ""
incount = self.ser.inWaiting()
while (incount > 0):
readstr += self.ser.read(incount)
time.sleep(0.01)
incount = self.ser.inWaiting()
# write the given string to the serial device
def write(self,string,flush=True):
self.ser.write(string)
if flush:
if self.stdout:
sys.stdout.flush()
else:
self.ser.flushOutput()
def read(self,nb):
return(self.ser.read(nb))
# returns number of bytes successfully read, -1 if error
def saferead(self):
if self.stdout:
return("",0)
nread = 0
try:
incount = self.ser.inWaiting()
except serial.SerialException, v:
incount = 0
raise
rstr = ""
while (incount > 0):
try:
rstr += self.ser.read(incount)
except serial.SerialException, v:
print str(v)
return(rstr,-1)
nread = nread + incount
incount = self.ser.inWaiting()
return(rstr,nread)
def readstr(self):
incount = self.ser.inWaiting()
readstring = ""
while (incount > 0):
readstring += self.ser.read(incount)
incount = self.ser.inWaiting()
if self.echo:
self.log("read %s" % readstring)
return readstring
# resturn a list of available serial devices
def GetPortList(plist):
# for port in portlist:
if len(plist) < 1:
# if no list of portnames, make one
plist = ['COM2','COM3','COM4','COM5']
OKports = []
for port in plist:
ser = None
try:
ser = serial.Serial(port, 9600)
except serial.SerialException, v:
#print str(v)
pass
else:
OKports.append(ser.name)
finally:
if(ser):
ser.close()
return OKports
if __name__ == '__main__':
plist = GetPortList([])
print str(plist)
所以它被初始化爲'SER = serialIO.serialIO(端口名,波特率)'。如果這是返回NoneType,是否意味着它找不到serialIO?再次,多年來沒有什麼改變,但也許是關於圖書館如何存儲的。系統經常備份。我不知道如何診斷這一點。 – DanG
我在查找該功能的文檔時遇到問題。它在哪個圖書館?然而,我發現這個[錯誤報告](http://sourceforge.net/p/pyserial/bugs/158/),它討論了serialIO寫入現在接受字節而不是字符串的變化。所以可能出現其他一些事情已經改變的情況。現在可能以不同的方式初始化serialIO類。但我似乎無法找到確切的代碼。 – cnluzon
噢,我想我找到了它,但是它可能會變成一個叫做Serial的類? http://pyserial.sourceforge.net/pyserial_api.html#classes。我編輯了我的答案,使它更有用。希望能幫助到你。 – cnluzon