Using Java to access SainSmart 4 Relay with Mac OS 10.8.5 doesn't work

蓝咒 提交于 2019-12-13 05:06:52

问题


I have the issue while using the SainSmart 4 Relay USB with my Mac (10.8.5). I am trying to access over Java while using the d2xx/fdti driver.

This is my Java Code:

public class Demo {

  public static void main(String[] args) throws InterruptedException {
     try {
        List<FTDevice> fTDevices;
        fTDevices = FTDevice.getDevices();
        for (FTDevice fTDevice : fTDevices) {

           System.out.println("fTDevice:" + fTDevice);
           System.out.println("fTDevice.DevType:" + fTDevice.getDevType());
           System.out.println("fTDevice.DevID:" + fTDevice.getDevID());
           System.out.println("fTDevice.DevLocationID:" + fTDevice.getDevLocationID());

           fTDevice.open();
           fTDevice.setBaudRate(9600);
           [...]
           fTDevice.close();
        }

     } catch (FTD2XXException ex) {
        Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
     }
  }
}

And I just get this exception:

com.ftdi.FTD2XXException: D2XX error, ftStatus:INVALID_HANDLE
at com.ftdi.FTDevice.ensureFTStatus(FTDevice.java:139)
at com.ftdi.FTDevice.setBaudRate(FTDevice.java:324)
at de.relay.Demo.main(Demo.java:27)

What does "INVALID_HANDLE" mean?

I also tried to connect over the serial port, which works.

# kextload -b com.FTDI.driver.FTDIUSBSerialDriver 
# cu -l /dev/tty.usbserial-XXXXXXX --baud 9600
Connected.

Which "echo" would switch a relay? And how can i fix the exception? I really appreciate any help!

Cheers, Felix


回答1:


I've encountered the same problem on Fedora 19.

The problem turned out to be an issue with JavaFTD2XX running on a 64-bit JVM. The handle appears to be a integer where it needs to be a long for 64-bit.

Switching to a 32-bit JRE got it working.

For a Mac, you might just be able to add the -d32 parameter to the JVM.



来源:https://stackoverflow.com/questions/20448032/using-java-to-access-sainsmart-4-relay-with-mac-os-10-8-5-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!