Wie kann ich Zyklus ein USB-Gerät von C #?
-
06-09-2019 - |
Frage
Ich möchte Zyklus (simulieren ausstecken und Wiedereinsetzen) ein USB-Gerät (Modem) nach einem bestimmten Ereignis ausgelöst hat. Ich fand eine Probe auf Codeproject:
Das ermöglicht es mir, + zu identifizieren, das Gerät über seinen nichtflüchtigen seriellen auswerfen, aber ich brauche es zu recyceln, nicht nur auszuwerfen.
Ich habe diese Zeilen lesen:
Ich verstehe es nicht.
Das ist in anderen USB-verwandten Beiträgen erwähnt:
Es ist mein Problem nicht relevant.
Lösung
habe es funktioniert durch ein Kommandozeilen-Tool namens devcon verwenden, die ich dann von Code aufgerufen.
Dropped devcon.exe in einer der Systempfad, so dass es überall funktioniert.
Devcon: devcon
genannt: DEVCON Remove * usb "* MI_01"
dann genannt: DEVCON Rescan
code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "DEVCON";
proc.StartInfo.Arguments = "Remove *usb"*MI_01";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
Andere Tipps
Sie können die C # Hardware Helper Lib verwenden und fügen Sie die Resetdevice Funktion .
So stellen Sie sicher, dass Sie fügen
public const int DICS_PROPCHANGE = ((0x00000003));
am public class Native
unter // PARMS,
public bool ResetDevice( IntPtr hDevInfo, IntPtr devInfoData )
{
int szOfPcp;
IntPtr ptrToPcp;
int szDevInfoData;
IntPtr ptrToDevInfoData;
Native.SP_PROPCHANGE_PARAMS pcp = new Native.SP_PROPCHANGE_PARAMS();
pcp.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Native.SP_CLASSINSTALL_HEADER));
pcp.ClassInstallHeader.InstallFunction = Native.DIF_PROPERTYCHANGE;
pcp.StateChange = Native.DICS_PROPCHANGE; // for reset
pcp.Scope = Native.DICS_FLAG_CONFIGSPECIFIC;
pcp.HwProfile = 0;
szOfPcp = Marshal.SizeOf(pcp);
ptrToPcp = Marshal.AllocHGlobal(szOfPcp);
Marshal.StructureToPtr(pcp, ptrToPcp, true);
szDevInfoData = Marshal.SizeOf(devInfoData);
ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData);
Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true);
bool rslt1 = Native.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Native.SP_PROPCHANGE_PARAMS)));
bool rstl2 = Native.SetupDiCallClassInstaller(Native.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
if (rslt1 && rstl2)
{
return true;
}
return false;
}