Sending looped code over port 30002

I’m attempting to send programs to a UR5 CB2 robot. My goal is to update registers via Modbus and have the CB2 follow those setpoints continuously via servoj calls. I am currently able to send a singular servoj command as such:
servoj(get_inverse_kin(p[read_port_register(128)*0.1,0.3,0.3,0,0,0]), 1, 1, 1)
However, when I attempt to loop this code in any manner, it fails to execute the loop and only runs the internal servo command once. I am attempting to send the following script:
while (True):
servoj(get_inverse_kin(p[read_port_register(128)*0.1,0.3,0.3,0,0,0]), 1, 1, 1)
end
How am I able to send a script that contains while loops? Would a recursive function solve this issue? Thanks!

I haven’t used a CB2 model, so can’t guarantee this suggestion will work.

If you send a function, then the robot will execute that function; perhaps you need to do that if you want to include flow-control like a loop. So try sending this script:

def follow_setpoints():
  while (True):
    servoj(get_inverse_kin(p[read_port_register(128)*0.1,0.3,0.3,0,0,0]), 1, 1, 1)
  end
end

I tried doing as such, but it doesn’t seem to perform the loop. Looking at the log, it attempts the method only once and the program ends after completing the servoj command