Data from dynamixcel XM540-270 some time change structure

Hi, I’m using the XM540-270 to simulate a mechanical spring via a control loop implemented in Python.
Everything works fine but occasionally the Dynamixel SDK library returns an error, sometime after 5-10s and sometime after many minutes (it seams that the error appear randomly, while the motor is under load and while the motor is powered (torque on and control loop in action) but with no load applied)
The error is the following:
“dynamixel_sdk\”, in read4ByteTxRx:
DXL_MAKEWORD(data[2],data[3])) if (result== COMM_SUCCESS) else 0

IndexError: list index (data[2]) out of range

I can’t understand why this error appears and how I can mitigate/prevent it. Thanks for any help!

The error you’re receiving is that the data[2] index for the array you’re accessing doesn’t exist. If you’re building this list dynamically you need to ensure that the indexes you are attempting to access are always created prior to the access attempt.

okay, that was clear to me. I’ didn’t modify the protocol2_packet_handler.
It’s like sometimes the parameter of the position is not 4 bytes anymore but only 2 or less, then of course the later functions do not work with another byte size. How can I prevent this random change?
By the way, I encounter the same problem with the current read ( 2 byte size randomly becomes smaller)

It might be helpful if you were able to share the section of your program that’s throwing these errors, to see if there might be anything in the function calls that might be related to the issue.

i’m using the standard call funtion to read the motor current:
dxl_present_current, dxl_comm_result, dxl_error = dxl_connection.packetHandler.read2ByteTxRx(dxl_connection.portHandler, dxl_value[“MOTOR_ID”], dxl_value[“pres_current”])
This line of code is inside a loop that runs indefinitely till a button is pressed.
Like I said, the program works fine but sometime after few second or more minutes encounter the problem already described.
Here the screenshot of the terminal (the python program was compiled with auto-py-to-exe)
Screenshot 2023-12-24 165840