简体   繁体   English

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

[英]RXTX send string as ASCII through serial port?

I have this code using the RXTX library: 我有使用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();
    }
}

I am basing this off of the 2 way communication example. 我基于两路通信示例。 out1 (global variable) is set like this after the out variable has been set: 在设置out变量之后,像这样设置out1(全局变量):

out1 = out;

But when I try to write or flush, it gives me a nullPointerException at the line of the writing. 但是,当我尝试编写或刷新时,它在编写时给了我一个nullPointerException。 Here's my full code: 这是我的完整代码:

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...)");
        }
    }
}

(Don't worry about the commandSender stuff, this is a CraftBukkit mod, and if you don't know what it is, don't worry. Just know that when the program starts, startSerial is called, and when writing a string, sendSerial is called.) (不用担心commandSender的东西,这是一个CraftBukkit mod,如果您不知道它是什么,也不必担心。只要知道程序启动时,就会调用startSerial,并且在编写字符串时, sendSerial被调用。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM