Skip to content

Commit

Permalink
minor performance improvements / bump to v0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
teauxfu committed Apr 21, 2021
1 parent 0d0a5dd commit adbe4a8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

v0.1.6
------
- Yet another minor performance improvement to write operations
- Minor performance improvement to read operations
- Cleaning up some code comments

v0.1.5
------
- Minor performance improvement to pump's write operation
Expand Down
32 changes: 18 additions & 14 deletions py_hplc/pump_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,21 @@ def write(self, msg: str, delay: float = 0.015) -> str:
"""
response = ""
tries = 1
# pump docs recommend 3 attempts
while tries <= 3:
cmd = "".join((msg, "\r")).encode() # defaults to utf-8
while tries <= 3: # pump docs recommend 3 attempts
# this would clear the pump's command buffer, but shouldn't be relied upon
# self.serial.write(b"#")
self.serial.reset_input_buffer()
self.serial.reset_output_buffer()
sleep(delay) # let the buffers clear (could defer here if async)

# it seems getting pre-encoded strings from a dict is only slightly faster,
# and only some of the time, when compared to just encoding args on the fly
self.serial.write(msg.encode() + b"\r")
self.logger.debug("Sent %s (attempt %s/3)", msg, tries)
sleep(delay) # let the hardware buffers clear (could defer here if async)
self.serial.write(cmd)
self.serial.flush() # sleeps on a tight loop until everything is written
self.logger.debug("Sent %s (attempt %s/3)", msg, tries)

if msg == "#": # this won't give a response
break
sleep(delay) # let the pump respond
response = self.read()
response = self.read() # returns an already-decoded string
if "OK" in response: # no need to retry
break
tries += 1
Expand All @@ -187,14 +184,21 @@ def write(self, msg: str, delay: float = 0.015) -> str:
return response

def read(self) -> str:
"""Reads a single message from the pump."""
response = ""
"""Reads a single message from the pump.
Returns:
str: The pump's response, or an empty string if no response is given.
"""
response = b""
tries = 1
while tries <= 3 and "/" not in response:
response = self.serial.read_until(b"/").decode()
while tries <= 3:
response = self.serial.read_until(b"/") # we don't know the size a priori
self.logger.debug("Got response: %s (attempt %s/3)", response, tries)
if b"/" in response: # b"/" is the pump's EOL flag
break
tries += 1
return response

return response.decode()

def close(self) -> None:
"""Closes the serial port associated with the pump."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "py-hplc"
version = "0.1.5"
version = "0.1.6"
description = "An unoffical Python wrapper for the SSI-Teledyne Next Generation class HPLC pumps."
license = "MIT"
repository = "https://github.com/teauxfu/py-hplc"
Expand Down

0 comments on commit adbe4a8

Please sign in to comment.