Universal Robots Forum

UR connection timeout when using urx python library

Hi, I’m right now using urx python library to send URscript to UR10 and utilizing the function " is_program_running( )" to check if the program has finished so that next program can then be sent to the robot.

However, the connection socket seems to be quite unstable and error message, “Did not receive a valid data packet from robot in 0.5 sec,” pops up frequently. The codes were as shown below:
The following part is the definition examples of URScript:

def ATC_1to2_part1():
    rob = urx.Robot("192.168.0.30")
    
    us = URScript()
    us.add_line_to_program('set_digital_out(2,False)')
    us.add_line_to_program('set_digital_out(1,True)')
    
    us.add_line_to_program('Frame_1 = p[0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000]')
    us.add_line_to_program('set_tcp(p[0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000])')

    us.add_line_to_program('movej(get_inverse_kin(p[.664885196535, -.155653687376, .623538831326, -1.208492756197, 1.204628918965, -1.195107552063], qnear=[0.0, -1.5707963267948966, -1.5707963267948966, -3.141592653589793, -1.5707963267948966, 3.141592653589793]), a=1.3962634015954636, v=0.5235987755982988)')
    us.add_line_to_program('movej(get_inverse_kin(p[.760000000000, -.141110700802, .758397012640, .005047062972, .005629129299, -1.577980881076], qnear=[0.0208220510140252, -1.6901489184342484, -1.5714205365207174, -1.4511245727634456, -1.5720380714670368, 3.102063269875302]), a=1.3962634015954636, v=0.3490658503988659)')
    us.add_line_to_program('movel(p[.970000000000, -.141098559362, .758500000000, .005113257770, .005378031794, -1.578026654070], a=1.2, v=0.08)')
    us.add_line_to_program('movel(p[1.073286839630, -.141178968076, .758500000000, .005132414795, .005334913325, -1.578085222502], a=1.2, v=0.015)')
    
    rob.send_program(us())
    
    rob.close()
    
def ATC_1to2_part2():
    rob = urx.Robot("192.168.0.30")
    
    us = URScript()
    
    us.add_line_to_program('movel(p[1.073200000000, -.141180000000, .730000000000, .005032117054, -.006778165754, -1.578054335413], a=1.2, v=0.03)')
    us.add_line_to_program('movel(p[1.073000000000, -.001500000000, .730000000000, .005124732799, -.006653452389, -1.578084697632], a=1.2, v=0.06)')
    us.add_line_to_program('movel(p[1.073000000000, -.001500000000, .759000000000, .005000000000, .000000000000, -1.578055049148], a=1.2, v=0.03)')
    
    rob.send_program(us())
    
    rob.close()

After sending the URScript to the robot, the following part will be executed to check whether the program is over or not:

rob = urx.Robot("192.168.0.30")
while rob.is_program_running():
    print("Program is still running")
    time.sleep(0.1)
rob.close()

Can anyone give me some advice to eliminate the timeout problem?
Thanks!