import struct from typing import Tuple from . import tools def read_unique_identifier(address: bytes) -> bytes: return tools.pack_command(address, command_id=0) def read_primary_variable(address: bytes) -> bytes: return tools.pack_command(address, command_id=1) def read_loop_current_and_percent(address: bytes) -> bytes: return tools.pack_command(address, command_id=2) def read_dynamic_variables_and_loop_current(address: bytes) -> bytes: return tools.pack_command(address, command_id=3) def write_polling_address(address: bytes, new_polling_address: int) -> bytes: assert 0 <= new_polling_address <= 63 return tools.pack_command(address, command_id=6, data=new_polling_address.to_bytes(1, "big")) def read_unique_identifier_associated_with_tag(tag: bytes, *, address: int = 0) -> bytes: return tools.pack_command(address, command_id=11, data=tag) def read_message(address: bytes) -> bytes: return tools.pack_command(address, command_id=12) def read_tag_descriptor_date(address: bytes) -> bytes: return tools.pack_command(address, command_id=13) def read_primary_variable_information(address: bytes) -> bytes: return tools.pack_command(address, command_id=14) def read_output_information(address: bytes) -> bytes: return tools.pack_command(address, command_id=15) def read_final_assembly_number(address: bytes) -> bytes: return tools.pack_command(address, command_id=16) def write_message(address: bytes, message: str) -> bytes: """Writes a 24-char message to the device (Command 17).""" # Per spec, message is 24 characters, packed into 18 bytes. packed_message = tools.pack_packed_ascii(message, expected_len=32) return tools.pack_command(address, command_id=17, data=packed_message) def write_tag_descriptor_date(address: bytes, tag: str, descriptor: str, date: Tuple[int, int, int]): """Writes tag, descriptor, and date to the device (Command 18).""" # Per spec, tag is 6 chars, descriptor is 12 chars. packed_tag = tools.pack_packed_ascii(tag, 8) packed_descriptor = tools.pack_packed_ascii(descriptor, 16) data = packed_tag + packed_descriptor day, month, year = date data += day.to_bytes(1, "big") data += month.to_bytes(1, "big") data += year.to_bytes(1, "big") # print(len(data)) return tools.pack_command(address, command_id=18, data=data) def write_final_assembly_number(address: bytes, number: int): data = number.to_bytes(3, "big") return tools.pack_command(address, command_id=19, data=data) def write_primary_variable_damping(address: bytes, damping_time: float) -> bytes: """修改主变量阻尼时间(单位s) command 34""" data = struct.pack(">f", damping_time) return tools.pack_command(address, command_id=34, data=data) def write_primary_variable_range(address: bytes, units_code: int, upper_range: float, lower_range: float) -> bytes: """修改主变量量程范围 command 35""" data = units_code.to_bytes(1, "big") data += struct.pack(">ff", upper_range, lower_range) return tools.pack_command(address, command_id=35, data=data) def calibrate_20ma(address: bytes) -> bytes: """20mA校准 command 36""" return tools.pack_command(address, command_id=36) def calibrate_4ma(address: bytes) -> bytes: """4mA校准 command 37""" return tools.pack_command(address, command_id=37) def set_fixed_current_output(address: bytes, enable: bool, current_value: float = 0.0) -> bytes: """设置固定电流输出/退出固定模式 command 40""" if enable: data = struct.pack(">f", current_value) else: data = struct.pack(">f", 0) return tools.pack_command(address, command_id=40, data=data) def calibrate_zero_point(address: bytes) -> bytes: """零点校准 command 43""" return tools.pack_command(address, command_id=43) def write_primary_variable_units(address: bytes, units_code: int) -> bytes: """修改主变量单位 command 44""" data = units_code.to_bytes(1, "big") return tools.pack_command(address, command_id=44, data=data) def trim_loop_current_4ma(address: bytes, measured_current: float) -> bytes: """微调环路电流4mA command 45""" data = struct.pack(">f", measured_current) return tools.pack_command(address, command_id=45, data=data) def trim_loop_current_20ma(address: bytes, measured_current: float) -> bytes: """微调环路电流20mA command 46""" data = struct.pack(">f", measured_current) return tools.pack_command(address, command_id=46, data=data) def write_primary_variable_output_function(address: bytes, transfer_function_code: int) -> bytes: """修改主变量输出函数 command 47""" data = transfer_function_code.to_bytes(1, "big") return tools.pack_command(address, command_id=47, data=data)