Pages

USB plug/unplug automation

The USB plug/unplug process can be easily automated using a USB power relay. In this example I am using the Phidget board 0/0/4 from http://www.phidgets.com/. All you need is to take one of the voltage lines from the USB cable and connect it to the Phidget board in one of the relay ports. The Phidget board can be controlled by a Host using the Phidget drivers. The plug/unplug simulation will occur once you close the circuit in the USB relay port.


Here is small app to control the Phidget board named phtest. 

How to compile?
% gcc phtest.c -o phtest -l phidget21

 /*  
  * phtest demo to control phidget board 0/0/4  
  * Usage: phtest <port> <status>  
  */  
 #include <stdio.h>  
 #include <stdlib.h>  
 #include <unistd.h>  
 #include <phidget21.h>  
 // the following example will turn on or off a Phidgets Output, such as a relay  
 // or IO device pin  
 int IOcontrol(int whichoutput, int value)  
 {  
      int result;  
      int numInputs = 0;  
      int numOutputs = 0;  
      CPhidgetInterfaceKitHandle ifk;     // the "handle" to the interface kit  
      // initialize the handle  
      CPhidgetInterfaceKit_create (&ifk);  
      // open access to the Phidgets hardware  
      CPhidget_open((CPhidgetHandle)ifk, -1);  
      // wait a max of 2 seconds (for example) before reporting  
      // an attachment error  
      if (result = CPhidget_waitForAttachment((CPhidgetHandle)ifk, 2000))     {  
           const char *err;  
           char buf[1000];  
           // get description of error as string, and output  
           CPhidget_getErrorDescription (result, &err);  
           sprintf (buf, "error: %s\n", err);  
           // output with a message box if you like ...  
           return -1;  
      }  
      // if interested, you can query the interface to  
      // determine how many inputs or outputs it may have  
      CPhidgetInterfaceKit_getNumInputs (ifk, &numInputs);  
      CPhidgetInterfaceKit_getNumOutputs (ifk, &numOutputs);  
      // adjust the output status of the device  
      CPhidgetInterfaceKit_setOutputState (ifk, whichoutput, value);  
      // cleanup  
      CPhidget_close ((CPhidgetHandle)ifk);  
      CPhidget_delete ((CPhidgetHandle )ifk);  
      return 0;  
 }     /* end IOcontrol */  
 int main( int argc, char *argv[] )  
 {  
   int port, status;  
   if (argc !=3) {  
     printf("\nUsage: %s <port> <status>\n\n", argv[0]);  
     printf("Where port=0|1|2|3 --> USB port relay\n");  
     printf("   status=0|1  --> 0=OFF 1=ON\n\n");  
     return 0;  
   }  
   port = atoi(argv[1]);  
   status = atoi(argv[2]);  
   IOcontrol (port, status);  
      return 0;  
 }  

Usage: ./phtest <port> <status>

Where port=0|1|2|3  --> USB port relay
      status=0|1    --> 0=OFF 1=ON

1 comment:

  1. Casinos Near Casinos in Washington DC - MapyRO
    Top 10 Casinos 서산 출장안마 in Washington 천안 출장안마 DC. A map showing casinos and other gaming facilities located 충주 출장안마 near the Washington 경상북도 출장마사지 DC Casino. 보령 출장샵 Mapyro users can also find

    ReplyDelete