Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
create serial connector cmd channel lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguez committed Oct 20, 2023
1 parent 16a04eb commit f1ee594
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
15 changes: 15 additions & 0 deletions platform/panduza_platform/connectors/serial_tty.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SerialTty(SerialBase):
# Hold instances mutex
__MUTEX = asyncio.Lock()

# Hold instances mutex
__MUTEX = asyncio.Lock()

# Contains instances
__INSTANCES = {}

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

# ---
22 changes: 14 additions & 8 deletions platform/panduza_platform/drivers/bpc/drv_korad_ka3005p_bpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions platform/panduza_platform/drivers/bpc/drv_tenma_722710_bpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

# ---

0 comments on commit f1ee594

Please sign in to comment.