ljean / modbus-tk

Create Modbus app easily with Python
Other
557 stars 211 forks source link

defines.READ_DISCRETE_INPUTS Unable to read data #143

Open Ginwyboo opened 3 years ago

Ginwyboo commented 3 years ago

modbus_tk/modbus.py 156 line ` if function_code == defines.READ_COILS or function_code == defines.READ_DISCRETE_INPUTS: is_read_function = True pdu = struct.pack(">BHH", function_code, starting_address, quantity_of_x)

        byte_count = quantity_of_x // 8
        if (quantity_of_x % 8) > 0:
            byte_count += 1
        nb_of_digits = quantity_of_x

        if not data_format:
            data_format = ">" + (byte_count * "B")
        if expected_length < 0:
            # No length was specified and calculated length can be used:
            # slave + func + bytcodeLen + bytecode + crc1 + crc2
            expected_length = byte_count + 5

error Traceback (most recent call last): File "D:/Ginbo/python/smtos1.1/app/protocols/Modbus.py", line 244, in print(mod.master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 1)) File "D:\Ginbo\python\smtos1.1\senv\lib\site-packages\modbus_tk\utils.py", line 39, in new raise excpt File "D:\Ginbo\python\smtos1.1\senv\lib\site-packages\modbus_tk\utils.py", line 37, in new ret = fcn(*args, **kwargs) File "D:\Ginbo\python\smtos1.1\senv\lib\site-packages\modbus_tk\modbus.py", line 312, in execute response_pdu = query.parse_response(response) File "D:\Ginbo\python\smtos1.1\senv\lib\site-packages\modbus_tk\modbus_rtu.py", line 64, in parse_response raise ModbusInvalidResponseError("Invalid CRC in response") modbus_tk.exceptions.ModbusInvalidResponseError: Invalid CRC in response ` expected_length Calculation error This calculation is not correct Can't wait for the real expected length