为什么我不首先设置self.ser = False
,我无法将self.ser
变量作为使用pyserial
写入的句柄传递?
import serial
import time
class TestClass(object):
def __init__(self, port='/dev/ttyUSB0', baud=9600):
self.baud = baud
self.port = port
self.ser = False
def connect(self):
"""Opens serial connection to Device"""
self.ser = serial.Serial(self.port, self.baud, timeout=.5)
def writeuart(self, message):
self.ser.flushInput()
self.ser.write(message)
time.sleep(1)
return self.ser.read(self.ser.inWaiting())
换句话说,我是否可以在self.ser
方法中创建connect()
,然后在同一类的其他方法中使用self.ser
?
答案 0 :(得分:-1)
def __init__(self, port='/dev/ttyUSB0', baud=9600):
self.baud = baud
self.port = port
self.connect() # it's ok