1

我正在尝试为用于相机控制的通用键盘开发应用程序。我能够通过 /dev/ttyACM0 伪终端设备在 Linux 上以某种基本方式与设备通信。但据我所知,这在 Windows 中不存在,当我检查 comm API 和 rxtx 库的示例时,我有点困惑。

我怎样才能像在 Linux 上那样简单地与这个设备通信?这是键盘的说明手册:http: //www.videotec.com/dep/DCZ_1051.pdf

这是代码:

Class1.java

package customkeyboard;

import java.io.FileInputStream;
import org.apache.commons.codec.binary.Hex;

public class CustomKeyboard {

    public static void main(String[] args) {
        // TODO code application logic here
        FileInputStream in = null;
        try {
            in = new FileInputStream("/dev/ttyACM0");
            int c = 0;

            int streamCounter = 0;
            String command = "";
            CustomKeyboard2 key2 = new CustomKeyboard2();
            key2.main(null);
            while ((c = in.read()) != -1) {
                if (streamCounter < 6) {
                    //System.out.print( + " - ");
                    command += Integer.toHexString(c);
                    streamCounter++;
                    if (streamCounter == 6) {
                        streamCounter = 0;
                        System.out.println(command);
                        byte[] bytes = Hex.decodeHex(command.toCharArray());
                        String deastranza = new String ( bytes, "UTF-8");
                        System.out.println(deastranza);
                        command = "";
                }
            } else {
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

Class2.java

package customkeyboard;

import java.io.FileOutputStream;
import org.apache.commons.codec.binary.Hex;

public class CustomKeyboard2 {
    public static void main(String[] args) {
        try {
            FileOutputStream out = null;
            out = new FileOutputStream("/dev/ttyACM0");
            String command = "[LedImmediate]";
            byte[] message = command.getBytes("UTF-8");
            out.write(message);

            command = "[Led+45]";
            message = command.getBytes();
            out.write(message);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
4

0 回答 0