Skip to content

Commit

Permalink
improve robustness of kat500 serial commands
Browse files Browse the repository at this point in the history
  • Loading branch information
n1kdo committed Feb 3, 2024
1 parent a1fe5da commit 965a665
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/kpa500-remote/kat500.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ async def kat500_server(self):
next_command += 1

# timeout = 2.0 if query in (b'MDA;', b'MDB;', b'MDM;') else 0.05
await self.device_send_receive(query, bl)
await self.device_send_receive(query, bl, retries=2)
if query == b'PS0;':
tuner_state = 1
logging.info('power off command, tuner state 3-->1', 'kat500:kat500_server')
Expand Down
29 changes: 19 additions & 10 deletions src/kpa500-remote/kdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,25 @@ def update_device_data(self, index, value):
if index not in network_client.update_list:
network_client.update_list.append(index)

async def device_send_receive(self, message, buf_and_length, wait_time=0.10):
# should the read buffer be flushed? can only read to drain
device_port = self.device_port
# empty the receive buffer
while device_port.readinto(buf_and_length.buffer) > 0:
logging.warning(f'waiting to send "{message}", rx buffer was not empty: "{buf_and_length.buffer}"')
device_port.write(message)
device_port.flush()
await asyncio.sleep(wait_time)
buf_and_length.bytes_received = device_port.readinto(buf_and_length.buffer)
async def device_send_receive(self, message, buf_and_length, wait_time=0.50, retries=1):
while retries > 0:
retries -= 1
device_port = self.device_port
# empty the receive buffer
while device_port.readinto(buf_and_length.buffer) > 0:
logging.warning(f'waiting to send "{message}", rx buffer was not empty: "{buf_and_length.buffer}"')
device_port.write(message)
device_port.flush()
while wait_time > 0:
await asyncio.sleep(0.01)
wait_time -= 0.01
if device_port.any() > 0:
break
buf_and_length.bytes_received = device_port.readinto(buf_and_length.buffer)
if buf_and_length.bytes_received > 0:
return
if retries > 0:
logging.debug(f'received {buf_and_length.bytes_received} bytes response to {message}, retrying')

@staticmethod
async def read_network_client(reader):
Expand Down
20 changes: 17 additions & 3 deletions src/kpa500-remote/serialport.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
import sys

impl_name = sys.implementation.name
if impl_name == 'cpython':
import serial
elif impl_name == 'micropython':
upython = impl_name == 'micropython'
if upython:
import machine
else:
import serial


class SerialPort:
Expand Down Expand Up @@ -75,6 +76,19 @@ def __init__(self, name='', baudrate=19200, timeout=0.040):
def close(self):
self.port.close()

def any(self):
if upython:
return self.port.any()
else:
return self.port.in_waiting()

def flush_input(self):
if upython:
if self.any():
_ = self.port.read()
else:
self.port.reset_input_buffer()

def write(self, buffer):
self.port.write(buffer)

Expand Down

0 comments on commit 965a665

Please sign in to comment.