RXTX通过串口发送字符串为ASCII?

时间:2014-12-27 10:15:35

标签: java bukkit rxtx

我使用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。)

1 个答案:

答案 0 :(得分:0)

我只需要将out1变量设为静态XD

相关问题