Skip to content

Commit

Permalink
rename function arguments to match MeCom Command Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
bleykauf committed Nov 24, 2023
1 parent dcd3a22 commit 78c0575
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 93 deletions.
22 changes: 12 additions & 10 deletions meer_tec/mecom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ def calc_checksum(string: str) -> str:


def construct_mecom_cmd(
addr: int,
cmd_id: int,
device_addr: int,
param_id: int,
value_type: Type[FloatOrInt],
instance: int = 1, # for example to distinguish between CH1 and CH2
param_inst: int = 1, # for example to distinguish between CH1 and CH2
value: Optional[Union[float, int]] = None,
request_number: Optional[int] = None,
seq_num: Optional[int] = None,
) -> str:
"""Construct a VS or ?VR command."""
if request_number is None:
if seq_num is None:
# generate a random request number if none is given
request_number = random.randint(0, 65535)
seq_num = random.randint(0, 65535)

if value is not None:
cmd_type = "VS"
Expand All @@ -37,13 +37,15 @@ def construct_mecom_cmd(
cmd_type = "?VR"
val = ""

cmd = f"#{addr:02X}{request_number:04X}{cmd_type}{cmd_id:04X}{instance:02X}{val}"
cmd = (
f"#{device_addr:02X}{seq_num:04X}{cmd_type}{param_id:04X}{param_inst:02X}{val}"
)
return f"{cmd}{calc_checksum(cmd)}\r"


def verify_response(reponse: "Message", request: "Message") -> bool:
checksum_correct = reponse.checksum == calc_checksum(reponse[0:-5])
request_match = reponse.request_number == request.request_number
request_match = reponse.seq_num == request.seq_num
return checksum_correct & request_match


Expand All @@ -55,8 +57,8 @@ def __new__(cls, response: str, value_type: Type[FloatOrInt]):

def __init__(self, response: str, value_type: Type[FloatOrInt]) -> None:
self.value_type = value_type
self.addr = int(self[1:3])
self.request_number = int(self[3:7])
self.device_addr = int(self[1:3])
self.seq_num = int(self[3:7])
self.payload = self[7:-5]
self.checksum = self[-5:-1]

Expand Down
Loading

0 comments on commit 78c0575

Please sign in to comment.