{"id":2588,"date":"2021-08-22T03:49:54","date_gmt":"2021-08-22T03:49:54","guid":{"rendered":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/?page_id=2588"},"modified":"2023-01-21T05:28:21","modified_gmt":"2023-01-21T05:28:21","slug":"modbus-registers-input-and-output-handling-via-port-502","status":"publish","type":"page","link":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/modbus-registers-input-and-output-handling-via-port-502\/","title":{"rendered":"MODBUS registers Input and Output handling via port 502"},"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 registers Input and Output handling via port 502<\/strong><\/p>\n\n\n\n<p><strong>Application Description:<\/strong><br>This example shows the use of internal MODBUS Input-Output registers on the Universal-Robots.<\/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 are for general purpose use. In this example register 0 (Inputs) and register 1 (Outputs) will be used.<\/p>\n\n\n\n<p>A list of the MODBUS and register can be found at this link <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 with Python code.<\/p>\n\n\n\n<p><strong>Function description:<\/strong><br>The external host (PC) is connected to the UR via Ethernet and access the MODBUS registers on the UR on TCP port 502.<\/p>\n\n\n\n<p><strong>Program description:<\/strong><br>The external host first reads the physical hardware inputs and outputs on the UR and displays the results on the host screen.<br>Then the host sets all outputs on the UR to 0 and wait 1 second.<br>Then the host set output number 2 (bit 3 i.e. value 04 = 0100) to high.<\/p>\n\n\n\n<p><strong>Program code:<\/strong><br>There is no program on UR. Instead the inputs and outputs are set to a random value in order to test the result on the host.<\/p>\n\n\n\n<p>In this case Input 0, 1 and 7 is set to high. And out put 2 is set to high. Rest is set to zero.<\/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\/09\/universal-robots-zacobria-modbus-input-output-handling-1.jpg\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips\/wp-content\/uploads\/2016\/09\/universal-robots-zacobria-modbus-input-output-handling-1.jpg\" alt=\"universal-robots-zacobria-modbus-input-output-handling-1\" class=\"wp-image-1681\"\/><\/a><\/figure>\n\n\n\n<p><strong>Host program in Python code.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import socket\nimport time\nimport binascii\n\nHOST = \"192.168.0.9\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # The Robot IP address\nPORT_502 = 502&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # The MODBUS Port on the robot\n\nprint \"Starting Program\"\ncount = 0\nprogram_run = 0\n\nwhile (True):\n&nbsp;&nbsp;&nbsp; if 1 == 1:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if program_run == 0:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.settimeout(10)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.connect((HOST, PORT_502))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sleep(0.05)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Modbus Read inputs\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Sending: x00 x01 x00 x00 x00 x06 x00 x03 x00 x00 x00 x01\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x00\\x00\\x01\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = s.recv(1024)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Raw received data = \", msg\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Received data as x format = \", repr(msg)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Print the receive data in \\x hex format (notice that data above 32 hex will be represented by the ascii code e.g. 50 will show P\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = msg.encode(\"hex\") #Convert the data from \\x hex format to plain hex\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Hex data received = \", msg\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Strip downto last two bytes\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_in = msg[21]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_in = msg[20]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"byte_1_in = \", byte_1_in\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"byte_2_in = \", byte_2_in\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_bin_in = bin(int(byte_1_in, 16))&nbsp;&nbsp;&nbsp;&nbsp; #convert hex to bin\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Binary value of input byte 1 = \", byte_1_bin_in\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_bin_in = byte_1_bin_in[2:].zfill(4)&nbsp; #get rid of the 0b and fill with zeros so byte take 4 positions\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Remove 0b and make byte occupie 4 positions = \", byte_1_bin_in\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_bin_in = bin(int(byte_2_in, 16))&nbsp;&nbsp;&nbsp;&nbsp; #convert hex to bin\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Binary value of input byte 2 = \", byte_2_bin_in\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_bin_in = byte_2_bin_in[2:].zfill(4)&nbsp; #get rid of the 0b and fill with zeros so byte take 4 positions\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Remove 0b and make byte occupie 4 positions = \", byte_2_bin_in\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 0 = \", byte_1_bin_in[3]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 1 = \", byte_1_bin_in[2]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 2 = \", byte_1_bin_in[1]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 3 = \", byte_1_bin_in[0]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 4 = \", byte_2_bin_in[3]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 5 = \", byte_2_bin_in[2]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 6 = \", byte_2_bin_in[1]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Input 7 = \", byte_2_bin_in[0]\n\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Modbus Read Outputs\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Sending: x00 x01 x00 x00 x00 x06 x00 x03 x00 x01 x00 x01\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x01\\x00\\x01\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = s.recv(1024)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Raw received data = \", msg\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Received data as x format = \", repr(msg)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #Print the receive data in \\x hex format (notice that data above 32 hex will be represented by the ascii code e.g. 50 will show P\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = msg.encode(\"hex\") #Convert the data from \\x hex format to plain hex\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Hex data received = \", msg\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Strip downto last two bytes\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_out = msg[21]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_out = msg[20]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"byte_1_out = \", byte_1_out\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"byte_2_out = \", byte_2_out\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_bin_out = bin(int(byte_1_out, 16))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #convert hex to bin\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Binary value of Output byte 1 = \", byte_1_bin_out\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_1_bin_out = byte_1_bin_out[2:].zfill(4)&nbsp;&nbsp;&nbsp; #get rid of the 0b and fill with zeros so byte take 4 positions\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Remove 0b and make byte occupie 4 positions = \",byte_1_bin_out\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_bin_out = bin(int(byte_2_out, 16))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #convert hex to bin\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Binary value of Output byte 2 = \", byte_2_bin_out\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte_2_bin_out = byte_2_bin_out[2:].zfill(4)&nbsp;&nbsp;&nbsp; #get rid of the 0b and fill with zeros so byte take 4 positions\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Remove 0b and make output byte occupie 4 positions = \", byte_2_bin_out\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 0 = \", byte_1_bin_out[3]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 1 = \", byte_1_bin_out[2]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 2 = \", byte_1_bin_out[1]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 3 = \", byte_1_bin_out[0]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 4 = \", byte_2_bin_out[3]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 5 = \", byte_2_bin_out[2]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 6 = \", byte_2_bin_out[1]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Output 7 = \", byte_2_bin_out[0]\n&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;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sleep(10.00)&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; #Give time to read the read result before change output\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Modbus set Outputs to 00\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Sending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x01 x00 x00\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x01\\x00\\x00\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sleep(1.00)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Modbus set Outputs to 04 i.e. set output 2 on (third bit)\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print \"Sending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x01 x00 x04\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.send (\"\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x01\\x00\\x04\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time.sleep(1.00)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.close()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; program_run = 0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except socket.error as socketerror:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(\"Error: \", socketerror)\nprint \"Program finish\"\n\n<\/pre>\n\n\n\n<p><strong>Program run:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; \nStarting Program\n\nModbus Read inputs\nSending: x00 x01 x00 x00 x00 x06 x00 x03 x00 x00 x00 x01\nRaw received data = ####\nReceived data as x format =&nbsp; '\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x03\\x02\\x00\\x83'\n\nHex data received =&nbsp; 0001000000050003020083\nStrip downto last two bytes\nbyte_1_in =&nbsp; 3\nbyte_2_in =&nbsp; 8\nBinary value of input byte 1 =&nbsp; 0b11\nRemove 0b and make byte occupie 4 positions =&nbsp; 0011\nBinary value of input byte 2 =&nbsp; 0b1000\nRemove 0b and make byte occupie 4 positions =&nbsp; 1000\n\nInput 0 =&nbsp; 1\nInput 1 =&nbsp; 1\nInput 2 =&nbsp; 0\nInput 3 =&nbsp; 0\nInput 4 =&nbsp; 0\nInput 5 =&nbsp; 0\nInput 6 =&nbsp; 0\nInput 7 =&nbsp; 1\n\nModbus Read Outputs\nSending: x00 x01 x00 x00 x00 x06 x00 x03 x00 x01 x00 x01\nRaw received data = ####\nReceived data as x format =&nbsp; '\\x00\\x01\\x00\\x00\\x00\\x05\\x00\\x03\\x02\\x00\\x04'\n\nHex data received =&nbsp; 0001000000050003020004\nStrip downto last two bytes\nbyte_1_out =&nbsp; 4\nbyte_2_out =&nbsp; 0\nBinary value of Output byte 1 =&nbsp; 0b100\nRemove 0b and make byte occupie 4 positions =&nbsp; 0100\nBinary value of Output byte 2 =&nbsp; 0b0\nRemove 0b and make output byte occupie 4 positions =&nbsp; 0000\n\nOutput 0 =&nbsp; 0\nOutput 1 =&nbsp; 0\nOutput 2 =&nbsp; 1\nOutput 3 =&nbsp; 0\nOutput 4 =&nbsp; 0\nOutput 5 =&nbsp; 0\nOutput 6 =&nbsp; 0\nOutput 7 =&nbsp; 0\n\nModbus set Outputs to 00\nSending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x01 x00 x00\n\nModbus set Outputs to 04 i.e. set output 2 on (third bit)\nSending: x00 x01 x00 x00 x00 x06 x00 x06 x00 x01 x00 x04\n\n<\/pre>\n\n\n\n<p><strong>Notes:<\/strong><br>Data exchange using the Python program with Modbus Data send to UR to read and write UR register.<\/p>\n\n\n\n<p>First request is to read inputs.<\/p>\n\n\n\n<p>Data send<br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x00\\x00\\x01<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 03 00 00 00 01<\/p>\n\n\n\n<p>Meaning:<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 \u2013 means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>03 is the function code for read<br>00 00 The address of the first register requested&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 00hex = 0 dec which are inputs).<br>00 01 The total number of register to read<\/p>\n\n\n\n<p>Data received from UR:<\/p>\n\n\n\n<p>(Notice the data received from the UR is in hex format which might have values below 32 which maybe does not display on the screen depending on the data value because such data below 32 can be control characters. Therefore the data is formatted by the python program to readable data).<\/p>\n\n\n\n<p>First readable poll received:&nbsp; 0001000000050003020083<\/p>\n\n\n\n<p>Meaning:<br>00 01 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 \u2013 means 5 bytes will follow<br>03 was the function code requested<br>02 registers are read<br>00 83 is data value received. (83 is the interesting data where 3 belongs to first byte of inputs and 8 belongs to second byte of inputs).<\/p>\n\n\n\n<p>Value 3 = 0011 for first byte which means bit 0 and bit 1 has the value &#8220;high&#8221;.<\/p>\n\n\n\n<p>Value 8 = 1000 for second byte which means bit 7 has the value &#8220;high&#8221;.<\/p>\n\n\n\n<p>This is correct according to physical current state of the inputs as shown above in the I\/O window on the UR.<\/p>\n\n\n\n<p>Then the python program strips the hex values down to bit value for each inputs and present on the screen.<\/p>\n\n\n\n<p>Input 0 =&nbsp; 1<br>Input 1 =&nbsp; 1<br>Input 2 =&nbsp; 0<br>Input 3 =&nbsp; 0<br>Input 4 =&nbsp; 0<br>Input 5 =&nbsp; 0<br>Input 6 =&nbsp; 0<br>Input 7 =&nbsp; 1<\/p>\n\n\n\n<p>Second request is to read outputs.<\/p>\n\n\n\n<p>Data send<br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x03\\x00\\x01\\x00\\x01<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 03 00 01 00 01<\/p>\n\n\n\n<p>Meaning:<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 \u2013 means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>03 is the function code for read<br>00 01 The address of the second register requested&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 01hex = 1 dec which are outputs).<br>00 01 The total number of register to read<\/p>\n\n\n\n<p>Data received from UR:<\/p>\n\n\n\n<p>(Notice the data received from the UR is in hex format which might have values below 32 which maybe does not display on the screen depending on the data value because such data below 32 can be control characters. Therefore the data is formatted by the python program to readable data).<\/p>\n\n\n\n<p>Second readable poll received:&nbsp; 0001000000050003020004<\/p>\n\n\n\n<p>Meaning:<br>00 01 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 \u2013 means 5 bytes will follow<br>03 was the function code requested<br>02 registers are read<br>00 04 is data value received. (04 is the interesting data where 4 belongs to first byte of outputs and 0 belongs to second byte of outputs).<\/p>\n\n\n\n<p>Value 4 = 0100 for first byte which means bit 2 has the value &#8220;high&#8221;.<\/p>\n\n\n\n<p>Value 0 = 0000 for second byte which means all bits has the value &#8220;low&#8221;.<\/p>\n\n\n\n<p>This is correct according to physical current state of the inputs as shown above in the I\/O window on the UR.<\/p>\n\n\n\n<p>Then the python program strips the hex values down to bit value for each inputs and present on the screen.<\/p>\n\n\n\n<p>Output 0 =&nbsp; 0<br>Output 1 =&nbsp; 0<br>Output 2 =&nbsp; 1<br>Output 3 =&nbsp; 0<br>Output 4 =&nbsp; 0<br>Output 5 =&nbsp; 0<br>Output 6 =&nbsp; 0<br>Output 7 =&nbsp; 0<\/p>\n\n\n\n<p>First request to write to set output.<\/p>\n\n\n\n<p>Data send.<br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x01\\x00\\x00<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 06 00 01 00 00<\/p>\n\n\n\n<p>Meaning:<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 \u2013 means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>06 is the function code for write<br>00 01 The data address of the first register to set &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 01hex = 1 dec).<br>00 00 The value to set into the register&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 &#8211; this sets all outputs low)<\/p>\n\n\n\n<p>Then the program wait 1 second.<\/p>\n\n\n\n<p>Second request to write.<\/p>\n\n\n\n<p>Data send.<br>\\x00\\x01\\x00\\x00\\x00\\x06\\x00\\x06\\x00\\x01\\x00\\x04<\/p>\n\n\n\n<p>Which is the same as<br>00 01 00 00 00 06 00 06 00 01 00 04<\/p>\n\n\n\n<p>Meaning:<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 \u2013 means 6 bytes will follow<br>00 Unit identifier \u2013 or slave address<br>06 is the function code for write<br>00 01 The data address of the first register to set&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (register 01hex = 1 dec).<br>00 04 The value to set into the register&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 4 &#8211; this sets output 3 high)<\/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 registers Input and Output handling via port 502 Application Description:This example shows the use of internal MODBUS Input-Output registers on the Universal-Robots. The application uses the MODBUS server running at port 502 on the Universal-Robots. The&#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-registers-input-and-output-handling-via-port-502\/\">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-2588","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\/2588","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=2588"}],"version-history":[{"count":5,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2588\/revisions"}],"predecessor-version":[{"id":3100,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2588\/revisions\/3100"}],"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=2588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}