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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM