{"id":1945,"date":"2018-07-19T10:26:22","date_gmt":"2018-07-19T10:26:22","guid":{"rendered":"https:\/\/ibex.tech\/resources\/?p=1945"},"modified":"2022-02-18T15:05:16","modified_gmt":"2022-02-18T15:05:16","slug":"byte-ordering","status":"publish","type":"post","link":"https:\/\/ibex.tech\/resources\/geek-area\/communications\/modbus\/byte-ordering","title":{"rendered":"Byte ordering"},"content":{"rendered":"<p>\nModbus is a big-endian&nbsp;protocol.&nbsp; The more significant byte of a 16-bit value is sent before the less significant byte\n<\/p>\n<p>\nSo, for bytes on the bus a UInt16&nbsp;is sent like this:\n<\/p>\n<p style=\"margin-left: 40px;\">\nBits15:8 | Bits7:0\n<\/p>\n<h4>\nConverting&nbsp;the modbus 16 bit words<br \/>\n<\/h4>\n<h5>\nUInt32&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] |&nbsp;UInt16[1]\n<\/p>\n<h5>\nUInt64&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] | UInt16[1] |&nbsp;UInt16[2] |&nbsp;UInt16[3]\n<\/p>\n<h5>\nFloat&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] | UInt16[1]\n<\/p>\n<pre>\r\n<code>\r\n\t\/\/Converting big endian modbus registers to float on little endian windows&nbsp;(ModbusRegisterValues[0] was received first)\r\n\tarray&lt;Byte&gt;^byteArray = gcnew array&lt;Byte&gt;(4);\r\n\tbyteArray[3] = (Byte)(ModbusRegisterValues[0] &gt;&gt; 8);\r\n\tbyteArray[2] = (Byte)(ModbusRegisterValues[0] &amp; 0x00ff);\r\n\tbyteArray[1] = (Byte)(ModbusRegisterValues[1] &gt;&gt; 8);\r\n\tbyteArray[0] = (Byte)(ModbusRegisterValues[1] &amp; 0x00ff);\r\n\tfloat my_float = BitConverter::ToSingle(byteArray, 0);\r\n<\/code><\/pre>\n<pre>\r\n<code>\r\n\t\/\/Converting float on little endian windows to big endian modbus registers (ModbusRegisterValues[0] will be sent first)\r\n\tModbusRegisterValues = gcnew array &lt;UInt16&gt;(2);\r\n\tarray&lt;Byte&gt;^byteArray = BitConverter::GetBytes(my_float_value);\r\n\tModbusRegisterValues[0] = ((UInt16)byteArray[3] &lt;&lt; 8) | ((UInt16)byteArray[2]);\t\t\/\/Converting float on little endian windows to big endian modbus registers\r\n\tModbusRegisterValues[1] = ((UInt16)byteArray[1] &lt;&lt; 8) | ((UInt16)byteArray[0]);\r\n<\/code><\/pre>\n<h5>\nchar conversion<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\t\/\/RegisterValues[] is UInt16 array read over modbus\r\n\tarray&lt;Byte&gt; ^NameArray = gcnew array&lt;Byte&gt;(10);\r\n\tNameArray[0] = (Byte)(RegisterValues[0] &gt;&gt; 8);\r\n\tNameArray[1] = (Byte)(RegisterValues[0] &amp; 0x00ff);\r\n\tNameArray[2] = (Byte)(RegisterValues[1] &gt;&gt; 8);\r\n\tNameArray[3] = (Byte)(RegisterValues[1] &amp; 0x00ff);\r\n\tNameArray[4] = (Byte)(RegisterValues[2] &gt;&gt; 8);\r\n\tNameArray[5] = (Byte)(RegisterValues[2] &amp; 0x00ff);\r\n\tNameArray[6] = (Byte)(RegisterValues[3] &gt;&gt; 8);\r\n\tNameArray[7] = (Byte)(RegisterValues[3] &amp; 0x00ff);\r\n\tNameArray[8] = (Byte)(RegisterValues[4] &gt;&gt; 8);\r\n\tNameArray[9] = (Byte)(RegisterValues[4] &amp; 0x00ff);\r\n\tsTemp = System::Text::Encoding::UTF8-&gt;GetString(NameArray);\r\n\tsTemp = sTemp-&gt;Replace(&quot;\\0&quot;, &quot;&quot;);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modbus is a big-endian&nbsp;protocol.&nbsp; The more significant byte of a 16-bit value is sent before the less significant byte So, for bytes on the bus a UInt16&nbsp;is sent like this: Bits15:8 | Bits7:0 Converting&nbsp;the modbus 16 bit words UInt32&nbsp;conversion UInt16[0] |&nbsp;UInt16[1] UInt64&nbsp;conversion UInt16[0] | UInt16[1] |&nbsp;UInt16[2] |&nbsp;UInt16[3] Float&nbsp;conversion UInt16[0] | UInt16[1] \/\/Converting big endian modbus [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[],"class_list":["post-1945","post","type-post","status-publish","format-standard","hentry","category-modbus"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/posts\/1945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/comments?post=1945"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/posts\/1945\/revisions"}],"predecessor-version":[{"id":1951,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/posts\/1945\/revisions\/1951"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/media?parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/categories?post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/resources\/wp-json\/wp\/v2\/tags?post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}