通过串行连接到 arduino 的 minecraft 插件
minecraft plugin to arduino over a serial connection
我正在尝试使用命令 /arduino 从 minecraft 通过 COM4 发送消息
(我的arduino所在的端口)消息必须是“allesfade”
我只用了 java 但在 minecraft 中不行。
这是我的代码(不工作)
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);
}
}
控制台错误
'4/04/2016 21:19:49 [INFO] PixelHamsters issued server command: /arduino
4/04/2016 21:19:49 [ERROR] null
4/04/2016 21:19:49 org.bukkit.command.CommandException: Unhandled exception executing command 'arduino' in plugin Arduino v1.0.0
4/04/2016 21:19:49 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot 1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at 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 at 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.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_31]
4/04/2016 21:19:49 at 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 at 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 at java.lang.Thread.run(Thread.java:745) [?:1.8.0_31]
4/04/2016 21:19:49 Caused by: java.lang.NullPointerException
4/04/2016 21:19:49 at me.PixelHamster.idk.Main.onCommand(Main.java:34) ~[?:?]
4/04/2016 21:19:49 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 ... 15 more'
有趣的项目:)
我会说问题是它是一个插件。插件在服务器上运行,而不是在客户端运行。所以你需要将 arduino 连接到服务器。如果你想从我的世界控制一个arduino,你需要一个mod(在客户端运行)用于我的世界。
IDK 如果那行得通。
另一种方法是将 arduino 连接到互联网(Shield,或使用 Raspberry pi),然后让插件(在服务器上运行)向 shield/pi 发送消息通过互联网。
抱歉英语不好:(
希望对您有所帮助:)
我正在尝试使用命令 /arduino 从 minecraft 通过 COM4 发送消息 (我的arduino所在的端口)消息必须是“allesfade” 我只用了 java 但在 minecraft 中不行。
这是我的代码(不工作)
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);
}
}
控制台错误
'4/04/2016 21:19:49 [INFO] PixelHamsters issued server command: /arduino
4/04/2016 21:19:49 [ERROR] null
4/04/2016 21:19:49 org.bukkit.command.CommandException: Unhandled exception executing command 'arduino' in plugin Arduino v1.0.0
4/04/2016 21:19:49 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot 1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at 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 at 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.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_31]
4/04/2016 21:19:49 at 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 at 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 at java.lang.Thread.run(Thread.java:745) [?:1.8.0_31]
4/04/2016 21:19:49 Caused by: java.lang.NullPointerException
4/04/2016 21:19:49 at me.PixelHamster.idk.Main.onCommand(Main.java:34) ~[?:?]
4/04/2016 21:19:49 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.jar:git-Spigot-7d15d07-c194444]
4/04/2016 21:19:49 ... 15 more'
有趣的项目:)
我会说问题是它是一个插件。插件在服务器上运行,而不是在客户端运行。所以你需要将 arduino 连接到服务器。如果你想从我的世界控制一个arduino,你需要一个mod(在客户端运行)用于我的世界。
IDK 如果那行得通。
另一种方法是将 arduino 连接到互联网(Shield,或使用 Raspberry pi),然后让插件(在服务器上运行)向 shield/pi 发送消息通过互联网。
抱歉英语不好:(
希望对您有所帮助:)