I reconstruct my code to more readable by using class and follow javascript style. The class is about modem device. In below I have 2 inner classes, SMS and Identify. For now it focuss on retrieve device informations. The problem it got error
'Identify' object has no attribute 'handle'
Inner class can't access outer attribute class.
import serial
class Device:
def open(self, port, baudrate):
self.handle = serial.Serial(port, baudrate)
def readline(self):
return self.handle.readline()
def close(self):
self.handle.close()
class SMS:
pass
class Identify:
def manufacturer(self):
self.handle.write(b'AT+CGMI
')
while True:
buffer = self.handle.readline()
print(buffer)
if buffer == b'OK
':
break
elif buffer == b'ERROR
':
break
device = Device()
device.open('COM12', 9600)
device.Identify().manufacturer()
device.close()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…