Mixing ActiveX with Java
by Al Williams

Example 1: 

ActiveXComponent xl = 
  new ActiveXComponent("Excel.Application");
System.out.println("version="+xl.getProperty("Version"));
xl.setProperty("Visible", new Variant(true));


Example 2: 

servo.invoke("SetPosition", 
  new Variant [] { new Variant(chan), 
                   new Variant(pos) });


Example 3:

(a)
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);

(b)
workbook.invoke("Close",
   new Variant [] { new Variant(false) });


Listing One

/* Java wrapper for GP-4 ActiveX DLL. Requires:
   JACOB -- http://danadler.com/jacob/
   GP4 -- http://www.awce.com/gp4.htm
*/
import com.jacob.com.*;
import com.jacob.activeX.*;
public class GP4
{
    private ActiveXComponent servo;  // The COM object
    // Create ActiveX object
    public GP4() 
    { 
        servo=new ActiveXComponent("AWCGP4DLL.GP4DLL");
    }
    // Reset servo controller
    public void reset() 
    { 
        servo.invoke("Reset",new Variant[] {} );
    }
    // Set servo position
    public void setPosition(int chan, int pos) 
    {
        servo.invoke("SetPosition", 
              new Variant [] { new Variant(chan), new Variant(pos) });
    }
    // Enable channel
    public void enableChannel(int chan, boolean enable) 
    {
        servo.invoke("EnableChannel", 
            new Variant[] { new Variant(chan), new Variant(enable) });
    }
    // Turn groups of servos on/off
    public void setMask(int mask)
    {
        // Another way to do this
        // servo.invoke("SetMask", new Variant [] { new Variant(mask) });
        Dispatch.call(servo,"SetMask",new Variant(mask));
    }
    // Enable/disable all servos
    public void enable(boolean enflag)
    {
        servo.invoke("Enable", new Variant [] { new Variant(enflag) });
    }
    // Set the COM port
    public void setComPort(int port)
    {
        servo.setProperty("Comport",new Variant(port));
    }
}


Listing Two
/* The robot driver */
public class ServoDrive
{
    // General-purpose pause
    {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {}
    }
    // Convert a string into a speed number
    private static int setSpeed(String v,int defspeed)
    {
      int speed;
      try {
          speed=Integer.parseInt(v);  // read integer
      }
      catch (Exception e) { speed=0; }
      if (speed==0) speed=defspeed;   // or use default
    return speed;
    }
    // The main code
  public static void main(String[] args)
  {
      GP4 servos=new GP4(); // create a servo controller
      int servoA=7;         // the wheels are on servo #7 and #6
      int servoB=6;
      // read speeds
      int speedA=setSpeed(args.length>0?args[0]:"",25);
      int speedB=setSpeed(args.length>1?args[1]:"",-speedA);
      // set the COM port
      servos.setComPort(1);
      // Turn servos on
      servos.enable(true);
      // Set them to run
      servos.setPosition(servoA,speedA);
      servos.setPosition(servoB,speedB);
      pause(3000);  // 3 seconds
     // disable all servos so they stop at once
      servos.setMask(0);
      // Reset both servos
      servos.setPosition(servoA,0);
      servos.setPosition(servoB,0);
      // And reenable them (although they are stopped now
      servos.setMask(0xFF);
 }
}







3


