<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[James's Programming Page — Ubuntu and byte ordering...]]></title>
		<link>https://swforum.seekye.com/topic/10140/</link>
		<atom:link href="https://swforum.seekye.com/feed/rss/topic/10140/" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Ubuntu and byte ordering....]]></description>
		<lastBuildDate>Thu, 16 Jan 2020 05:14:17 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Ubuntu and byte ordering...]]></title>
			<link>https://swforum.seekye.com/post/13110/#p13110</link>
			<description><![CDATA[<p>Thanks...actually think I know what is happening, but surprised that no one else is having the same issue.</p><p>I dumped out the byte array that is returned for the features that I was requesting and found something interesting.</p><p>The call to GetFeature which I invoke as follows:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public static byte[] GetReportData(byte reportNumber, HidStream stream, int maxInputBufferSize)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exception internalException = null;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var inputReportBuffer = new byte[maxInputBufferSize];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputReportBuffer[0] = reportNumber;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream.GetFeature(inputReportBuffer);</p><p>// DUMP TO CONSOLE TO SEE OUTPUT OF ABOVE CALL<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($@&quot;Report {reportNumber}: {BitConverter.ToString(inputReportBuffer)}&quot;);<br />///////////</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return inputReportBuffer;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; internalException = e;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception($&quot;Unable to retrieve data from UPS after {internalException}&quot;, internalException);<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><p>In Windows I see this in the Console Window: </p><p>Report 0x51: 51-00-00-00-00-00-00-00<br />Report 0x32: 32-00-00-09-00-00-00-00<br />Report 0x17: 17-FF-FF-00-00-00-00-00</p><p>In Linux I see this in the Console Window:</p><p>Report 0x51: 51-51-00-00-00-00-00-00<br />Report 0x32: 32-32-00-09-00-00-00-00<br />Report 0x17: 17-17-FF-FF-00-00-00-00</p><p>The return from the call to GetFeature returns the reportID in byte 0 &amp; byte 1.</p><p>When I took a look at the code in LinuxHidStream.cs, I noticed that the ReportId was being copied to the second byte and the call to NativeMethods.ioctl was indexing at ptr + offset + 1 for the 3rd parameter:</p><p>public unsafe override void GetFeature(byte[] buffer, int offset, int count)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throw.If.OutOfRange(buffer, offset, count).False(count &gt;= 2);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HandleAcquireIfOpenOrFail();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte reportID = buffer[offset];</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fixed (byte* ptr = buffer)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[offset + 1] = reportID;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int bytes = NativeMethods.ioctl(_handle, NativeMethods.HIDIOCGFEATURE(count - 1), (IntPtr)(ptr + offset + 1));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bytes &lt; 0) { throw new IOException(&quot;GetFeature failed.&quot;); }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Array.Clear(buffer, 1 + bytes, count - (1 + bytes));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HandleRelease();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />I changed the code so that it didn&#039;t copy the ReportId from byte 0 to byte 1 and adjusted the offset to the call to NativeMethods.ioctl as follows:</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; public unsafe override void GetFeature(byte[] buffer, int offset, int count)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throw.If.OutOfRange(buffer, offset, count).False(count &gt;= 2);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HandleAcquireIfOpenOrFail();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ////////////////////////////////////////////////////<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; Not needed -- see below<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; byte reportID = buffer[offset];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //////////////////////////////////////////</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fixed (byte* ptr = buffer)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;///////////////////////////////////////////////////////////<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;// Remove copying the report ID, since we already have it in byte 0<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; buffer[offset + 1] = reportID;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; int bytes = NativeMethods.ioctl(_handle, NativeMethods.HIDIOCGFEATURE(count - 1), (IntPtr)(ptr + offset + 1));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;///////////////////////////////////////////////////////////<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int bytes = NativeMethods.ioctl(_handle, NativeMethods.HIDIOCGFEATURE(count), (IntPtr)(ptr + offset));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bytes &lt; 0) { throw new IOException(&quot;GetFeature failed.&quot;); }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Array.Clear(buffer, bytes, count - bytes);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HandleRelease();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />And everything started working correctly.</p><p>Not sure why the code was adding the ReportId twice and surprised that no one else is having a similar issue. </p><p>Hoping James might give some insight as to why it was coded this way and if there is an issue with my fix.</p><p>Thanks!!</p>]]></description>
			<author><![CDATA[null@example.com (johnbizios)]]></author>
			<pubDate>Thu, 16 Jan 2020 05:14:17 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13110/#p13110</guid>
		</item>
		<item>
			<title><![CDATA[Re: Ubuntu and byte ordering...]]></title>
			<link>https://swforum.seekye.com/post/13107/#p13107</link>
			<description><![CDATA[<p>All the systems you mentioned are x86_x64 so the byte order should be the same.&nbsp; <br />Just to be sure, have you tried reversing the order of the bytes on Ubuntu before converting them?&nbsp; (as a test only)<br />If the byte order is indeed reversed, the values should then be what you expected,&nbsp; <br />changes are they might still be &quot;out of whack&quot;</p>]]></description>
			<author><![CDATA[null@example.com (timothyp)]]></author>
			<pubDate>Thu, 16 Jan 2020 03:07:32 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13107/#p13107</guid>
		</item>
		<item>
			<title><![CDATA[Ubuntu and byte ordering...]]></title>
			<link>https://swforum.seekye.com/post/13106/#p13106</link>
			<description><![CDATA[<p>Hi -</p><p>I&#039;ve written a .NET Core 3.1 service to read data from a UPS device using HidSharp.&nbsp; I am targeting 3 different OS&#039;s:&nbsp; Windows, Mac and Ubuntu.</p><p>When I publish the service to Windows and Mac self-contained images, everything is working great and I&#039;m getting good values for the various HID Feature reports.</p><p>However, when I set my Target runtime to Linux-x64 and attempt to run it on Ubuntu, the values that I&#039;m retrieving from the HID Features are &quot;out of whack&quot; (great technical term!).&nbsp; &nbsp;For example, Voltage which should be 120V and is getting reported as such on MAC and Window.&nbsp; However, on Ubuntu I get a value of 4,841V.</p><p>My best guess is that it might be some byte ordering difference, though I&#039;m running on a dual-booted 64-bit Windows 10/Ubuntu Intel&nbsp; i7 machine. The Windows 10 image works, the Ubuntu does not.&nbsp; Strings on Ubuntu are coming back fine, getting Serial Number, Vendor Id, etc w/o issue. </p><p>It&#039;s only when I&#039;m trying to convert the byte array to a double (for example) that I&#039;m seeing the issue.</p><p>When I publish, I&#039;m targeting Linux-x64 with an Any CPU configuration to create the binary image of the Service.</p><p>Any thoughts or suggestions as to what I&#039;m doing wrong would be greatly appreciated.</p><p>Thanks!</p>]]></description>
			<author><![CDATA[null@example.com (johnbizios)]]></author>
			<pubDate>Wed, 15 Jan 2020 19:58:57 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13106/#p13106</guid>
		</item>
	</channel>
</rss>
