diff --git a/platform/panduza_platform/connectors/serial_tty.py b/platform/panduza_platform/connectors/serial_tty.py index 1ad5660..ceadd36 100644 --- a/platform/panduza_platform/connectors/serial_tty.py +++ b/platform/panduza_platform/connectors/serial_tty.py @@ -15,6 +15,9 @@ class SerialTty(SerialBase): # Hold instances mutex __MUTEX = asyncio.Lock() + # Hold instances mutex + __MUTEX = asyncio.Lock() + # Contains instances __INSTANCES = {} @@ -90,6 +93,10 @@ def __init__(self, loop,**kwargs): # Init local mutex self._mutex = asyncio.Lock() + # Init command mutex + self._cmd_mutex = asyncio.Lock() + + # Init time lock self._time_lock_s = None @@ -125,6 +132,14 @@ async def connect(self): # ============================================================================= # OVERRIDE FROM SERIAL_BASE + + async def beg_cmd(self): + await self._cmd_mutex.acquire() + + async def end_cmd(self): + self._cmd_mutex.release() + + # --- async def read_data(self, n_bytes = None): diff --git a/platform/panduza_platform/drivers/ammeter/drv_korad_ka3005p_ammeter.py b/platform/panduza_platform/drivers/ammeter/drv_korad_ka3005p_ammeter.py index 02c7f4d..1585e96 100644 --- a/platform/panduza_platform/drivers/ammeter/drv_korad_ka3005p_ammeter.py +++ b/platform/panduza_platform/drivers/ammeter/drv_korad_ka3005p_ammeter.py @@ -39,9 +39,11 @@ async def _PZA_DRV_loop_init(self, loop, tree): # --- async def _PZA_DRV_AMMETER_read_measure_value(self): + await self.serial_connector.beg_cmd() cmd = "IOUT1?" await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) current = await self.serial_connector.read_data(n_bytes=5) + await self.serial_connector.end_cmd() return float(current) # --- \ No newline at end of file diff --git a/platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py b/platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py index cdf391f..db94cba 100644 --- a/platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py +++ b/platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py @@ -53,35 +53,39 @@ async def _PZA_DRV_loop_init(self, loop, tree): ########################################################################### async def _PZA_DRV_BPC_read_enable_value(self): - await asyncio.sleep(1) + # await asyncio.sleep(1) print("Sending cmd: {}".format("STATUS?")) + await self.serial_connector.beg_cmd() await self.serial_connector.write_data("STATUS?", time_lock_s=COMMAND_TIME_LOCK) status = await self.serial_connector.read_data(n_bytes=1) print("LOL", status) + await self.serial_connector.end_cmd() return bool(status[0] & (1 << 6)) async def _PZA_DRV_BPC_write_enable_value(self, v): - await asyncio.sleep(1) + await self.serial_connector.beg_cmd() cmd = "OUT{}".format(int(v)) print("Sending cmd: {}".format(cmd)) await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) status = await self.serial_connector.read_data(n_bytes=1) - await asyncio.sleep(1) + await self.serial_connector.end_cmd() # --- async def _PZA_DRV_BPC_read_voltage_value(self): - await asyncio.sleep(0.2) + await self.serial_connector.beg_cmd() cmd = "VSET1?" await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) voltage = await self.serial_connector.read_data(n_bytes=5) return float(voltage) + await self.serial_connector.end_cmd() async def _PZA_DRV_BPC_write_voltage_value(self, v): - await asyncio.sleep(0.2) + await self.serial_connector.beg_cmd() v = "{:05.2f}".format(v) cmd = "VSET1:{}".format(v) await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) + await self.serial_connector.end_cmd() async def _PZA_DRV_BPC_voltage_value_min_max(self): return VOLTAGE_BOUNDS @@ -92,18 +96,20 @@ async def _PZA_DRV_BPC_read_voltage_decimals(self): # --- async def _PZA_DRV_BPC_read_current_value(self): - await asyncio.sleep(0.2) + await self.serial_connector.beg_cmd() cmd = "ISET1?" await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) current = await self.serial_connector.read_data(n_bytes=5) + await self.serial_connector.end_cmd() return float(current) async def _PZA_DRV_BPC_write_current_value(self, v): - await asyncio.sleep(0.2) + await self.serial_connector.beg_cmd() v = "{:05.3f}".format(v) cmd = "ISET1:{}".format(v) await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) - + await self.serial_connector.end_cmd() + async def _PZA_DRV_BPC_current_value_min_max(self): return CURRENT_BOUNDS diff --git a/platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py b/platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py index b08d7f2..857d689 100644 --- a/platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py +++ b/platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py @@ -67,10 +67,11 @@ async def _PZA_DRV_loop_init(self, loop, tree): async def _PZA_DRV_BPC_read_enable_value(self): # Send "STATUS?" to get back the output state - + await self.serial_connector.beg_cmd() await self.serial_connector.write_data(f"STATUS?\n", time_lock_s=COMMAND_TIME_LOCK) statusBytes = await self.serial_connector.read_data() - + await self.serial_connector.end_cmd() + self.log.debug(f"{statusBytes.strip()}") status = ord(statusBytes.strip()) diff --git a/platform/panduza_platform/drivers/voltmeter/drv_korad_ka3005p_voltmeter.py b/platform/panduza_platform/drivers/voltmeter/drv_korad_ka3005p_voltmeter.py index 247f9a2..9f8789c 100644 --- a/platform/panduza_platform/drivers/voltmeter/drv_korad_ka3005p_voltmeter.py +++ b/platform/panduza_platform/drivers/voltmeter/drv_korad_ka3005p_voltmeter.py @@ -40,8 +40,11 @@ async def _PZA_DRV_loop_init(self, loop, tree): async def _PZA_DRV_VOLTMETER_read_measure_value(self): cmd = "VOUT1?" + await self.serial_connector.beg_cmd() await self.serial_connector.write_data(cmd, time_lock_s=COMMAND_TIME_LOCK) voltage = await self.serial_connector.read_data(n_bytes=5) + await self.serial_connector.end_cmd() + return float(voltage) # --- \ No newline at end of file