<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[James's Programming Page — HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
	<link rel="self" href="https://swforum.seekye.com/feed/atom/topic/10122/" />
	<updated>2023-09-29T12:30:33Z</updated>
	<generator>PunBB</generator>
	<id>https://swforum.seekye.com/topic/10122/</id>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13143/#p13143" />
			<content type="html"><![CDATA[<p>var list = DeviceList.Local;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var device = list.GetHidDevices(vendorId, productId).FirstOrDefault();<br />&nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var stream = device.Open())<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($&quot;Listening to {device.GetFriendlyName()}...&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[device.GetMaxInputReportLength()];<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int bytesRead = stream.Read(buffer);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (bytesRead &gt; 0)<br />&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; // Process the keypress data from the buffer<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You&#039;ll need to interpret the data to determine which keys were pressed<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($&quot;Received {bytesRead} bytes: {BitConverter.ToString(buffer)}&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><p>getting &#039;Can&#039;t read from this device&#039;</p><p>can anyone help please</p>]]></content>
			<author>
				<name><![CDATA[jannat]]></name>
				<uri>https://swforum.seekye.com/user/14975/</uri>
			</author>
			<updated>2023-09-29T12:30:33Z</updated>
			<id>https://swforum.seekye.com/post/13143/#p13143</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13139/#p13139" />
			<content type="html"><![CDATA[<p>I&#039;m new to HID and I think I&#039;m having the exact same problem with the aux buttons of my graphics tablet. Does it matter which driver I install in Device Manager? I&#039;ve tried changing it from HID Keyboard to HID-compliant device and it still does not open the stream.</p><p>But with USB analysing software like USB Analyzer from eltima it seems to receive the input. I&#039;m wondering why this program is able to receive the input, and HIDSharp isn&#039;t.</p>]]></content>
			<author>
				<name><![CDATA[dasjulian]]></name>
				<uri>https://swforum.seekye.com/user/14465/</uri>
			</author>
			<updated>2022-06-04T18:58:55Z</updated>
			<id>https://swforum.seekye.com/post/13139/#p13139</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13061/#p13061" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>regisls wrote:</cite><blockquote><p>I am trying to connect to a barcode scanner and I&#039;m having the same problem when I try to open the stream.</p><p>If I understand correctly, does this mean that I can&#039;t read the text produced by a bar code scanner with the HidSharp library because the scanner is interpreted as a keyboard and it is blocked by Windows?</p><p>Do I have any alternatives? Can I read the input buffer without &quot;opening&quot; the device?</p></blockquote></div><p>There are barcode scanners that act as a keyboard (Keyboard Wedge Device).&nbsp; I&#039;d drop a link here explaining but I don&#039;t appear to have permissions.</p><p>So it&#039;s possible that&#039;s the reason.&nbsp; Do you have a manual/datasheet for your scanner that would confirm what the interface is?</p><p>You may not even need HIDSharp to read it.&nbsp; You can probably use the KeyDown event in .NET if it&#039;s all Alphanumeric.</p><p>In my case the codes I need aren&#039;t seen by the keydown or keypreview events so I was hoping HIDSharp would just let me see the codes directly.</p>]]></content>
			<author>
				<name><![CDATA[KTrenholm]]></name>
				<uri>https://swforum.seekye.com/user/14273/</uri>
			</author>
			<updated>2019-06-03T17:59:33Z</updated>
			<id>https://swforum.seekye.com/post/13061/#p13061</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13060/#p13060" />
			<content type="html"><![CDATA[<p>I am trying to connect to a barcode scanner and I&#039;m having the same problem when I try to open the stream.</p><p>If I understand correctly, does this mean that I can&#039;t read the text produced by a bar code scanner with the HidSharp library because the scanner is interpreted as a keyboard and it is blocked by Windows?</p><p>Do I have any alternatives? Can I read the input buffer without &quot;opening&quot; the device?</p>]]></content>
			<author>
				<name><![CDATA[regisls]]></name>
				<uri>https://swforum.seekye.com/user/14278/</uri>
			</author>
			<updated>2019-06-03T14:52:04Z</updated>
			<id>https://swforum.seekye.com/post/13060/#p13060</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13055/#p13055" />
			<content type="html"><![CDATA[<p>Oh wow, I had no idea about HID keyboards being essentially &quot;Locked&quot; in Windows like that.&nbsp; </p><p>In my case there can be no separate device for these &quot;extra&quot; keys as the customer is expecting the presses from the HID keyboard device.&nbsp; </p><p>It&#039;s USB codes 0x68 through to 0x77 I need to confirm are being sent, and&nbsp; unfortunately the .NET Keypress/Keydown/KeyPreview events do not seem to trigger on these codes which is why I wanted to capture the bytes right from the keyboard.&nbsp; I can confirm they are being sent via a USB Analyzer.&nbsp; Perhaps I need to look past .NET then and see if I have any better luck.</p><p>Back to the drawing board I suppose.&nbsp; </p><p>Thank you for the information, I don&#039;t think I would have ever figured that out!</p>]]></content>
			<author>
				<name><![CDATA[KTrenholm]]></name>
				<uri>https://swforum.seekye.com/user/14273/</uri>
			</author>
			<updated>2019-05-29T17:45:58Z</updated>
			<id>https://swforum.seekye.com/post/13055/#p13055</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13054/#p13054" />
			<content type="html"><![CDATA[<p>Windows does not allow you to open keyboard HID devices for security reasons. Extra keys like volume controls, etc. often show up separately and can be opened. Those OpenOptions are a HIDSharp feature to allow coordinating opening the device between different HIDSharp instances on the same PC (the OS doesn&#039;t prevent opening HID devices twice).</p>]]></content>
			<author>
				<name><![CDATA[Zer]]></name>
				<uri>https://swforum.seekye.com/user/2/</uri>
			</author>
			<updated>2019-05-28T20:23:31Z</updated>
			<id>https://swforum.seekye.com/post/13054/#p13054</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[HIDSharp: Cannot open datastream, TryOpen() always returns false?]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/13052/#p13052" />
			<content type="html"><![CDATA[<p>Hello all,</p><p>I&#039;m fairly new to working with HID, especially from the host side.&nbsp; I usually stick to microcontrollers but in this case I need to write an application to monitor the traffic from my microcontroller based HID devices for testing purposes.</p><p>I&#039;ve dabbled in C# .NET so HIDSharp seems perfect for whipping something up quickly with WinForms.&nbsp; It&#039;s strange to me that MS doesn&#039;t just have libraries in .NET for this but that&#039;s not my problem today.</p><p>I&#039;m working with HIDSharp v2.0.2.0 attempting to monitor traffic from a composite HID device.&nbsp; The composite device consists of a Keyboard with LEDs and a 3-Button mouse.&nbsp; Both are HID.&nbsp; Keyboard Codes on EP1, Mouse is on EP2.&nbsp; The device works with the standard windows HID Mouse and Keyboard drivers.</p><p>First things first, I want to have a look at the keyboard traffic, since I have a few weird custom scancodes I need to ensure are coming through correctly.</p><p>I am able to find the device based on the VID/PID and confirm based on the name if I am seeing the Keyboard:</p><div class="codebox"><pre><code>HidDevice KeyboardDevice;
HidStream KeyboardStream;

HidSharp.Reports.ReportDescriptor KeyboardRptDescriptor;
HidSharp.Reports.Report KeyboardReport;
HidSharp.Reports.Input.HidDeviceInputReceiver InputReceiver;

OpenConfiguration KbdConnectCfg = new OpenConfiguration();

if (d.VendorID == 0x0000 &amp;&amp; d.ProductID == 0xA0A0)
{

    if (d.GetProductName() == &quot;Keyboard&quot;)
    {
         KeyboardDevice = d;
         KeyboardRptDescriptor =KeyboardDevice.GetReportDescriptor();            
     }

}

if (KeyboardDevice != null)
{
    /*Device Found*/
    if (KeyboardDevice.TryOpen(out KeyboardStream))    //This always returns false
    {
        KeyboardReport = KeyboardRptDescriptor.InputReports.FirstOrDefault();
        keyboardBuffer = new byte[KeyboardDevice.GetMaxInputReportLength()];
        InputParser = KeyboardReport.DeviceItem.CreateDeviceItemInputParser();
        InputReceiver = KeyboardRptDescriptor.CreateHidDeviceInputReceiver();
        InputReceiver.Start(KeyboardStream);
        InputReceiver.Received -= new EventHandler(HidInputReceived);
        InputReceiver.Received += new EventHandler(HidInputReceived);
    } 
    else
    {
        rtb_hidLog.AppendText(&quot;Unable to open Datastream\r\n&quot;);
    }
                
}
else
{
    rtb_hidLog.AppendText(&quot;No Device Found\r\n&quot;);
}</code></pre></div><p>The device is found with no problem and my KeyboardDevice object populates, but when I use TryOpen(), it always returns false and I don&#039;t get a datastream.&nbsp; Might someone be able to point out why this might be, or maybe how to get some more information to determine why I can&#039;t open the datastream to this device?&nbsp; The only thing I&#039;ve really tried is opening the device with the Exclusive and Transient options set:</p><div class="codebox"><pre><code>KbdConnectCfg.SetOption(OpenOption.Exclusive, true);
KbdConnectCfg.SetOption(OpenOption.Transient, true);
KeyboardDevice.TryOpen(KbdConnectCfg,out KeyboardStream)</code></pre></div><p> but it appears to have no effect, I still cannot get the datastream.</p><p>I&#039;m probably just missing something due to not really being familiar with HID from the host side.&nbsp; If there&#039;s any additional information I can provide that would be of help, let me know.&nbsp; Any thoughts are appreciated.</p><p>Thanks!</p>]]></content>
			<author>
				<name><![CDATA[KTrenholm]]></name>
				<uri>https://swforum.seekye.com/user/14273/</uri>
			</author>
			<updated>2019-05-28T16:45:14Z</updated>
			<id>https://swforum.seekye.com/post/13052/#p13052</id>
		</entry>
</feed>
