Sending URScript to Robot via Python

I have created a URScript file via Python but I am having a hard time figuring out how to send the entire script file through the Primary Interface (Port: 30001). I have successfully used the Dashboard Interface and custom Ports in the past but can’t seem to figure out the format in which to send a .script file.

Here is the Python code for sending the file.

def RobotInterface(command: str):
    # initialize variables
    robotIP = "172.28.XXX.XXX"
    PORT = 30001

    # create socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((robotIP, PORT))  # connect to UR port 30001
    s.send(command.encode())    # encode data and send through socket
    s.close()                   # close the socket
    print("Dashboard Command Complete")

Where the input to this command is:

    with open(scriptFile) as f:
        lines = f.read()
    URF.RobotInterface(lines)

When this code is ran, I receive the “Dashboard Command Complete” statement which means that the socket was opened and something was sent (correct?). However, the robot doesn’t move or throw an error, it does nothing.

I have manually loaded the scriptFile to my UR and ran it from PolyScope and it performs as expected. I think the issue is how I am trying to send the file from Python. How do you send a .script file? Does it need to be condensed into one single variable consisting of \n markers? Is it possible to send the whole file in one command?

Structure of the scriptFile :

def moves():
  movej(get_inverse_kin(...))
  movej(get_inverse_kin(...))
  movej(get_inverse_kin(...))
  movej(get_inverse_kin(...))
end

More Information

If I manually create the variable lines and enter it into my function then the robot makes the proper move. Example:

lines = "movej(get_inverse_kin(...))\n"
RobotInterface(lines)

However, if I change my looping function to read one line at a time and try to send that line to the robot nothing happens. Example:

    with open(scriptFile) as f:
        for line in f:
            URF.RobotInterface(line)

It’s my understanding that this looping function is doing the same thing as manually setting the variable line to the movej function. I just don’t understand why it isn’t working.

I was able to develop a workaround solution for this but would still like help figuring out how to send a full .script file via Python and the UR Primary Interface.

The workaround solution reads each line from the .script file and sends the line to the robot one at a time. The error I was having had something to do with the newline \n code at the end of the text file. The fix was to use .readline() and then append "//n" to the variable before sending to the robot.

    with open(scriptFile) as f:
        for lines in f:
            line = f.readline()
            print(line)
            URF.RobotInterface(line + " \\n")
            time.sleep(5)