# Sending URScript to UR5 using Python socket

**URL:** <https://forum.universal-robots.com/t/sending-urscript-to-ur5-using-python-socket/26966>\
**Category:** Technical Questions\
**Created:** [February 22, 2023, 2:19pm UTC](https://forum.universal-robots.com/t/sending-urscript-to-ur5-using-python-socket/26966 "2023-02-22T14:19:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon18273086](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@anon18273086](https://forum.universal-robots.com/u/anon18273086)\
**Post date:** [February 22, 2023, 2:19pm UTC](https://forum.universal-robots.com/t/sending-urscript-to-ur5-using-python-socket/26966/1 "2023-02-22T14:19:41Z")

</div>

Hello,  
I am trying to send a URScript code to UR5 via Python socket and I have tried this method:

> **[UR Script: Send Commands from host (PC) to robot via socket connection. – UR...](https://www.zacobria.com/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3/index.php/ur-script-send-commands-from-host-pc-to-robot-via-socket-connection/)**

I need force\_mode to be activated during robot movement and I have tried sending one line at a time but then when the force\_mode command is sent, it is activated and deactivated before sending another line. Also, I have tried sending the entire URScript at once (force\_mode with moveL commands) but nothing happened. Robot did not move.

Does moveL work only when sending line by line? Is there a way to maintain force\_mode when the robot is moving through desired points?

---

<div class="post-metadata">

**Author:** ![cags](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/cags/32/6304_2.png) [@cags](https://forum.universal-robots.com/u/cags)\
**Post date:** [February 24, 2023, 10:16am UTC](https://forum.universal-robots.com/t/sending-urscript-to-ur5-using-python-socket/26966/2 "2023-02-24T10:16:01Z")

</div>

Something missing on that tutorial is that those commands are independent from each other, and it just executes them as 1-line programs. When you want to send a script that might affect the rest of the script or following commands, you have to send it as a function:

```auto
def myFun():
     # command_1 
     # command_2
     # ...
end

```

This way the robot will keep receiving all the commands until the last `end` is received, and executed them as a single program. Then the force\_mode will remain active until the end.

---

<div class="post-metadata">

**Author:** ![anon18273086](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@anon18273086](https://forum.universal-robots.com/u/anon18273086)\
**Post date:** [February 27, 2023, 10:31am UTC](https://forum.universal-robots.com/t/sending-urscript-to-ur5-using-python-socket/26966/3 "2023-02-27T10:31:41Z")

</div>

> [@cags](#):
>
> `def myFun():`

Thank you so much, here is the example that works.

Script code:

def myFun():  
while(True):  
force\_mode(tool\_pose(), [1, 1, 0, 0, 0, 0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 2, [0.2, 0.2, 0.1, 0.35, 0.35, 0.35])  
movej(get\_inverse\_kin(p[-0.06,-0.42,0.5,1.4,-0.6,0.3], qnear=[4.91,-1.42,-1.51,2.99,-1.15,0.57]),a=1.4,v=1.04)  
movej(get\_inverse\_kin(p[-0.06,-0.5,0.5,1.4,-0.6,0.3], qnear=[4.8,-1.7,-1.16,2.98,-1.21,0.57]),a=1.4,v=1.04)  
end  
end

myFun()

Python code:

#!/usr/bin/env python3  
import socket  
import time  
HOST=“192.168.22.14”  
PORT=30002  
print(“Starting program”)  
s=socket.socket(socket.AF\_INET,socket.SOCK\_STREAM)  
s.connect((HOST,PORT))  
time.sleep(1)  
f = open(“/home/lukrecia/catkin\_ws/src/ft300s/src/funkcija.txt”, “rb”)  
l = f.read(1024)  
print(l)  
s.sendall(l)  
time.sleep(5)  
print(“End program”)  
data=s.recv(1024)  
s.close()  
print(“Receved”,repr(data))
