{"id":2659,"date":"2021-08-22T05:54:56","date_gmt":"2021-08-22T05:54:56","guid":{"rendered":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/?page_id=2659"},"modified":"2023-01-21T05:45:44","modified_gmt":"2023-01-21T05:45:44","slug":"ur-script-send-commands-from-host-pc-to-robot-via-socket-connection","status":"publish","type":"page","link":"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\/","title":{"rendered":"UR Script: Send Commands from host (PC) to robot via socket connection."},"content":{"rendered":"\n<p><a title=\"Visit Zacobria &amp; Webshop &amp; Universal-Robots solutions.\" href=\"https:\/\/www.zacobria.com\/automation\/webshop\/\">Visit Zacobria Webshop<\/a><\/p>\n\n\n\n<p><strong>UR Script: Send Commands from host (PC) to robot via socket connection.<\/strong><\/p>\n\n\n\n<p>The example &#8220;UR Script: Script programming from the teaching pendant.&#8221;on CB2 forum used the \u201cScript\u201d function available in the teaching pendant to type Script code or import entire files with script code program into the robot and run from the teaching pendant.<br><a href=\"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/ur-script-script-programming-from-the-teaching-pendant\/\">UR Script: Script programming from the teaching pendant.<\/a><\/p>\n\n\n\n<p>Note: Variable will not work when sending raw script commands because variables (and other programming technique like loop \u2013 If-Else etc) needs to be controlled either by the external host program (e.g. a Python program) \u2013 or within a Polyscope program. Such variable cannot be left in between a host program and a Polyscope program \u2013 there is no controller in between.<\/p>\n\n\n\n<p>To send variables &#8211; make loops &#8211; use If-Else and to get informations back from the robot then the client-server method can be considered.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/universal-robots-script-client-server-example\/\">UR Script: Client-Server example.<\/a><\/p>\n\n\n\n<p><strong>Application Description:<\/strong><br>This example focus on making a script that entirely control the robot remotely from a host computer by Script programming.<\/p>\n\n\n\n<p>The format is the same as in previous chapter and as documented in the Script manual, but the teaching pendant is not used for programming in this example. On the robot a server on TCP port 30002 is running to receive script commands and thereby control the robot.<\/p>\n\n\n\n<p>Python\u00ae is used as the scripting language for making these program for testing the script programming with the robot. The Script file can also still be edited in a Notepad.<\/p>\n\n\n\n<p>With this method the data flow is sort of &#8220;one way&#8221; i.e. it is possible to send commands to the robot, but it is not meant to read data back i.e. inputs or other read data cannot be send back to the host with this method. To send back data see the &#8220;Client-Server&#8221; example.<br><strong>Function description:<\/strong><br><strong>1. Script program for Socket connection to test the connection:<\/strong><\/p>\n\n\n\n<p>The first program example is just to test the connection and illustrate how to send a script file through via the Ethernet to the TCP port 30002 on the robot.<\/p>\n\n\n\n<p>In this case the robot has already been configured to IP address 192.168.0.9 and the host must be in the same subnet e.g. in this example defined as 255.255.255.0<\/p>\n\n\n\n<p><strong>Programm description:<\/strong><\/p>\n\n\n\n<p>The first Program 1 just turns digital output number 2 ON and the robot does not move at all and the program looks like this.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-1.jpg\" alt=\"universal-robots-script-from-host-program-basic-1\" class=\"wp-image-1054\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"># Echo client program\nimport socket\nHOST = \"192.168.0.9\" # The remote host\nPORT = 30002 # The same port as used by the server<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.connect((HOST, PORT))<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"set_digital_out(2,True)\" + \"\\n\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">data = s.recv(1024)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.close()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print (\"Received\", repr(data))<\/pre>\n\n\n\n<p>When the program is run the robot sends back status data which is normal.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-result-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-result-1.jpg\" alt=\"universal-robots-script-from-host-program-basic-result-1\" class=\"wp-image-1055\"\/><\/a><\/figure>\n\n\n\n<p>The program is run from the host computer and will only have one passage because there is no loop in the program.<\/p>\n\n\n\n<p>The library \u201csocket\u201d in imported into the program so the Socket connection is available. The \u201cHost\u201d in this case is the Robot and \u201cPORT\u201d is the open port on the Robot that listens for Script code.<\/p>\n\n\n\n<p>Then the Script code necessary to connect and send on the Socket. The \u201cs.send\u201d command send that actual script code for the robot to execute which in the case is (\u201cset_digital_out(2,True)\u201d Which is like previous chapter. The + \u201c\\n\u201d) at the end of the line is a linefeed because the Robot need a linefeed after each command. The Socket connection needs to be closed again with the s.close() command. The print (\u201cReceived\u201d, repr(data)) command prints the output from the robot on the Host or PC monitor \u2013 in this case status data.<\/p>\n\n\n\n<p>The second Program 2 is similar and just turn digital out number 2 OFF again and the robot does not move at all.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-2.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-2.jpg\" alt=\"universal-robots-script-from-host-program-basic-2\" class=\"wp-image-1056\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"># Echo client program\nimport socket\nHOST = \"192.168.0.9\" # The remote host\nPORT = 30002 # The same port as used by the server<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.connect((HOST, PORT))<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"set_digital_out(2,False)\" + \"\\n\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">data = s.recv(1024)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.close()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print (\"Received\", repr(data))<\/pre>\n\n\n\n<p>When the program is run the robot sends back status data which is normal.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-result-2.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-basic-result-2.jpg\" alt=\"universal-robots-script-from-host-program-basic-result-2\" class=\"wp-image-1057\"\/><\/a><\/figure>\n\n\n\n<p><strong>2. Script program for Socket connection that makes the robot move:<\/strong><\/p>\n\n\n\n<p>This program send in Script mode via socket port runs through 6 Waypoints.<\/p>\n\n\n\n<p>The first 3 positions are defined by joint position data.<\/p>\n\n\n\n<p>The next 3 positions are defined by a pose.<br>Notice the &#8220;p&#8221; in front of the data wich are represented as X, Y, Z, Rx, Ry, Rz.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-1.jpg\" alt=\"universal-robots-script-from-host-program-1\" class=\"wp-image-1063\"\/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-2.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-program-2.jpg\" alt=\"universal-robots-script-from-host-program-2\" class=\"wp-image-1064\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"># Echo client program\nimport socket\nimport time<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">HOST = \"192.168.0.9\" # The remote host\nPORT = 30002 # The same port as used by the server<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Starting Program\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">count = 0\nwhile (count &lt; 1):\n s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n s.connect((HOST, PORT))\n time.sleep(0.5)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Set output 1 and 2 high\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"set_digital_out(1,True)\" + \"\\n\")\n time.sleep(0.1)\n\n s.send (\"set_digital_out(2,True)\" + \"\\n\")\n time.sleep(2)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Robot starts Moving to 3 positions based on joint positions\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej([-1.95, -1.58, 1.16, -1.15, -1.55, 1.18], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej([-1.95, -1.66, 1.71, -1.62, -1.56, 1.19], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej([-1.96, -1.53, 2.08, -2.12, -1.56, 1.19], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Robot starts Moving to 3 positions based on pose positions\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej(p[0.00, 0.3, 0.4, 2.22, -2.22, 0.00], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej(p[0.00, 0.3, 0.3, 2.22, -2.22, 0.00], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"movej(p[0.00, 0.3, 0.2, 2.22, -2.22, 0.00], a=1.0, v=0.1)\" + \"\\n\")\n time.sleep(10)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Set output 1 and 2 low\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.send (\"set_digital_out(1,False)\" + \"\\n\")<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">time.sleep(0.1)\n\n s.send (\"set_digital_out(2,False)\" + \"\\n\")\n time.sleep(0.1)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">count = count + 1\n print \"The count is:\", count<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Program finish\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">time.sleep(1)\n data = s.recv(1024)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">s.close()\n print (\"Received\", repr(data))<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">print \"Status data received from robot\"<\/pre>\n\n\n\n<p><strong>Result feedback from robot when the program is executed<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-output-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/01\/universal-robots-script-from-host-output-1-1024x407.jpg\" alt=\"universal-robots-script-from-host-output-1\" class=\"wp-image-1066\"\/><\/a><\/figure>\n\n\n\n<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" method=\"post\" target=\"_top\"><input name=\"cmd\" type=\"hidden\" value=\"_s-xclick\"><br><input name=\"hosted_button_id\" type=\"hidden\" value=\"5UM6A44W2P6PN\">\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><input name=\"on0\" type=\"hidden\" value=\"Contribute moderator\">Contribute moderator<\/td>\n<\/tr>\n<tr>\n<td><select name=\"os0\">\n<option value=\"Option 1\">Option 1 $50.00 SGD<\/option>\n<option value=\"Option 2\">Option 2 $100.00 SGD<\/option>\n<option value=\"Option 3\">Option 3 $500.00 SGD<\/option>\n<\/select><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><input name=\"currency_code\" type=\"hidden\" value=\"SGD\"><br><input alt=\"PayPal \u2013 The safer, easier way to pay online!\" name=\"submit\" src=\"https:\/\/www.paypalobjects.com\/en_GB\/SG\/i\/btn\/btn_buynowCC_LG.gif\" type=\"image\"><br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.paypalobjects.com\/en_GB\/i\/scr\/pixel.gif\" alt=\"\" width=\"1\" height=\"1\" border=\"0\"><\/p>\n<\/form>\n\n\n\n<p><strong>Disclaimer:<\/strong> While the Zacobria Pte. Ltd. believes that information and guidance provided is correct, parties must rely upon their skill and judgement when making use of them. Zacobria Pte. Ltd. assumes no liability for loss or damage caused by error or omission, whether such an error or omission is the result of negligence or any other cause. Where reference is made to legislation it is not to be considered as legal advice. Any and all such liability is disclaimed.<\/p>\n\n\n\n<p>If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.<\/p>\n\n\n\n<p>Author:<br><a href=\"https:\/\/plus.google.com\/u\/0\/116832821661215606670?rel=author\">By Zacobria Lars Skovsgaard<\/a><br>Accredited 2015-2018 Universal Robots support Centre and Forum.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/share\">Tweet<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/zacobria\">Follow @zacobria<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visit Zacobria Webshop UR Script: Send Commands from host (PC) to robot via socket connection. The example &#8220;UR Script: Script programming from the teaching pendant.&#8221;on CB2 forum used the \u201cScript\u201d function available in the teaching pendant to type Script code&#8230; <\/p>\n<div class=\"more-link-container\"><a class=\"more-link\" href=\"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\/\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":2481,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"templates\/full-width-page.php","meta":{"footnotes":""},"class_list":["post-2659","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/comments?post=2659"}],"version-history":[{"count":6,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2659\/revisions"}],"predecessor-version":[{"id":3136,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2659\/revisions\/3136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media\/2481"}],"wp:attachment":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media?parent=2659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}