work around firmware bug in KA3005P that impacts get_i()

This commit is contained in:
Joe Britton 2016-12-29 15:38:11 -07:00 committed by Sebastien Bourdeauducq
parent 010ce019b3
commit 2dd72fb94c
1 changed files with 16 additions and 14 deletions

View File

@ -38,12 +38,16 @@ class KoradKA3005P:
async def _ser_read(self, fixed_length=None): async def _ser_read(self, fixed_length=None):
""" strings returned by firmware are zero-terminated or fixed length """ strings returned by firmware are zero-terminated or fixed length
""" """
c = (await self.port.read(1)).decode() r = ""
r = c if self.simulation:
while len(c) > 0 and ord(c) != 0 and not len(r) == fixed_length: logger.info("simulation _ser_read()")
else:
c = (await self.port.read(1)).decode() c = (await self.port.read(1)).decode()
r += c r = c
logger.debug("_read %s: ", r) while len(c) > 0 and ord(c) != 0 and not len(r) == fixed_length:
c = (await self.port.read(1)).decode().rstrip('\0')
r += c
logger.debug("_read %s: ", r)
return r return r
async def _ser_write(self, cmd): async def _ser_write(self, cmd):
@ -106,21 +110,19 @@ class KoradKA3005P:
async def get_i(self): async def get_i(self):
"""Request the current as set by the user. """ """Request the current as set by the user. """
# Expected behavior of ISET1? is to return 5 bytes.
# ISET1? replies with a sixth byte on many models (all?) # However, if *IDN? has been previously called, ISET1? replies
# which is the sixth character from *IDN? # with a sixth byte 'K' which should be discarded. For consistency,
# reply if *IDN? was queried before (during same power cycle). # always call *IDN? before calling ISET1?.
# This byte is read and discarded. self.get_id()
await self._ser_write("ISET1?") await self._ser_write("ISET1?")
r = await self._ser_read(fixed_length=5) r = (await self._ser_read(fixed_length=6)).rstrip('K')
if r[0] == "K":
r = r[1:-1]
return float(r) return float(r)
async def measure_i(self): async def measure_i(self):
"""Request the actual output current.""" """Request the actual output current."""
await self._ser_write("IOUT1?") await self._ser_write("IOUT1?")
r = await self._ser_read(fixed_length=6) r = await self._ser_read(fixed_length=5)
if r[0] == "K": if r[0] == "K":
r = r[1:-1] r = r[1:-1]
return float(r) return float(r)