{"id":2579,"date":"2021-08-22T03:40:46","date_gmt":"2021-08-22T03:40:46","guid":{"rendered":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/?page_id=2579"},"modified":"2023-01-21T05:27:51","modified_gmt":"2023-01-21T05:27:51","slug":"modbus-internal-registers-read-and-write","status":"publish","type":"page","link":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/modbus-internal-registers-read-and-write\/","title":{"rendered":"MODBUS internal registers read and write."},"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>MODBUS internal registers read and write.<\/strong><\/p>\n\n\n\n<p><strong>Application Description:<\/strong><\/p>\n\n\n\n<p>This example shows the use of internal MODBUS registers on the Universal-Robots.<\/p>\n\n\n\n<p>For also using external MODBUS registers see this link <a href=\"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/modbus-registers-and-nodes\/\">UR Modbus internal and external registers<\/a><\/p>\n\n\n\n<p>The application uses the MODBUS server running at port 502 on the Universal-Robots. The MODBUS server has a range of register available with dedicated content and register 128 to 255 is for general purpose use.<\/p>\n\n\n\n<p>A list of the MODBUS and register can be found at this link&nbsp;<a href=\"http:\/\/www.universal-robots.com\/how-tos-and-faqs\/how-to\/ur-how-tos\/modbus-server-16377\/\">UR Modbus page<\/a><\/p>\n\n\n\n<p>The commands are send from a external host (PC) with Python code.<\/p>\n\n\n\n<p><strong>Function description:<\/strong><br>The Polyscope program on the UR reads the internal MODBUS register 128. And the Polyscope program toggles the register 129 True and False with 1 second interval. (True and False is set as 1 or 0).<\/p>\n\n\n\n<p><strong>I\/O table Inputs:<\/strong><br>N\/A<\/p>\n\n\n\n<p><strong>Variable Table:<\/strong><br>var_1 = Value of register 128 (set by remote host (PC))<\/p>\n\n\n\n<p><strong>Electrical diagrams.<\/strong><br>N\/A<\/p>\n\n\n\n<p><strong>Program description:<\/strong><br>As this program is for testing the MODBUS interface the robot program has only one waypoint.<\/p>\n\n\n\n<p>The value of the internal MODBUS register 128 is read into variable var_1.<\/p>\n\n\n\n<p>Then register 129 is toggled between the value 01 and value 00 with a 1 sec interval. (True and False).<\/p>\n\n\n\n<p><strong>Program code:<\/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\/2017\/01\/ur-modbus-internal-register-read-write-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-1.jpg\" alt=\"ur-modbus-internal-register-read-write-1\" class=\"wp-image-1973\"\/><\/a><\/figure>\n\n\n\n<p><strong>Host program in Python code.<\/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\/2017\/01\/ur-modbus-internal-register-read-write-21.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-21.jpg\" alt=\"ur-modbus-internal-register-read-write-2\" class=\"wp-image-1977\"\/><\/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\/2017\/01\/ur-modbus-internal-register-read-write-3.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-3.jpg\" alt=\"ur-modbus-internal-register-read-write-3\" class=\"wp-image-1975\"\/><\/a><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"># Echo client program\nimport socket\nimport time\n\nHOST = \"192.168.0.9\" # The remote host\nPORT_502 = 502\n\nprint \"Starting Program\"\n\ncount = 0\nhome_status = 0\nprogram_run = 0\n\nwhile (True):\n if 1 == 1:\n if program_run == 0:\n try:\n s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n s.settimeout(10)\n s.connect((HOST, PORT_502))\n time.sleep(0.05)\n print \"\"\n print \"Modbus set reg 128 to 01\"\n print \"Sending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x80 x00 x01\"\n s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x80\\x00\\x01\") #sets register 128 to 1\n time.sleep(1.00)\n print \"\"\n print \"Modbus set reg 128 to 00\"\n print \"Sending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x80 x00 x00\"\n s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x80\\x00\\x00\") #sets register 128 to 0\n time.sleep(1.00)\n s.close() #clear buffer\n s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n s.settimeout(10)\n s.connect((HOST, PORT_502))\n time.sleep(0.05)\n print \"\"\n print \"Request reg 129\"\n print \"Sending: x00 x04 x00 x00 x00 x06 x00 x03 x00 x81 x00 x01\"\n msg = \"\"\n s.send (\"\\x00\\x04\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x81\\x00\\x01\") #request data from register 128-133 (cartisian data)\n print \"Modbus command send to read reg 129\"\n time.sleep(0.75)\n print \"Received\"\n msg = s.recv(1024)\n print repr(msg) #Print the receive data in \\x hex notation (notice that data above 32 hex will berepresented at the ascii code e.g. 50 will show P\n print \"\"\n msg = msg.encode(\"hex\") #convert the data from \\x hex notation to plain hex\n print msg\n print \"\"\n home_status = 1\n program_run = 0\n s.close()\n except socket.error as socketerror:\n print(\"Error: \", socketerror)\nprint \"Program finish\"\n\n<\/pre>\n\n\n\n<p><strong>Program run:<\/strong><br>The var_1 is getting the value of True and False according to the settings of register 128 (which is set by the external Python program (PC)).<\/p>\n\n\n\n<p>This can be verified in the &#8220;Variables&#8221; tab when the Polyscope program in running.<\/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\/2017\/01\/ur-modbus-internal-register-read-write-4.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-4.jpg\" alt=\"ur-modbus-internal-register-read-write-4\" class=\"wp-image-1978\"\/><\/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\/2017\/01\/ur-modbus-internal-register-read-write-5.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-5.jpg\" alt=\"ur-modbus-internal-register-read-write-5\" class=\"wp-image-1979\"\/><\/a><\/figure>\n\n\n\n<p>At the same time time data received on the external host (Python program) also can be verified.<\/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\/2017\/01\/ur-modbus-internal-register-read-write-6.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2017\/01\/ur-modbus-internal-register-read-write-6.jpg\" alt=\"ur-modbus-internal-register-read-write-6\" class=\"wp-image-1980\"\/><\/a><\/figure>\n\n\n\n<p><strong>Notes:<\/strong><br>Data exchange using the Python program:<\/p>\n\n\n\n<p><strong><u>Modbus Data send to <\/u><\/strong><strong><u>UR<\/u><\/strong><strong><u> to write UR register.<\/u><\/strong><\/p>\n\n\n\n<p><strong><u>First request to write.<\/u><\/strong><\/p>\n\n\n\n<p><strong>Data send<\/strong><br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x80\\x00\\x01<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 06 00 80 00 01<\/p>\n\n\n\n<p><strong>Meaning:<\/strong><br>00 01 is the sequence number Modbus TCP follows (Sequence no = Transaction identifier (serial no)<br>00 00 is protocol indentifier<br>00 06 messages length &#8211; means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>06 is the function code for write<br>00 80 The address of the first register to set &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 80hex = 128 dec).<br>00 01 The value to set into register 128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (set value to 1)<\/p>\n\n\n\n<p><strong><u>Second request to write.<\/u><\/strong><\/p>\n\n\n\n<p><strong>Data send.<\/strong><br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x80\\x00\\x00<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 06 00 80 00 00<\/p>\n\n\n\n<p><strong>Meaning:<\/strong><br>00 01 is the sequence number Modbus TCP follows (Sequence no = Transaction identifier (serial no)<br>00 00 is protocol indentifier<br>00 06 messages length &#8211; means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>06 is the function code for write<br>00 80 The address of the first register to set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 80hex = 128 dec).<br>00 00 The value to set into register 128&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (set value to 0)<\/p>\n\n\n\n<p><strong><u>Modbus Data send to UR to read UR register.<\/u><\/strong><\/p>\n\n\n\n<p>\\x00\\x04\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x81\\x00\\x01<\/p>\n\n\n\n<p>Which is the same as<br>00 04 00 00 00 06 00 03 00 81 00 01<\/p>\n\n\n\n<p><strong>Meaning:<\/strong><br>00 04 is the sequence number Modbus TCP follows (Sequence no = Transaction identifier (serial no)<br>00 00 is protocol indentifier<br>00 06 messages length &#8211; means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>03 is the function code for read<br>00 81 The data address of the first register requested&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 81hex = 129 dec).<br>00 01 The total number of register requested<\/p>\n\n\n\n<p><strong>Data received from UR<\/strong><\/p>\n\n\n\n<p><strong><u>First poll received:<\/u><\/strong><\/p>\n\n\n\n<p>0004000000050003020000<\/p>\n\n\n\n<p><strong>Meaning:<\/strong><br>00 04 is the sequence number that Modbus TCP follow (same as in the request send i.e. replied to)<br>00 00 Protocol identifier<br>00 05 messages length &#8211; means 5 bytes will follow<br>03 was the function code requested<br>02 registeres are read<br>00 00 is data value received.<\/p>\n\n\n\n<p><strong><u>Another pool received.<\/u><\/strong><\/p>\n\n\n\n<p>0004000000050003020001<\/p>\n\n\n\n<p><strong>Meaning:<\/strong><br>00 04 is the sequence number that Modbus TCP follow (same as in the request send i.e. replied to)<br>00 00 Protocol identifier<br>00 05 messages length &#8211; means 5 bytes will follow<br>03 was the function code requested<br>02 registeres are read<br>00 01 is data value received.<\/p>\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.<br><br><br><\/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 MODBUS internal registers read and write. Application Description: This example shows the use of internal MODBUS registers on the Universal-Robots. For also using external MODBUS registers see this link UR Modbus internal and external registers The application&#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\/modbus-internal-registers-read-and-write\/\">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-2579","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\/2579","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=2579"}],"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\/2579\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2579\/revisions\/3098"}],"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=2579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}