7

I see post after post after post asking how to sleep a USB port. What I don't see anywhere is direction on how to prevent such.

I have a C# program that operates a piece of hardware that is a literal black box. I have zero control over the physical internals. Unfortunately, if the USB port sleeps, even for the tiniest fraction of a second, the device loses its state and all current work is lost.

I need a way to make absolutely sure that the USB ports never ever ever sleep while my app is running. Even if no data has been sent to the port and even if the user doesn't touch the computer for days at a time.

A few things:
1: It's not necessary to keep the port wakelocked only while the application is running. Permanently disabling USB sleep would actually be preferred.

2: Disabling other power saving features in Windows is acceptable.

3: Simply asking customers to disable USB sleeping is not acceptable. Many are not tech savvy enough to follow written directions for such and we lack the manpower to walk hundreds and hundreds of users through it.

4: Using the computer for anything except our software technically violates the license. Our license also reserves us the right to alter the Windows configuration without user knowledge or consent. As such, disabling USB sleeping silently and transparently is strongly preferred.

KatDevsGames
  • 1,109
  • 10
  • 21
  • 2
    Note that the *correct* solution is for the device driver, not the application, to tell Windows that the device must remain powered on. (Though I'd have thought that should be the default behaviour anyway?) – Harry Johnston Nov 04 '15 at 00:52
  • I'd have thought so too actually but it doesn't seem to be the case. Also, the "driver" is a super generic USB serial driver. I can do anything the FTDI D2XX dll permits me to do. I can turn the port off... windows seems insistent on saving power though, even when the computer is plugged in. – KatDevsGames Nov 04 '15 at 06:00
  • I really can arbitrarily mess with the computers though, I think we're giving them away when you order the black box which costs something like $25k up front and $700/yr thereafter so the computer itself we can consider effectively disposable. – KatDevsGames Nov 04 '15 at 06:00
  • At any rate, I can only work within the boundaries I am given. I agree that the device should do this but it doesn't. I'm just one cog in a larger system but this problem has been made my problem and all I can control are a bunch of .NET dlls. I literally don't have write access to anything else and nothing I say to anyone will make this not my problem. – KatDevsGames Nov 04 '15 at 06:00
  • The fact that it is a USB-to-serial device should probably be edited into the question. Would preconfiguring the computer be an option, or do you need to retroactively fix this problem on units that have already shipped? You imply that you know how to fix the problem via the GUI; what exactly do you do? In particular, does it resolve the problem if you set the "USB selective suspend setting" to Disabled (in the Advanced Settings of the Power Options control panel)? – Harry Johnston Nov 04 '15 at 09:18
  • Yes, that setting I believe is the one. No, the computers go directly from the vendor to the customer so no preconfiguration. That was my 2nd requirement above. – KatDevsGames Nov 04 '15 at 14:15
  • OK, since that's a global rather than a per-device power setting you should be able to configure it by shelling out to `powercfg.exe` (the easy way out, since you can experiment on the command line) or with the [Power Management API](https://msdn.microsoft.com/en-us/library/windows/desktop/bb968807(v=vs.85).aspx) (more elegant). If you want to take the latter route, and assuming nobody posts an already-working code sample, I recommend you figure out how to do it in plain C first, and only then port it to C# via P/Invoke. – Harry Johnston Nov 04 '15 at 19:40
  • There's probably already a library for the api. If not, I'm no stranger to p/invoke with this project as it is. The hardware itself is accessed through some random C dll. I'll report back what I find, with relevant code if nobody else does by the time I check it out. – KatDevsGames Nov 04 '15 at 23:40
  • @JoshuaPech did you find anything out on this? We have the same issues. – ToddK May 13 '16 at 19:39
  • @ToddK, nothing in .NET. Our installer already requires admin priv to run in the first place so I actually ended up modifying our installer to permanently disable all power saving systemwide and letting it roll out with the next automatic update. – KatDevsGames May 14 '16 at 02:38

0 Answers0