2016-04-05 68 views
0

我想通過命令/ arduino發送來自我的世界的COM4的消息 (我的arduino所在的端口)消息必須是「allesfade」 我得到了它的工作只有java而不是我的世界。通過串口連接到arduino的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); 
} 

} 

在控制檯

「2016年4月4日21點19分49秒[INFO] PixelHamsters發出服務器命令的錯誤:/ Arduino的

2016年4月4日21: 19時49分[ERROR]空

2016年4月4日21點19分49秒org.bukkit.command.CommandException:未處理的異常在插件的Arduino V1.0.0執行命令 '的Arduino'

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]

2016-04-04 21 :19:49 at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645)〜[spigot-1.9.jar:git-Spigot-7d15d07-c194444]

2016/4/4 21: 19:49 at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302)[spigot-1.9.jar:git-Spigot-7d15d07-c194444]

2016年4月4日21點19分49秒在net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137)[插口1.9.jar:GIT-插口7d15d07-c194444]

2016年4月4日21點十九分49秒在net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)[插口1.9.jar:GIT-插口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]

2016年4月4日二十一時19分49秒在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511):1.8.0_31]

2016年4月4日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 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 ]

2016/4/4 21:19:49引起:java.lang.NullPointerException

2016年4月4日21點19分49秒在me.PixelHamster.idk.Main.onCommand(Main.java:34)〜[:?]

2016年4月4日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多個」

回答

0

野趣項目:)

我要說的是,問題是,它是一個插件。插件在服務器上運行,而不在客戶端運行。所以你需要將arduino連接到服務器。如果你想從Minecraft控制一個arduino,你需要一個mod(在客戶端運行)爲我的世界。

IDK如果可以的話。

另一種方法可能是將arduino連接到Internet(Shield或Raspberry pi),然後讓插件(在服務器上運行)通過互聯網向該屏蔽/ pi發送消息。

SRY的英語不好:(

我希望這有助於:)

+0

好,但我嘗試使它的第一部作品只是在本地服務器上 – ToxicMushroom

+0

然後它可以工作,但心不是在它應該的方式。 –

相關問題