RXTX 通过串口将字符串作为 ASCII 发送?
Posted
技术标签:
【中文标题】RXTX 通过串口将字符串作为 ASCII 发送?【英文标题】:RXTX send string as ASCII through serial port? 【发布时间】:2014-12-27 10:15:34 【问题描述】:我有这个使用 RXTX 库的代码:
public void sendSerial(String msg) throws IOException
try
out1.write(msg.getBytes());
catch(Exception e)
e.printStackTrace();
try
out1.flush();
catch(Exception e)
e.printStackTrace();
我基于 2 路通信示例。 out1(全局变量)在设置 out 变量后设置如下:
out1 = out;
但是当我尝试写入或刷新时,它会在写入行给我一个 nullPointerException。这是我的完整代码:
package net.codepixl.serialPortBuk;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.ByteArrayInputStream;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Scanner;
import org.bukkit.command.CommandSender;
public class Serial
public Serial()
super();
OutputStream out1;
CommPort commPort;
CommPortIdentifier portIdentifier = null;
SerialPort serialPort;
void connect ( String portName, CommandSender s) throws Exception
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
System.out.println("Error: Port is currently in use");
s.sendMessage("Error: Port COM3 is currently in use");
else
commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
out1 = out;
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
else
public static class SerialWriter implements Runnable
OutputStream out;
public SerialWriter ( OutputStream out )
this.out = out;
public void run ()
public void sendSerial(String msg,CommandSender s) throws IOException
s.sendMessage("Sending "+msg+"...");
try
out1.write(msg.getBytes());
catch(Exception e)
e.printStackTrace();
s.sendMessage("Flushing output...");
try
out1.flush();
catch(Exception e)
e.printStackTrace();
s.sendMessage("Done!");
public void disconnect()
if (commPort != null)
try
// close the i/o streams.
commPort.getInputStream().close();
commPort.getOutputStream().close();
catch (IOException ex)
// don't care
// Close the port.
commPort.close();
/** */
public static class SerialReader implements Runnable
InputStream in;
public SerialReader ( InputStream in )
this.in = in;
public void run ()
byte[] buffer = new byte[1024];
int len = -1;
try
while ( ( len = this.in.read(buffer)) > -1 )
System.out.print(new String(buffer,0,len,"US-ASCII"));
catch ( IOException e )
e.printStackTrace();
public void startSerial(CommandSender s)
try
(new Serial()).connect("COM3",s);
catch ( Exception e )
s.sendMessage("COM port in use! (Maybe by this server...)");
(别担心commandSender的东西,这是一个CraftBukkit mod,如果你不知道它是什么,别担心。只要知道程序启动时调用了startSerial,写的时候一个字符串,调用 sendSerial。)
【问题讨论】:
你有可用的 NPE 堆栈跟踪吗? @ArnaudKleinveld 我唯一的堆栈跟踪是:Java.lang.NullPointerException 指向我写字符串的行。 我建议添加一些调试代码。例如从 runnables 开始,看看它们是否真的启动了: System.out.print("Starting SerialReader"); @ArnaudKleinveld 我已经这样做了;他们做到了。一切运行。我在每个方法的末尾添加了一条消息,说明它何时完成,以及应该完成的那些。 runnables 正在运行,它确实通过了 out1.flush(),但抛出了一个错误。 @ArnaudKleinveld 我现在觉得自己很蠢。我的 out1 不是静态的 XD 【参考方案1】:我只需要将 out1 变量设为静态 XD
【讨论】:
以上是关于RXTX 通过串口将字符串作为 ASCII 发送?的主要内容,如果未能解决你的问题,请参考以下文章