我试图通过将模拟棒连接到Arduino并使用Eclipse在Java文件中处理发送的数据来创建一个简单的“游戏控制器”。以下是代码:
Arduino的:
int Ver = 0;
int Hor = 0;
int data[2];
void setup(){
Serial.begin(9600);
}
void loop(){
Ver = analogRead(A0); //511 D = 0
Hor = analogRead(A1); //515 L = 0 max = 1023
data[0] = Ver;
data[1] = Hor;
Serial.print("[");
Serial.print(data[0], DEC);
Serial.print(",");
Serial.print(data[1], DEC);
Serial.println("]");
delay(50);
}
爪哇:
package Controller;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class AnalogStick implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = { "/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM12", // Windows
};
private BufferedReader input;
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// First, Find an instance of serial port as set in PORT_NAMES.
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum
.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
System.out.println("Port ID: ");
System.out.println(portId);
System.out.println("");
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
serialPort.getOutputStream();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine = input.readLine();
//System.out.println("" + inputLine);
String[] items = inputLine.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
int[] data = new int[items.length];
for (int i = 0; i < items.length; i++) {
try {
data[i] = Integer.parseInt(items[i]);
} catch (NumberFormatException nfe) {};
}
//System.out.println("Ver: " + data[0] + " Hor: " + data[1]);
Robot robot = new Robot();
if(data[0] > 511 + 10 && data[1] < 515 + 169 && data[1] > 515 - 169){
System.out.println("Up");
robot.keyPress(KeyEvent.VK_W);
}
else if(data[0] < 511 - 10 && data[1] < 515 + 169 && data[1] > 515 - 169){
System.out.println("Down");
robot.keyPress(KeyEvent.VK_S);
}
if(data[1] < 515 - 10 && data[0] < 511 + 170 && data[0] > 511 - 170){
System.out.println("Left");
robot.keyPress(KeyEvent.VK_A);
}
else if(data[1] > 515 + 10 && data[0] < 511 + 170 && data[0] > 511 - 170){
System.out.println("Right");
robot.keyPress(KeyEvent.VK_D);
}
} catch (Exception e) {
System.err.println(e.toString());
//System.out.println("failed stuff");
}
}
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
}
}
public static void main(String[] args) throws Exception {
AnalogStick main = new AnalogStick();
main.initialize();
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(5000000);
} catch (InterruptedException ie) {
}
}
};
t.start();
System.out.println("- Started -");
System.out.println("");
}
}
我在eclipse上按下运行,它工作正常,但当我将鼠标指针移动到另一个屏幕或尝试在游戏中使用模拟棒(因此切换屏幕)时,我得到以下错误并且模拟棒停止在游戏中运作:
java.io.IOException:底层输入流返回零字节
我有一种感觉,这个错误是由于在模拟棒能够处理单个输入线之前将太多数据发送到PC造成的,但我不确定,因为这是我第一次使用模拟棒。有人可以教我吗
1)为什么它不起作用,因为我期望它工作
2)如何解决此错误
和
3)如果有更好的方法来编程模拟棒,请告诉我如何和/或向我展示参考网站
提前致谢