我正试图用命令/ arduino从Minecraft发送一条消息通过COM4 (我的arduino所在的端口)消息必须是" allesfade" 我只使用java而不是在我的世界中使用它。
这是我的代码(不工作)
package me.PixelHamster.idk;
import java.io.IOException;
import java.io.PrintWriter;
import javax.comm.SerialPort;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
static SerialPort basispoort;
public void onEnable() {
System.out.println("suc6vol aangezet :3");
}
public void onDisable() {
System.out.println("sec6vol afgezet :3");
}
public boolean onCommand(CommandSender zender, Command cmd, String label, String[] argument) {
if (cmd.getName().equalsIgnoreCase("arduino")) {
try {
PrintWriter output = new PrintWriter(basispoort.getOutputStream());
output.print("allesfade");
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
这里是我编写的java中的代码。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.fazecast.jSerialComm.SerialPort;
public class Main {
static SerialPort chosenPort;
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Arduino Kleurenzender");
window.setSize(600, 75);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Sound.HAM.play();
JComboBox < String > portList = new JComboBox < String > ();
JComboBox < String > kleurselector = new JComboBox < String > ();
JComboBox < String > wat = new JComboBox < String > ();
JButton connectButton = new JButton("aan");
JPanel topPanel = new JPanel();
topPanel.add(portList);
topPanel.add(kleurselector);
topPanel.add(wat);
topPanel.add(connectButton);
window.add(topPanel, BorderLayout.NORTH);
SerialPort[] portNames = SerialPort.getCommPorts();
for (int i = 0; i < portNames.length; i++)
portList.addItem(portNames[i].getSystemPortName());
kleurselector.addItem("groen");
kleurselector.addItem("geel");
kleurselector.addItem("rood");
kleurselector.addItem("blauw");
kleurselector.addItem("alles");
wat.addItem("aan");
wat.addItem("flikkeren");
wat.addItem("fade");
connectButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) {
if (connectButton.getText().equals("aan")) {
chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if (chosenPort.openPort()) {
connectButton.setText("uit");
portList.setEnabled(false);
Thread thread = new Thread() {
@Override public void run() {
try {
Thread.sleep(100);
} catch (Exception e) {}
PrintWriter output = new PrintWriter(chosenPort.getOutputStream());
while (true) {
output.print(kleurselector.getSelectedItem() + "" + wat.getSelectedItem());
output.flush();
try {
Thread.sleep(2000);
} catch (Exception e) {}
}
}
};
thread.start();
}
} else {
chosenPort.closePort();
portList.setEnabled(true);
connectButton.setText("aan");
}
}
});
window.setVisible(true);
}
}
控制台中的错误
&#39; 4/04/2016 21:19:49 [INFO] PixelHamsters发出服务器命令:/ arduino
4/04/2016 21:19:49 [ERROR] null
4/04/2016 21:19:49 org.bukkit.command.CommandException:未处理的异常执行命令&#39; arduino&#39;在插件Arduino v1.0.0中
4/04/2016 21:19:49在org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)〜[spigot 1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49在org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)〜[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645)~ [spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49在net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.PlayerConnectionUtils $ 1.run(SourceFile:13)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)[?:1.8.0_31]
4/04/2016 21:19:49在java.util.concurrent.FutureTask.run(FutureTask.java:266)[?:1.8.0_31]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49在net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49在java.lang.Thread.run(Thread.java:745)[?:1.8.0_31]
4/04/2016 21:19:49引起:java.lang.NullPointerException
4/04/2016 21:19:49 at me.PixelHamster.idk.Main.onCommand(Main.java:34)〜[?:?]
4/04/2016 21:19:49在org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)〜[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 ...还有15个&#39;
答案 0 :(得分:0)
Intersting project:)
我会说问题是它是一个插件。插件在服务器上运行,而不是在客户端运行。所以你需要将arduino连接到服务器。如果你想从我的世界中控制一个arduino你将需要一个mod(在客户端运行)用于我的世界。
IDK,如果可行的话。
另一种方法是将arduino连接到Internet(Shield,或者使用Raspberry pi),然后让插件(在服务器上运行)通过互联网向该屏蔽/ pi发送消息。
不好说英语:(我希望这会有所帮助:)