Reputation: 147
I'm developing a C# application that logs data coming in on a USB serial-port. I'd like to be able to run it on a laptop, and have it continue to operate even when Windows 10 turns off the screen to save power. I've already taken several steps to make this happen:
(1) Call SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED) when the port is opened - see e.g. this answer. This seems to work, preventing Windows from going to sleep and keeping the application running.
(2) Disabled "USB Selective Suspend" in Windows' "Power & sleep settings" -> "Additional power settings" -> "Change plan settings" -> "Change advanced power settings" -> USB, for both "On battery" and "Plugged in".
(3) From Device Manager, disabled "Allow the computer to turn off this device to save power" in the "Power Management" settings of the USB-serial device.
And yet... when the "Screen turn-off" power-saving timeout elapses and the laptop screen goes dark, the serial-port closes! My program handles this as "device unplugged", but clearly it hasn't been. I've run Wireshark and used the "USB PCAP" feature to monitor the relevant USB bus, and I don't see any SUSPEND occurring.
So is this an undocumented feature of Windows Power-Management, or of the C# SerialPort class? Or is there a trick I've missed?
Upvotes: 0
Views: 497
Reputation: 147
False alarm! My function that detects Win32_DeviceChangedEvent was not discriminating exactly which device had changed. Modified it to check "serialPort1.IsOpen" property, and only shows "Device unplugged" if the port has been closed.
Upvotes: 0