So I've bought a USB 4 port Relay Module that has been shipped and is on its way.
Here is the link to the product: Denkovi Assembly Electronics ltd: USB Four Channel Relay Board for Automation
Meanwhile I'm waiting for it I thought I'd maybe prepare a simple application in Java for it however I've encountered some trouble.
I asked the supplier for a java source code example and got this:
Firstly, please donwload the library I have attached. It is documented here - http://bleyer.org/jd2xx/
Secondly, please use the following code I am sending to you.
I have attached the library (jd2xx.jar inside the jd2xx.zip) he provided me with that email.//Create a JD2XX object usb4Relay = new JD2XX(); //Use some open method usb4Relay.open(0); //Adjust the bit-bang mode usb4Relay.setBitMode(255, 1); //Set the relays String RelayState="1111" //prepare the state for the relays String State=""; for (int i=RelayState.length; i>0; i--) if (RelayState[i-1]==0) State=State+"00"; else State=State+"10"; // Now we have State=10101010. We are using bits 1,3,5,7 bits from FT245 for controlling the relays. usb4Relay.write(Integer.parseInt(State, 2)); //Finally, close it usb4Relay.close();
I found some mistakes in his code and made some minor changes to it:
import java.io.IOException; import jd2xx.JD2XX; class Main { public static void main(String[] args) throws IOException { // Create a JD2XX object JD2XX usb4Relay = new JD2XX(); // Use some open method usb4Relay.open(0); // Adjust the bit-bang mode usb4Relay.setBitMode(255, 1); // Set the relays String RelayState = "1111"; // prepare the state for the relays char[] chars = RelayState.toCharArray(); String state = ""; for (int i = RelayState.length(); i > 0; i--) if (chars[i - 1] == 0) state = state + "00"; else state = state + "10"; // Now we have State=10101010. We are using bits 1,3,5,7 bits from FT245 // for controlling the relays. usb4Relay.write(Integer.parseInt(state, 2)); // Finally, close it usb4Relay.close(); } }
While running the application in Eclipse I receive the following errors:
Code for JD2XX.java:587 says:Exception in thread "main" java.lang.UnsatisfiedLinkError: no jd2xx in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at jd2xx.JD2XX.<clinit>(JD2XX.java:587)
at Main.main(Main.java:10)
So what am I doing wrong here?586: static {
587: System.loadLibrary("jd2xx");
588: }