# Define script function in python

**URL:** <https://forum.universal-robots.com/t/define-script-function-in-python/8872>\
**Category:** Technical Questions\
**Tags:** python\
**Created:** [September 21, 2020, 4:46am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872 "2020-09-21T04:46:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehteshyam.a](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ehteshyam.a](https://forum.universal-robots.com/u/ehteshyam.a)\
**Post date:** [September 21, 2020, 4:46am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/1 "2020-09-21T04:46:08Z")

</div>

I’m trying to define a URscript function in python using socket communication.  
I have used **s.send()** command to pass the function, although the command is executed in python terminal it is not reflected on the robot(UR5e) means that the robot is not executing the function which we have passed.  
So do we have to use **s.send** command to pass the function or is there some other way to do this and how we can write it to make the robot work according to the function.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)\
**Post date:** [September 21, 2020, 8:10am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/2 "2020-09-21T08:10:17Z")

</div>

Hi

Could you please tell us more in detail?

Is xmlrpc server running?  
I will give you advice if you publish python code.

---

<div class="post-metadata">

**Author:** ![ehteshyam.a](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ehteshyam.a](https://forum.universal-robots.com/u/ehteshyam.a)\
**Post date:** [September 21, 2020, 8:37am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/3 "2020-09-21T08:37:04Z")

</div>

Here is my python code and I’m using socket communication

```
        import socket
        import time
        HOST = "192.168.1.228" # The remote host
        PORT = 30003 # The same port as used by the server
        print "Starting Program"
        count = 0
        while (count<5):
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((HOST, PORT))
            time.sleep(0.1)
            def zero_ftsensor():
                zero_ftsensor()
            def force_mode(task_frame, selection_vector, wrench, type, limits):
                    task_frame= p[]
                    selection_vector= [0, 0, 1, 0, 0, 0]
                    wrench= [0.0, 0.0, 2.0, 0.0, 0.0, 0.0]
                    type= 2
                    limits= [0.1, 0.1, 0.01, 0.3490658503988659, 0.3490658503988659, 0.3490658503988659]
                    s.send("movej(get_inverse_kin(p[.327703289323, .476061350313, -.097627345841, -2.812739082151, 1.304660291291, -.023691609980], qnear=[1.2080140113830566, -2.283745904962057, -1.968592643737793, -0.42255671442065434, 1.551553726196289, 0.5063714981079102]), a=1.3962634015954636, v=1.0471975511965976" + "\n")
                    time.sleep(0.5)
                    s.send("movej(get_inverse_kin(p[.327714366290, .476029125399, -.118442542479, -2.812736183950, 1.304738140748, -.023707104201], qnear=[1.2080497741699219, -2.3317533932127894, -1.9631872177124023, -0.37998469293627934, 1.5515055656433105, 0.50634765625]), a=1.3962634015954636, v=1.0471975511965976" + "\n")
                    time.sleep(0.5)
            def end_force_mode():
                end_force_mode()

            s.send("movej(get_inverse_kin(p[.327730194161, .476086610312, -.097604445909, -2.812638420151, 1.304676383799, -.023700524115], qnear=[1.2079901695251465, -2.283733984033102, -1.9685449600219727, -0.422532395725586, 1.551541805267334, 0.5063834190368652]), a=1.3962634015954636, v=1.0471975511965976"+ "\n")
            time.sleep(0.5)
            s.send("movej(get_inverse_kin(p[.303899571901, .476060424078, -.097640923256, -2.812774988664, 1.304630843166, -.023677841810], qnear=[1.2490949630737305, -2.268569131890768, -2.007960319519043, -0.39916451395068364, 1.5498871803283691, 0.547391414642334]), a=1.3962634015954636, v=1.0471975511965976)"+ "\n")
            time.sleep(0.5)
            def zero_ftsensor():
                s.send("force_mode(tool_pose(), [0, 0, 1, 0, 0, 0], [0.0, 0.0, 2.0, 0.0, 0.0, 0.0], 2, [0.1, 0.1, 0.01, 0.3490658503988659, 0.3490658503988659, 0.3490658503988659])")
                time.sleep(0.5)
                s.send("movej(get_inverse_kin(p[.303899571901, .476060424078, -.097640923256, -2.812774988664, 1.304630843166, -.023677841810], qnear=[1.2490949630737305, -2.268569131890768, -2.007960319519043, -0.39916451395068364, 1.5498871803283691, 0.547391414642334]), a=1.3962634015954636, v=1.0471975511965976)" + "\n")
                time.sleep(0.5)
                s.send("movej(get_inverse_kin(p[.303932640600, .476072954596, -.123883177495, -2.812715881976, 1.304681068896, -.023687024763], qnear=[1.249143123626709, -2.3310352764525355, -2.000452995300293, -0.34417279184375005, 1.5498275756835938, 0.5473432540893555]), a=1.3962634015954636, v=1.0471975511965976)" + "\n")

            def end_force_mode():
                s.send("end_force_mode()")
            time.sleep(5)
            s.send(" movej(get_inverse_kin(p[.303899571901, .476060424078, -.097640923256, -2.812774988664, 1.304630843166, -.023677841810], qnear=[1.2490949630737305, -2.268569131890768, -2.007960319519043, -0.39916451395068364, 1.5498871803283691, 0.547391414642334]), a=1.3962634015954636, v=1.0471975511965976)" + "\n")
            time.sleep(0.5)
            s.send(" movej(get_inverse_kin(p[.303932640600, .476072954596, -.123883177495, -2.812715881976, 1.304681068896, -.023687024763], qnear=[1.249143123626709, -2.3310352764525355, -2.000452995300293, -0.34417279184375005, 1.5498275756835938, 0.5473432540893555]), a=1.3962634015954636, v=1.0471975511965976)" + "\n")

        count = count + 1
        print "The count is:", count
        print "Program finish"
        data = s.recv(1024)
        s.close()
        print ("Received", repr(data))
    print "Status data received from robot"
```

---

<div class="post-metadata">

**Author:** ![moliveira](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@moliveira](https://forum.universal-robots.com/u/moliveira)\
**Post date:** [September 21, 2020, 1:05pm UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/4 "2020-09-21T13:05:35Z")

</div>

To send a socket command from python :

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/9/9452c675417a95b121a1cd2a8c3bbce9edf8c06d.png)

---

<div class="post-metadata">

**Author:** ![ehteshyam.a](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ehteshyam.a](https://forum.universal-robots.com/u/ehteshyam.a)\
**Post date:** [September 22, 2020, 4:42am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/5 "2020-09-22T04:42:46Z")

</div>

How can I define a URscript function in like for example in URscript there is **force\_mode(**) function how can I deploy it in python.  
Do I have to use **s.send(force\_mode())** to define it?

---

<div class="post-metadata">

**Author:** ![moliveira](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@moliveira](https://forum.universal-robots.com/u/moliveira)\
**Post date:** [September 22, 2020, 12:44pm UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/6 "2020-09-22T12:44:24Z")

</div>

myfunction = “def myFunction():\n”+" while (1):"+"\n"+" sync()"+"\n"+" end"+"\n"+"\nend\n"

s = socket.socket(socket.AF\_INET, socket.SOCK\_STREAM)  
s.connect((HOST, SocketPORT))  
s.sendall(myFunction.encode())  
s.close()

This is a generic form, but you can easily modify to fit your application

Be careful with spaces using this method. just like python, the lines have to match

---

<div class="post-metadata">

**Author:** ![ehteshyam.a](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@ehteshyam.a](https://forum.universal-robots.com/u/ehteshyam.a)\
**Post date:** [September 23, 2020, 4:55am UTC](https://forum.universal-robots.com/t/define-script-function-in-python/8872/7 "2020-09-23T04:55:28Z")

</div>

Thanks a lot for your response
