Y
Y
YouHim2013-05-02 11:42:20
Iron
YouHim, 2013-05-02 11:42:20

How to programmatically reconnect a USB device?

Hello.
There is a usb wi-max modem with which problems periodically arise - it freezes tightly. And I want the computer to be always in touch, since I often have to work with it remotely. I'm trying to write a script that would automatically reconnect the modem when the Internet goes down. To do this, I use the devcon.exe utility.
Below is an example of a script where the modem is reconnected, the service is restarted and the manager is connected. The problem is that often the modem hangs so much that devcon.exe cannot distort it. Saves only physical reconnection. The question is, is there another way to distort it? Maybe some more radical way? Soldering the device for a hard power interruption was left as a last resort :)

strComputer = "."
Set WshShell = WScript.CreateObject("WScript.Shell")
  Command ="d:\Portable\Script\restart_modem\devcon.exe disable USB\VID_1076*"
  Command1="d:\Portable\Script\restart_modem\devcon.exe enable USB\VID_1076*"
  Command2="d:\Portable\Script\restart_modem\devcon.exe restart USB\VID_1076*"
  strProcessKill = "'SW_WiMaxCM.exe'" 
  strProcessStart="d:\Portable\Script\restart_modem\SW_WiMaxCM.lnk"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next 

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & ".\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where Name ='GCTWiMAXService'")
For Each objService In colListOfServices
    objService.StopService  
  WshShell.Run Command
  WScript.Sleep 2000
  WshShell.Run Command1
  WScript.Sleep 5000
  WshShell.Run Command2
  t = now()
  Set FSO = CreateObject("Scripting.FileSystemObject") 
    	Set f = FSO.OpenTextFile("log.txt", 8, True) 
    	f.WriteLine t 
    	objService.StartService
  WshShell.Run strProcessStart
  Wscript.Echo  "GCT WiMAX USB Network Device restarted"

Next

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
miroslav, 2013-05-02
@miroslav

There are usb hubs with power management (for example DUB-H7 from dlink).
I don’t know how under windows, but under linux there is a simple hub-ctrl program (opensource, there are sources) that can turn ports off and on by hub numbers (it actually lowers the power to a minimum), although the port numbers change after a reboot and you have to turn it off the whole hub :-)

A
Alexey, 2013-05-06
@WildHorn

I have been tormenting a similar topic for a long time, and came to the conclusion that under Windows it is impossible to do this programmatically.
habrahabr.ru/qa/26579/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question