Skip to content

Commit

Permalink
Update in handler, now is possible to try to read in all iterations, …
Browse files Browse the repository at this point in the history
…it only reads if there is data available
  • Loading branch information
RobertoAldea committed Jan 11, 2024
1 parent 829cb89 commit 539df74
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
54 changes: 27 additions & 27 deletions code/drv_scpi/src/scpi_sniffer/drv_scpi_iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
####################### MODULE IMPORTS #######################

####################### ENUMS #######################

###################### CONSTANTS ######################
from .context import DEFAULT_NUM_ATTEMPTS, DEFAULT_RX_CHAN
NUM_ATTEMPTS = 10

####################### CLASSES #######################
class DrvScpiErrorC(Exception):
Expand All @@ -43,7 +41,7 @@ def __str__(self) -> str:

class DrvScpiHandlerC:
"Driver for SCPI devices."
def __init__(self, serial_conf: DrvScpiSerialConfC, rx_chan_name: str= DEFAULT_RX_CHAN) -> None:
def __init__(self, serial_conf: DrvScpiSerialConfC, rx_chan_name: str) -> None:
'''
Args:
- serial_conf (DrvScpiSerialConfC): Configuration of the serial port.
Expand Down Expand Up @@ -94,7 +92,7 @@ def decode_and_split(self, data: bytes) -> List[str]:
- None.
"""
data_dec = data.decode("utf-8")
msg_decode = data_dec.split(f"{self.__separator}")
msg_decode = data_dec.split(f"{self.__separator}")[0]
return msg_decode


Expand All @@ -121,32 +119,34 @@ def read(self) -> None:
Raises:
- None.
'''
msg_read = self.__serial.readline()
log.debug(f"Port: {self.__serial.port} \t Message read RX: {msg_read}")

resp_msg = DrvScpiCmdDataC(port = self.__serial.port, data_type = DrvScpiCmdTypeE.RESP,
payload = [], status = SysShdNodeStatusE.OK)
if len(msg_read) > 0:
msg_read_decoded = self.decode_and_split(msg_read)
self.wait_4_response = False
self.num_attempts_read = 0
self.status = SysShdNodeStatusE.OK

# Update message
resp_msg.payload = msg_read_decoded
resp_msg.status = self.status
log.info(f"Rx chan: {self.__rx_chan_name}. Message send: {resp_msg.payload}")
self.__rx_chan.send_data(resp_msg)
else:
self.num_attempts_read += 1
if self.num_attempts_read >= DEFAULT_NUM_ATTEMPTS:
log.critical(f"rx chan: {self.__rx_chan_name}. No response from device") # pylint: disable=logging-fstring-interpolation
self.status = SysShdNodeStatusE.COMM_ERROR
if self.__serial.readable() and self.__serial.in_waiting > 0:
msg_read = self.__serial.readlines()
log.warning(f"Port: {self.__rx_chan_name} \t Message read RX: {msg_read}")

resp_msg = DrvScpiCmdDataC(port = self.__serial.port, data_type = DrvScpiCmdTypeE.RESP,
payload = [], status = SysShdNodeStatusE.OK)
if len(msg_read) > 0:

msg_read_decoded = [self.decode_and_split(msg_read_partially) for msg_read_partially in msg_read]
self.wait_4_response = False
self.num_attempts_read = 0
self.status = SysShdNodeStatusE.OK

# Update message
resp_msg.payload = []
resp_msg.payload = msg_read_decoded
resp_msg.status = self.status
log.info(f"Rx chan: {self.__rx_chan_name}. Message send: {resp_msg.payload}")
self.__rx_chan.send_data(resp_msg)
else:
self.num_attempts_read += 1
if self.num_attempts_read >= NUM_ATTEMPTS and self.wait_4_response:
log.critical(f"rx chan: {self.__rx_chan_name}. No response from device") # pylint: disable=logging-fstring-interpolation
self.status = SysShdNodeStatusE.COMM_ERROR

# Update message
resp_msg.payload = []
resp_msg.status = self.status
self.__rx_chan.send_data(resp_msg)

def close(self) -> None:
''' Close the serial connection.
Expand Down
7 changes: 4 additions & 3 deletions code/drv_scpi/src/scpi_sniffer/drv_scpi_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ def __receive_response(self) -> None:
'''
for handler in self.__used_dev.values():
handler: DrvScpiHandlerC
if handler.wait_4_response:
handler.read()

handler.read()
# if handler.wait_4_response:
# handler.read()
# handler.read()

def process_iteration(self) -> None:
''' Read the chan.
Expand Down

0 comments on commit 539df74

Please sign in to comment.