Complete the code to set the Modbus function code for reading coils.
modbus_request = {"function_code": [1], "address": 0x0013, "quantity": 19}The Modbus function code 0x01 is used to read coils (digital outputs).
Complete the code to specify the Modbus register type for holding registers.
register_type = "[1]"
Holding registers are used to store data that can be read or written. The correct type is holding_register.
Fix the error in the Modbus RTU frame checksum calculation.
checksum = calculate_crc([1])The checksum is calculated over the entire frame except the last two bytes which contain the checksum itself. So use modbus_frame[:-2].
Fill both blanks to create a Modbus TCP request header with transaction ID and protocol ID.
header = [1].to_bytes(2, 'big') + [2].to_bytes(2, 'big') + b'\x00\x06'
The Modbus TCP header starts with a 2-byte transaction ID and a 2-byte protocol ID. So the first blank is transaction_id and the second is protocol_id.
Fill all three blanks to build a dictionary comprehension filtering coils with status ON.
active_coils = {addr: status for addr, status in coils.items() if status [1] [2] and addr [3] 100}The comprehension filters coils where status is True (ON) and address is less than 100. So the blanks are ==, True, and <.