<?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 — Problems writing to Skylander RFID portal]]></title>
		<link>https://swforum.seekye.com/topic/10149/</link>
		<atom:link href="https://swforum.seekye.com/feed/rss/topic/10149/" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Problems writing to Skylander RFID portal.]]></description>
		<lastBuildDate>Sat, 20 Jan 2024 08:28:18 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13144/#p13144</link>
			<description><![CDATA[<p>Have you considered checking the USB transfer type and making adjustments to match the working C++ version, as it seems the portal may be sensitive to the type of data transfer? Additionally, have you explored the possibility of exposing the HidD_SetOutputReport function in the NativeMethods to see if it resolves the issue with the Skylander RFID portal?</p>]]></description>
			<author><![CDATA[null@example.com (FuluplEnulk)]]></author>
			<pubDate>Sat, 20 Jan 2024 08:28:18 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13144/#p13144</guid>
		</item>
		<item>
			<title><![CDATA[Re: Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13136/#p13136</link>
			<description><![CDATA[<p>Using an googling around using the above discoveries, I found that microsoft advertises two methods to write to usb hid Writefile and HidD_SetOutputReport from hid.dll</p><p>The latter one is not among the ones exposed in nativemethods. But set/get feature from the same dll is there. So I added this function to nativemethods.</p><p>[DllImport(&quot;hid.dll&quot;, SetLastError = true)]<br />&nbsp; &nbsp; &nbsp; &nbsp; public static extern bool HidD_SetOutputReport(IntPtr handle, byte[] lpReportBuffer, int ReportBufferLength);</p><p>change write in winhidstream to </p><p>fixed (byte* ptr = _writeBuffer)<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int offset0 = 0;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (count &gt; 0)<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var overlapped = stackalloc NativeOverlapped[1];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overlapped[0].EventHandle = @event;</p><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bytesTransferred = 33;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.HidD_SetOutputReport(_handle, _writeBuffer, 33);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //bool result = NativeMethods.WriteFile(_handle, ptr + offset0, Math.Min(minOut, count), IntPtr.Zero, overlapped);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //int win32Error = Marshal.GetLastWin32Error();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //NativeMethods.OverlappedOperation(_handle, @event, WriteTimeout, _closeEventHandle,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // result,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // overlapped, out bytesTransferred);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count -= (int)bytesTransferred; offset0 += (int)bytesTransferred;<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p>Crude change I know, but it works flawlessly. I guess the skylander rfid portal is real picky about exactly the type of transfer/message it gets. As I said, I know to little about usb to completely understand this. But I&#039;am happy it works at least.</p><p>Is it possible to make a new version that exposes this setreport function somehow? Keeping de write intact, but adding a WriteReport function to the stream?</p><p>I also expect a similar change is needed on the linux side of things.... But I know even less where to look on that platform.</p><p>edit: Digging a little more it seems that writefile does different thing depending on the device. It uses the control endpoint to send reports unless it finds a interrupt out endpoint. (I don&#039;t know if this behavior is controllable). </p><p>I thing the rfid portal uses the interrupt pipe to send a constant stream of status updates. But that it still expects commands on the control endpoint or something like that.</p>]]></description>
			<author><![CDATA[null@example.com (bzuidgeest)]]></author>
			<pubDate>Wed, 24 Nov 2021 22:18:10 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13136/#p13136</guid>
		</item>
		<item>
			<title><![CDATA[Re: Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13135/#p13135</link>
			<description><![CDATA[<p>Though its a little out of my league it tried to use a usb analyser to get a trace both with the c# and the c++ version.</p><p>The analyser gives the same report data in both cases and in both cases an actual &quot;report&quot; seems to be send. </p><p>But the analyser has a summerize windows and it show this for the working c++ version. Note the text at URB function.<br />&nbsp; URB Class Interface issued<br />Device Object&nbsp; &nbsp; FFFFD40A17A13CB0h<br />Driver Object&nbsp; &nbsp; hhdusbh64</p><p>URB Function&nbsp; &nbsp; URB_FUNCTION_CLASS_INTERFACE</p><p>Request&nbsp; &nbsp; Set Report<br />Report Type&nbsp; &nbsp; Output<br />Report Length&nbsp; &nbsp; 32</p><p>when I write using c# using hidsharp I get:</p><br /><p>Device Object&nbsp; &nbsp; FFFFD40A17A13CB0h<br />Driver Object&nbsp; &nbsp; hhdusbh64</p><p>URB Function&nbsp; &nbsp; URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER</p><p>Endpoint 01h&nbsp; &nbsp; 1 Out, Interrupt</p><p>Report Type&nbsp; &nbsp; Output<br />Report Length&nbsp; &nbsp; 32</p><p>Seems to me there is some difference in the type of data transfer. The working c++ code does set report and hidsharp does not? Somewhere in this difference seems to be something the portal does not like.</p>]]></description>
			<author><![CDATA[null@example.com (bzuidgeest)]]></author>
			<pubDate>Wed, 24 Nov 2021 21:47:50 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13135/#p13135</guid>
		</item>
		<item>
			<title><![CDATA[Re: Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13133/#p13133</link>
			<description><![CDATA[<div class="quotebox"><cite>Zer wrote:</cite><blockquote><p>I don&#039;t have this device.</p></blockquote></div><p> <br />I could hope, but this was to be expected. You cannot have every device. </p><div class="quotebox"><cite>Zer wrote:</cite><blockquote><p>but, just want to make sure, portal.GetMaxOutputReportLength() returns 33, yes?</p></blockquote></div><p> <br />Yes is does return 33 both for in and output report. </p><br /><div class="quotebox"><cite>Zer wrote:</cite><blockquote><p>hid_write in the C version seems to assume this.</p></blockquote></div><p> <br />It assumes a lot. But I did not write it <img src="https://swforum.seekye.com/img/smilies/smile.png" width="15" height="15" alt="smile" />. I&#039;m just happy someone wrote it, as figuring out usb devices is not exactly a big skillset of mine <img src="https://swforum.seekye.com/img/smilies/smile.png" width="15" height="15" alt="smile" /></p>]]></description>
			<author><![CDATA[null@example.com (bzuidgeest)]]></author>
			<pubDate>Mon, 22 Nov 2021 12:34:59 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13133/#p13133</guid>
		</item>
		<item>
			<title><![CDATA[Re: Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13131/#p13131</link>
			<description><![CDATA[<p>I don&#039;t have this device, but, just want to make sure, portal.GetMaxOutputReportLength() returns 33, yes?<br />(hid_write in the C version seems to assume this.)</p>]]></description>
			<author><![CDATA[null@example.com (Zer)]]></author>
			<pubDate>Fri, 19 Nov 2021 16:04:37 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13131/#p13131</guid>
		</item>
		<item>
			<title><![CDATA[Problems writing to Skylander RFID portal]]></title>
			<link>https://swforum.seekye.com/post/13130/#p13130</link>
			<description><![CDATA[<p>For fun I wanted to to play around with the skylander rfid portal. This is an USB Hid device and the &quot;protocol&quot; is quite simple or so it appears from SKyDumper project (<a href="http://h**ps://github.com/capull0/SkyDumper">h**ps://github.com/capull0/SkyDumper</a>). This tool SkyDumper works on my linux computer, but when I try to convert it to hidsharp it won&#039;t work.</p><p>To be more specific.<br />* I can detect the portal just fine and get the device. <br />* I can open a HidStream<br />* I then try to send the &quot;restart&quot; message. Basically report Zero and the letter R, the rest all zeros.<br />* As soon as this is send out the portal starts sending status messages which I receive continuously. So the message did wake up something.<br />* However the write errors out with an exception. On my linux laptop its and I/O exception hardcoded in the output source code of hidsharp and on windows its a timeout exception.</p><p>I&#039;m no usb export and have little clue why this happens. </p><p>I converted portalIO.cpp to the C# code below. I must be missing something silly I think. No reason for it to work using the original cpp code and not thru HidSharp. The first write failure occurs on the call to restartportal. After that I am buried in status output from the portal but nothing else. Any further write messages give the same result</p><p>If anyone has a clue or suggestion on what to try next? That would be appreciated.</p><p>using System;<br />using System.Text;<br />using HidSharp;<br />using System.Linq;<br />using System.Collections.Generic;<br />using System.Threading;<br />using System.Threading.Tasks;<br />using HidSharp.Reports;<br />using HidSharp.Reports.Input;</p><p>namespace SkyReaderLib<br />{<br />&nbsp; &nbsp; public class PortalIO<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; public const int rw_buffer_size = 0x21;<br />&nbsp; &nbsp; &nbsp; &nbsp; public const int TIMEOUT = 30000;<br />&nbsp; &nbsp; &nbsp; &nbsp; public const int SKYLANDER_SIZE = 1024;<br />&nbsp; &nbsp; &nbsp; &nbsp; public const int MAX_STR = 255;<br />&nbsp; &nbsp; &nbsp; &nbsp; private HidDevice portal;<br />&nbsp; &nbsp; &nbsp; &nbsp; private HidStream portalStream;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private bool initialized = false;</p><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; public event EventHandler&lt;StatusChangedEventArgs&gt; StatusEvent;</p><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; public static IEnumerable&lt;HidDevice&gt; ListPortals()<br />&nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IEnumerable&lt;HidDevice&gt; devices = DeviceList.Local.GetHidDevices()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x =&gt; (x.VendorID == 0x12ba || x.VendorID == 0x54c || x.VendorID == 0x1430)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (x.ProductID == 0x150 || x.ProductID == 0x967));</p><p>#if DEBUG<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (devices.Any())<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Found portal usb devices:&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (HidDevice device in devices)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($&quot;\t{device.GetFriendlyName()}&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />#endif<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return devices;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><br /><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; // Send a command to the portal<br />&nbsp; &nbsp; &nbsp; &nbsp; private void Write(byte[] message)<br />&nbsp; &nbsp; &nbsp; &nbsp; {</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; byte[] buffer = new byte[portal.GetMaxOutputReportLength()];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.CopyTo(buffer, 0);<br />#if DEBUG<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Issued report out:&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(string.Join(&quot; &quot;, buffer));<br />#endif<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; portalStream.Write(buffer);<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; Console.WriteLine($&quot;Exception while issuing a report occured: {e}&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; </p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private bool CheckResponse(out RWBlock response, byte expect)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = new RWBlock();<br />#if DEBUG<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;&gt;&gt;&gt; CheckResponse\n&quot;);<br />#endif</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int result = portalStream.Read(response.buffer, 0, rw_buffer_size);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result &lt; 0)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(&quot;Could not read response&quot;);</p><p>#if DEBUG<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;CheckResponse read = {0} bytes\n&quot;, result);<br />#endif</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.dwBytesTransferred = result;</p><p>&nbsp; &nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // found wireless USB but portal is not connected<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.buffer[0] == &#039;Z&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception(&quot;Found portal dongle, but portal not connected&quot;);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Status says no skylander on portal<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if (response-&gt;buffer[0] == &#039;Q&#039; &amp;&amp; response-&gt;buffer[1] == 0)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp;throw 11;</p><br /><p>#if DEBUG<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;&lt;&lt;&lt; CheckResponse\n&quot;);<br />#endif<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (response.buffer[0] != expect);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; // <br />&nbsp; &nbsp; &nbsp; &nbsp; public void PortalStatus()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[portal.GetMaxOutputReportLength()];</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[0] = 0;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[1] = (byte)&#039;S&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write(buffer);<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; // Start portal<br />&nbsp; &nbsp; &nbsp; &nbsp; public void RestartPortal()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[portal.GetMaxOutputReportLength()];</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[0] = 0;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[1] = (byte)&#039;R&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write(buffer);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; // Antenna up / activate<br />&nbsp; &nbsp; &nbsp; &nbsp; public void ActivatePortal(int active)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[portal.GetMaxOutputReportLength()];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[0] = 0;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[1] = (byte)&#039;A&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[2] = (byte)active;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write(buffer);<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; // Set the portal color<br />&nbsp; &nbsp; &nbsp; &nbsp; public void SetPortalColor(byte r, byte g, byte b)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[portal.GetMaxOutputReportLength()];</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[0] = 0;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[1] = (byte)&#039;C&#039;;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[2] = r; // R<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[3] = g; // G<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer[4] = b; // B</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Write(buffer);<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; // Release hPortalInstance<br />&nbsp; &nbsp; &nbsp; &nbsp; ~PortalIO()<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivatePortal(0);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; portalStream.Close();<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; public void Flash()<br />&nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (; ; )<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivatePortal(1);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivatePortal(0);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><br /><p>&nbsp; &nbsp; &nbsp; &nbsp; public PortalIO(HidDevice portal)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Connecting to portal.\n&quot;);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.portal = portal;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initialized = OpenPortal(portal);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (initialized)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartPortal();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivatePortal(1);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetPortalColor(0xC8, 0xC8, 0xC8);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Error initializing portal.&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private bool OpenPortal(HidDevice portal)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(portal.GetMaxInputReportLength());<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReportDescriptor descriptor = portal.GetReportDescriptor();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HidDeviceInputReceiver reciever = descriptor.CreateHidDeviceInputReceiver();<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool opened = portal.TryOpen(out portalStream);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var x = portal.GetReportDescriptor();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reciever.Start(portalStream);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reciever.Received += OnReceiverReceived;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; portalStream.Closed += PortalStreamOnClosed;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return opened;<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void PortalStreamOnClosed(object? sender, EventArgs e)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Closed!&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void OnReceiverReceived(object sender, EventArgs args)<br />&nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var receiver = (HidDeviceInputReceiver)sender;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (receiver.Stream?.Device == null)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var length = receiver.Stream.Device.GetMaxInputReportLength();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var buffer = new byte[length];</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (receiver.TryRead(buffer, 0, out Report report))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Console.WriteLine($&quot;Received report with id: {report.ReportID}, type: {report.ReportType}&quot;);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; report.Read(buffer, 0, (bytes, offset, item, dataItem) =&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Console.WriteLine(string.Join(&quot; &quot;, bytes));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch((char)buffer[1])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case &#039;S&#039;:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StatusEvent?.Invoke(this, new StatusChangedEventArgs(buffer));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &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; else<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(&quot;Failed to read report.&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<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; Console.WriteLine($&quot;Exception while reading report occured: {e}&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><br /><br /><p>&nbsp; &nbsp; }<br />}</p>]]></description>
			<author><![CDATA[null@example.com (bzuidgeest)]]></author>
			<pubDate>Mon, 15 Nov 2021 16:43:39 +0000</pubDate>
			<guid>https://swforum.seekye.com/post/13130/#p13130</guid>
		</item>
	</channel>
</rss>
