VM140 Solution for JAVA-Programmers

Hi there

This post is for JAVA-Programmers.
I found an easy solution to access the VM140/K8061 card with JAVA. It is easy to do the same with other cards.
You have to download the JNative-Library from jnative.free.fr for accessing DLL’s from Java-Code dynamically.
This is still not alway easy to use, mainly because of passing of parameteres to the DLL. So I wrote a wrapper-class (VM140.java). For testing I use a JUnit-Java-Test-case (VM140Test.java). Both ist tested with JAVA6 running on a VISTA-32-Bit-PC.
Hope it helps some of the folks.
randb.

package test;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;


/**
 * Hält einiges zentrales und ist die Facade zur USB-Schnittstellenkarte VM140 
 * @author randb
 * DEMO-Version für FORUM
 * 08.04.2010
 */
public class VM140 {

	private static final short INVALID_CARDADRESS = -1;
	private static final String VM140DLL_NAME = "K8061.dll";
	private static final int VM140_IMPULSE_DURATION = 500;		// in mSecs

	private short mCardAdress = INVALID_CARDADRESS;
	
	
	public VM140() {
		super();
	}

	//@deprecated
	public static JNative getDLLObject(String functionName) throws NativeException {
		return new JNative(VM140DLL_NAME, functionName);
	}
	
	private String getParam(short param) {
		return "" + param;
	}
	
	public boolean hasCardNumber() {
		return (mCardAdress != INVALID_CARDADRESS);
	}
	
	private JNative createDLLObject(String pCommandName) throws Exception {
		System.out.println("**\\** createDLLObject(); pCommandName=" + pCommandName);
		JNative dllObject = null;
	 	try {
			dllObject = new JNative(VM140DLL_NAME, pCommandName);
		} catch (NativeException e) {
			throw new Exception("VM140 failed getting command=" + pCommandName, e);
		}
		return dllObject;
	}

	private Pointer createPointer(int pSizeInBytes) throws Exception {
		try {
			return new Pointer(MemoryBlockFactory.createMemoryBlock(pSizeInBytes));
		} catch (NativeException e) {
			throw new Exception("VM140 failed creating pointer of size=" + pSizeInBytes, e);
		}
	}
	
	private String pointerToString(Pointer pointer) throws NativeException {
		 int pointerSize = pointer.getSize();
		 byte[] validBytes = new byte[pointerSize / 4];
		 byte charByte = 0;
		 for (int i=0; i<validBytes.length; i++) {
			 charByte = pointer.getAsByte(4*i);
			 validBytes[i] = charByte;
		 }
		 return new String(validBytes);
	 }
	 

	private boolean checkBoolean(String retValue) {
		return "1".equalsIgnoreCase(retValue);
	}
	
	private void sleep() {
		try {
			Thread.sleep(VM140_IMPULSE_DURATION);
		} catch (InterruptedException e) {
			System.out.println("sleeping was interrupted " + e.getMessage());
		}
	}

	/**
	 * Oeffnet eine Verbindung zum VM140
	 * Speichert die Adresse in der Instanz-Var mCardAdress
	 * @return boolean --> vernünftige Karten-Adresse gefunden
	 */
	 public boolean openDevice() throws Exception {
		 	JNative dllObject = createDLLObject("OpenDevice");
		 	try {
				dllObject.setRetVal(Type.INT);
		        dllObject.invoke();
		        String returnValue =  dllObject.getRetVal();
		        mCardAdress = (new Integer(returnValue)).shortValue();
		        System.out.println("**\\** end of openDevice(); returnValue=" + returnValue + " mCardAdress=" + mCardAdress);
			} catch (Exception e) {
				throw new Exception("testing connection to VM140 failed", e);
			}
			return hasCardNumber();
	   }	

	 public void closeDevices() throws Exception {
		 	JNative dllObject = createDLLObject("CloseDevices");
		 	try {
		        dllObject.invoke();
		        System.out.println("**\\** end of closeDevices();");
			} catch (Exception e) {
				throw new Exception("closing devices of VM140 failed", e);
			} finally {
				mCardAdress = INVALID_CARDADRESS;
			}
	   }	

	 public boolean isConnected() throws Exception {
		 	boolean connected = false;
		 	JNative dllObject = createDLLObject("Connected");
		 	try {
				dllObject.setRetVal(Type.INT);
			 	dllObject.setParameter(1, Type.INT, getParam(mCardAdress));
		        dllObject.invoke();
		        String returnValue =  dllObject.getRetVal();
		        connected = checkBoolean(returnValue);
		        System.out.println("**\\** end of isConnected(); returnValue=" + returnValue + " connected=" + connected);
			} catch (Exception e) {
				throw new Exception("testing connection to VM140 failed", e);
			}
	        return connected;
	   }	

	 public boolean isPowerGood() throws Exception {
		 	boolean powerGood = false;
		 	JNative dllObject = createDLLObject("PowerGood");
		 	try {
				dllObject.setRetVal(Type.INT);
			 	dllObject.setParameter(1, Type.INT, getParam(mCardAdress));
		        dllObject.invoke();
		        String returnValue =  dllObject.getRetVal();
		        powerGood = checkBoolean(returnValue);
		        System.out.println("**\\** end of isPowerGood(); returnValue=" + returnValue + " powerGood=" + powerGood);
			} catch (Exception e) {
				throw new Exception("testing power of VM140 failed", e);
			}
	        return powerGood;
	   }
	 
	 /**
	  * Holt den Versionsstring
	  * @return Versionsstring, NULL oder leerer String "" falls nicht ok 
	  * @throws Exception
	  */
	 public String readVersion() throws Exception {
		 	String version = null;
		 	JNative dllObject = createDLLObject("ReadVersion");
		 	Pointer bufferPointer = null;
		 	try {
			 	dllObject.setParameter(0, Type.INT, getParam(mCardAdress));
		 		bufferPointer = createPointer(50 * 4);		// 50 long integers 
		 		dllObject.setParameter(1, bufferPointer.getPointer());
			 	dllObject.invoke();
			 	version = pointerToString(bufferPointer);
			 	System.out.println("**\\** end of readVersion(); version=" + version);
			} catch (Exception e) {
				throw new Exception("reading version of VM140 failed", e);
			} finally {
				try {
					bufferPointer.dispose();
				} catch (Throwable ignore) {
					System.out.println("failed disposing pointer " + ignore.getMessage());
				}
			}
	        return version;
	   }	

	 	public void clearAllDigital() throws Exception {
		 	JNative dllObject = createDLLObject("ClearAllDigital");
		 	try {
			 	dllObject.setParameter(0, Type.INT, getParam(mCardAdress));
		        dllObject.invoke();
		        System.out.println("**\\** end of clearAllDigital()");
			} catch (Exception e) {
				throw new Exception("clearing all digital outputs of VM140 failed", e);
			}
	     
	   }	

	 	public void setDigital(int channel) throws Exception {
	 		short channelShort = new Integer(channel).shortValue();
		 	JNative dllObject = createDLLObject("SetDigitalChannel");
		 	try {
			 	dllObject.setParameter(0, Type.INT, getParam(mCardAdress));
			 	dllObject.setParameter(1, Type.INT, getParam(channelShort));
		        dllObject.invoke();
		        System.out.println("**\\** end of setDigital(); channel=" + channel);
			} catch (Exception e) {
				throw new Exception("setting digital output of VM140 failed for channel =" + channel, e);
			}	     
	   }	

	 	public void clearDigital(int channel) throws Exception {
	 		short channelShort = new Integer(channel).shortValue();
		 	JNative dllObject = createDLLObject("ClearDigitalChannel");
		 	try {
			 	dllObject.setParameter(0, Type.INT, getParam(mCardAdress));
			 	dllObject.setParameter(1, Type.INT, getParam(channelShort));
		        dllObject.invoke();
		        System.out.println("**\\** end of clearDigital(); channel=" + channel);
			} catch (Exception e) {
				throw new Exception("clearing digital output of VM140 failed for channel =" + channel, e);
			}	     
	   }	

	 	/**
	 	 * Gibt einen Impuls auf den digitalen Kanal, d.h. aktiviert und deaktiviert ihn wieder
	 	 * @param channel
	 	 * @throws Exception
	 	 */
	 	public void impulseDigital(int channel) throws Exception {
	 		System.out.println("**\\** impulseDigital(); channel=" + channel);
	        try {
				setDigital(channel);
				sleep();
			} catch (Exception ignore) {
				System.out.println("setting digital chennel failed");
			} finally {
				try {
					clearDigital(channel);
				} catch (Throwable ignore) {
					System.out.println("clearing digital output failed " + ignore.getMessage());
					clearAllDigital();
				}				
			}
	   }
	 	
}


package test;

//import ch.randb.vm140.VM140;
import junit.framework.TestCase;

public class VM140Test extends TestCase {

	private static VM140 vm140 = new VM140(); 
	
	public void test01() {
		try {
			System.out.println("VM140Test > openDevice");
			assertTrue(vm140.openDevice());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}

	public void test02() {
		try {
			System.out.println("VM140Test > isConnected");
			assertTrue(vm140.isConnected());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}
	
	public void test03() {
		try {
			System.out.println("VM140Test > isPowerGood");
			assertTrue(vm140.isPowerGood());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}

	public void test04() {
		try {
			System.out.println("VM140Test > readVersion");
			String version = vm140.readVersion();
			assertTrue(version.length() > 0);
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}


	public void test05() {
		try {
			System.out.println("VM140Test > setDigital(1)");
			vm140.setDigital(1);
			assertTrue(vm140.hasCardNumber());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}

	public void test06() {
		try {
			System.out.println("VM140Test > clearDigital(1)");
			vm140.clearDigital(1);
			assertTrue(vm140.hasCardNumber());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}
	public void test07() {
		try {
			System.out.println("VM140Test > impulseDigital(2)");
			vm140.impulseDigital(2);
			assertTrue(vm140.hasCardNumber());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}

	public void test08() {
		try {
			System.out.println("VM140Test > clearAllDigital");
			vm140.clearAllDigital();
			assertTrue(vm140.hasCardNumber());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}		
	}

	public void test99() {
		try {
			System.out.println("VM140Test > closeDevices");
			vm140.closeDevices();
			assertTrue(! vm140.hasCardNumber());
		} catch (Exception ex) {
			ex.printStackTrace();
			assertTrue(false);
		}
	}

}

I’m beginner at java programming please help me step by step how to use java to interface with this k8061
I already bought the card and want to use Java programming
Thanx

Try learning JAVA first before doing more complicated things