43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from communication import convert
|
|
from communication.convert import float_to_int16_n
|
|
|
|
|
|
class ComCmd:
|
|
|
|
class OperationType:
|
|
READ = 0
|
|
WRITE = 1
|
|
|
|
def __init__(self, op_type: OperationType, p_name, addr, length, write_vals=None):
|
|
self.op = op_type
|
|
self.param_name = p_name
|
|
self.addr = int(addr)
|
|
self.len = int(length)
|
|
if op_type == self.OperationType.WRITE:
|
|
self.write_vals = float_to_int16_n(write_vals)
|
|
|
|
def resolve_read_response(self, reg_data):
|
|
res_data = []
|
|
for i in range(0, self.len, 2):
|
|
res_data.append(convert.int16_to_float(reg_data[i], reg_data[i + 1]))
|
|
return res_data
|
|
|
|
def resolve_write_response(self, reg_data):
|
|
"""
|
|
解析write命令的响应数据
|
|
:param reg_data:
|
|
:return:
|
|
"""
|
|
# 对于写入操作,只用反馈成功或者失败
|
|
return None
|
|
|
|
def modbus_write_vals(self):
|
|
if self.write_vals is None:
|
|
return
|
|
|
|
_int16_write_vals = convert.float_to_int16_n(self.write_vals)
|
|
return _int16_write_vals
|
|
|
|
|
|
|