Linux 下开源RXTX库的配置与使用

由于项目需要再Linux下读取串口并进行相关操作。

一般串口操作(Linux)都是用C语言编写的,因为更加底层,也更安全,更易编写,但是由于项目是java的,所以就要使用java的相关库来实现。

我在这里使用的是RXTXcomm.jar。

http://rxtx.qbang.org/wiki/index.php/Main_Page

这里有作者关于各个操作系统下的配置问题,由于我这里只用在Linux下使用,所以也就说一下在linux的配置问题。

首先下载RXTXcomm

INSTALL是简单的安装说明,然后还有就是在四个操作系统下的配置文件以及jar包。

在Linux下

这里是针对不同的Linux版本的不同配置文件。

然后进入 $JAVA_HOME中,具体配置如下:

 在JAVA_HOME/jre/lib中,有一个配置文件,amd64,这个文件可能不一样,然后在RXTXcomm中找到与自己系统相对应的配置文件。然后将里面的.so 文件放入amd64中。

然后再进入JAVA_HOME/jre/lib/ext中,将之前的jar包放入该文件夹中,配置就已经完成了。

然后附上一个简单的例子。查看端口并输出端口信息。

package init;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class eee
{
    static String  defaultPort = "/dev/ttyS1"; //linux
    public static void main ( String[] args )
    {
        
        listPorts();
        try
        {
            (new test()).connect(defaultPort);
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    
    public eee()
    {
        super();
    }
    
    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                
                InputStreamReader reader = new InputStreamReader(in);
                BufferedReader r = new BufferedReader(reader);
                
                (new Thread(new SerialReader(r))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }
    
    /** */
    public static class SerialReader implements Runnable 
    {
        BufferedReader in;
        
        public SerialReader ( BufferedReader in )
        {
            this.in = in;
        }
        
        public void run ()
        {
            try
            {
                String line;
                while ((line = in.readLine()) != null){
                    System.out.println(line);
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;
        
        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }
        
        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }
    
    static void listPorts()
    {
        System.out.println("all ports!!!");
        @SuppressWarnings("unchecked")
        java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while ( portEnum.hasMoreElements() ) 
        {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            System.out.println(portIdentifier.getName()  +  " - " +  getPortTypeName(portIdentifier.getPortType()) );
        }        
    }
    
    static String getPortTypeName ( int portType )
    {
        switch ( portType )
        {
            case CommPortIdentifier.PORT_I2C:
                return "I2C";
            case CommPortIdentifier.PORT_PARALLEL:
                return "Parallel";
            case CommPortIdentifier.PORT_RAW:
                return "Raw";
            case CommPortIdentifier.PORT_RS485:
                return "RS485";
            case CommPortIdentifier.PORT_SERIAL:
                return "Serial";
            default:
                return "unknown type";
        }
    }
    
    
}
原文地址:https://www.cnblogs.com/xiaoba1203/p/5787568.html