# URScipt + robot code

**URL:** <https://forum.universal-robots.com/t/urscipt-robot-code/8598>\
**Category:** URScript\
**Created:** [August 27, 2020, 1:01pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598 "2020-08-27T13:01:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [August 27, 2020, 1:01pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/1 "2020-08-27T13:01:59Z")

</div>

I have written while loop for the robot via a teaching pendant that checks all the time if the digital\_out[0] is True or False and then depending on the outcome, performs some action.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/8/884349f0722af486a03da2fd9e3479f17f1f639d.png)  
I also have my PC connected to the robot and I communicate via port 30003 where I send a command to set the digital out[0] to either True or False.  
The issue I am having is that once I run the code using python and the communication via socket, the code that was defined via a teaching pendant doesn’t run.

Is it possible to run two of those in parallel?

---

<div class="post-metadata">

**Author:** ![Ebbe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ebbe/32/1413_2.png) [@Ebbe](https://forum.universal-robots.com/u/Ebbe)\
**Post date:** [August 27, 2020, 3:24pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/3 "2020-08-27T15:24:50Z")

</div>

You can make a secondary program [Secondary program - 17257](https://www.universal-robots.com/articles/ur/secondary-program/) and send it on the secondary interface on port 30002

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [August 27, 2020, 7:32pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/4 "2020-08-27T19:32:54Z")

</div>

Thank you, @Ebbe  
However, I am looking for an option of running a program that was coded on the robot, and then to change a variable defined in that code using an external URScript which is sent to robot via port 30003.  
Do you know if I can run both the URScript and the already programmed program at the same time ?

---

<div class="post-metadata">

**Author:** ![anon73882248](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@anon73882248](https://forum.universal-robots.com/u/anon73882248)\
**Post date:** [August 28, 2020, 8:06am UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/5 "2020-08-28T08:06:36Z")

</div>

Yes. As long as the secondary code is simple enough. If you look up the script manual section 1.10 it describes how to run secondary programs, the limitations, and requirements they must meet.

If you only need to change an output then the following example from the same section will do:

```
sec secondaryProgram():
  set_digital_out(1,True)
end

```

EDIT: I don’t think you can change a variable in the other script but you can use the built-in registers as a variable and set that with a secondary program.

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [August 28, 2020, 9:44am UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/6 "2020-08-28T09:44:34Z")

</div>

@anon73882248 Thank you for your reply, will try to do it

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [August 31, 2020, 1:05pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/7 "2020-08-31T13:05:23Z")

</div>

Whenever I run the program on the teaching pendant, and I also run a second program from PC, the program that was running on the teaching pedant stops running and only the PC Script is run.  
Do you know how can I run the script from PC so it wont stop the script from teaching pendant?

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [September 1, 2020, 1:35am UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/8 "2020-09-01T01:35:32Z")

</div>

Did you change your port from 30003 to 30002? This will only work on secondary client interface at port 30002.

Are you specifically looking to control a digital output, or just any straightforward way to communicate a variable change to a running program?

If it’s the latter I’d look into using the general purpose RTDE registers instead of secondary programs:

[https://www.universal-robots.com/articles/ur/real-time-data-exchange-rtde-guide/](https://www.universal-robots.com/articles/ur/real-time-data-exchange-rtde-guide/)

It includes sample python code at the bottom of the article and you can name the registers in the I/O setup menu in Polyscope to use them directly in your program.

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [September 1, 2020, 11:52am UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/9 "2020-09-01T11:52:03Z")

</div>

@ajp  
I have a script where commands are executed in real-time (RTDE) via my PC . Because I am using an OnRobot gripper and I am unable to operate it from the PC script (there have been some issues with OnRobot communication), I thought of writting a program on the Polyscope that loops all the time, and opens/ closes the gripper when specific variable is True/False.  
The issue of mine is that when I first run the program on the polyscope and then run a second program using my PC, when the connection between PC and robot is established the initial program that was created on polyscope is terminated and only the connection via PC is taken into account.

Thus I am looking to communicate a variable change to a running program which I know how to do. The issue again is the fact that when I do change the variable in the running program, that should trigger an action that was defined on the plyscope, but that doesnt happen,

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [September 2, 2020, 1:21am UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/10 "2020-09-02T01:21:37Z")

</div>

I don’t think you can directly modify variables defined in the main program from a secondary program, they don’t exist within the same scope. I’d still use the RTDE registers here, if you want to do it with a secondary program then you could use write\_output\_boolean\_register() in there instead.

The above wouldn’t explain why your main program is being terminated though. Can you post exactly what you’re sending? Secondary program is not allowed to use any physical time, so no waits, syncs, moves, anything like that. Just a set/write command.

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [September 2, 2020, 12:42pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/11 "2020-09-02T12:42:34Z")

</div>

Here is what I am doing, in this example I communicate via port 30003 but its same idea via 30004 (RTDE)

Step 1 (define the program in polyscope)

 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/2/26c58d45550b506fcf05127ed68331158e0b6427.jpeg)  
Step 2 (Run the program in polyscope and test if gripper opens and closes when I change the input-output via polyscope)  
It does 🙂  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/a/aadded64262ba2351f123fa1a7489bb2ff92e962.jpeg)  
Step 3 (Switch from local control to remote, and use the below attached code to send command to change the digital\_out[0])  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/e/eee7cd5ca1a04329b913d40fa00cfb32f62d8f50.png)

So when the script is sent from my PC to the robot, the I/O is changed which I can see on the screen in a similar manner as in step 2) howeveer because the polyscope code is not running, the gripper doesnt open/close

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [September 2, 2020, 1:18pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/12 "2020-09-02T13:18:35Z")

</div>

If you just send the set command the controller will treat it as a primary program and terminate the existing primary program. You need to wrap it in the secondary program definition. Easy way to do this for a multiline script function is with triple quotes:

secprog="""  
sec secondaryProgram():  
set\_standard\_digital\_out(1, True)  
end  
“”"  
s.send(secprog)

---

<div class="post-metadata">

**Author:** ![anon59108369](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@anon59108369](https://forum.universal-robots.com/u/anon59108369)\
**Post date:** [September 2, 2020, 1:54pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/13 "2020-09-02T13:54:59Z")

</div>

@ajp  
Great, thank you very much it works 🙂

Just out of curiosity, do you know if there is a way to reverse the situation ie. instead of polyscope program to be a primary program and the python script to run as a secondary, how could I run the python script as primary and the polsycope as a secondary?

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [September 2, 2020, 11:20pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/14 "2020-09-02T23:20:22Z")

</div>

You can’t get the polyscope program to run as secondary, but you could send them both from python, primary first then secondary if that’s of any help?

---

<div class="post-metadata">

**Author:** ![anon17717985](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@anon17717985](https://forum.universal-robots.com/u/anon17717985)\
**Post date:** [October 22, 2020, 12:30pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/15 "2020-10-22T12:30:38Z")

</div>

thanks for haring the information  
[vmware](https://www.fieldengineer.com/skills/vmware-engineer)

---

<div class="post-metadata">

**Author:** ![anon73720875](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@anon73720875](https://forum.universal-robots.com/u/anon73720875)\
**Post date:** [June 20, 2022, 5:00pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/17 "2022-06-20T17:00:01Z")

</div>

Hello UR Technical Support,  
How can I get the running status back from the UR robot while running the script after sending the .script file over the Primary or secondary port (30001 or 30002)? OR how to send the .script file on RTDE Port (30004) because I can able to get the status back while the robot is running on 30004?

---

<div class="post-metadata">

**Author:** ![ajp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ajp/32/471_2.png) [@ajp](https://forum.universal-robots.com/u/ajp)\
**Post date:** [June 22, 2022, 3:50pm UTC](https://forum.universal-robots.com/t/urscipt-robot-code/8598/19 "2022-06-22T15:50:00Z")

</div>

Hi @anon73720875,

This library is a nice easy way to get started interfacing with the robot if you haven’t done it before:

[https://sdurobotics.gitlab.io/ur\_rtde/introduction/introduction.html](https://sdurobotics.gitlab.io/ur_rtde/introduction/introduction.html)
