I have following code design:
class A:
def __init__(self, serial):
self.serial = serial
def callback(self, data):
if data.type == 5:
print(data.response)
if data.type == 10:
print(data.response)
if data.type == 15:
print(data.response)
def command(self):
self.serial.send(5, self.callback)
self.serial.send(10, self.callback)
self.serial.send(15, self.callback)
serial
has a queue in which stream data is coming, the queue data is then read in a separate thread and command respective callbacks are called. (This design can also be changed if needed).
now what I am looking for is to send the response from the command
function itself.
def command(self):
self.serial.send(5, self.callback)
self.serial.send(10, self.callback)
self.serial.send(15, self.callback)
status1 = self.get_status(5)
status2 = self.get_status(10)
status3 = self.get_status(15)
return status1, status2, status3
current Ideas to implement get_status
:
To have a wait for timeout time and check if command response received. (need to have a separate variable for each command, also there can be some commands that don't require to return any response)
timeout = 2 # in sec
while not self.response5 and timeout > 0:
time.sleep(0.5)
timeout -= 0.5
will store data.response
in self.response5
in callaback otherwise None
Is there any better and easier way?
Aucun commentaire:
Enregistrer un commentaire