In a recent project, I used C# to call Realsense to implement some 3D measurement functions. But the connection stability of Realsense seems to have some problems. I encountered the following:
one The device will be disconnected, and the device object can continue to be used after re initialization in the software;
two After the device is disconnected, it can be initialized successfully, but the image cannot be collected. I solved this problem by automatically restarting the software
three The most troublesome problem is that sometimes the device can't be connected again after being disconnected. No matter using my own software or realsense viewer, it can't be connected to this realsense again, and even restarting the computer doesn't work. The problem can only be solved by re plugging and changing to another U port.
In order to solve this problem, I right-click the realsense device in the "universal serial bus device" column in the device manager, select "uninstall the device", and then right-click to select "scan to detect hardware changes". In this way, the equipment can be used.
But such operation is a little troublesome. If the product is delivered to the customer for use, the customer will not carry out such operation, it will be very embarrassing. So I need to implement these operations in the C# program.
In order to use the device manager, we first need to obtain devcon.exe. This tool can be understood as the command line version of the device manager. It is included in WDK, and a link to the official website is attached here Windows Device Console (Devcon.exe) - Windows drivers | Microsoft Docs
Go to the download address from the link on the official website Download the Windows Driver Kit (WDK) - Windows drivers | Microsoft Docs
According to the instructions on the official website, to install WDK, you need to install vs2019 first, and select C + + desktop program development in "workload". Fortunately, I have installed it here. If there are no comrades installed, you need to install vs2019 first.
After that, install the Windows SDK.
Finally, you can install WDK.
After installation, you can find devcon.exe under the path "C:\Program Files (x86)\Windows Kits\Tools\x64". There are many related instructions on how to use DevCon on the command line. You can try it on the command line first.
By using rescan parameters, the function of "scanning to detect hardware changes" can be realized.
Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "C:\\Program Files (x86)\\Windows Kits\\10\\Tools\\x64\\devcon.exe"; startInfo.Arguments = "-rescan"; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = false; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = false; process.StartInfo = startInfo; startInfo.Verb = "RunAs"; process.Start();
Deleting a device is a little more complicated. First, you need to obtain the device ID according to the device type. My equipment here uses SR305. To delete a device, you must specify the device ID. This ID can also be displayed when you right-click the device in the device manager, select properties, and then browse to the device instance path in the details column.
Use the code as follows.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity"); List<string> deviceIDs = new List<string>(); foreach (ManagementObject mgt in searcher.Get()) { if (Convert.ToString(mgt["Name"]) == "Intel(R) RealSense(TM) Depth Camera SR305") { Console.WriteLine(Convert.ToString(mgt["DeviceID"])); deviceIDs.Add(Convert.ToString(mgt["DeviceID"])); } }
But there are some questions here. I got two ID S through the C# code, but I really only inserted one realSense. I haven't carefully studied the specific meaning of this series of serial numbers here. If there is a great God to understand, please give me some advice.
After obtaining the ID, you can uninstall the device through the ID.
foreach (var item in deviceIDs) { string strOut = ""; Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "C:\\Program Files (x86)\\Windows Kits\\10\\Tools\\x64\\devcon.exe"; startInfo.Arguments = "-remove @" + item; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = false; startInfo.RedirectStandardOutput = true; startInfo.CreateNoWindow = false; process.StartInfo = startInfo; startInfo.Verb = "RunAs"; process.Start(); }
Here, the refresh of the device is completed. If you need to download my code, please click https://download.csdn.net/download/gaooolianggg/40119778