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

}
时间: 2024-08-04 22:18:05

Linux 下开源RXTX库的配置与使用的相关文章

在Window和Linux下使用Zthread库

ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread.sourceforge.net 最新版本Zthread远吗下载地址: http://prdownloads.sourceforge.net/zthread/ZThread-2.3.2.tar.gz ZThread文档:http://zthread.sourceforge.net/documentat

Linux下Mongodb数据库主从同步配置

说明: 有两台已经安装完成的Mongodb数据库服务器,现在需要把一台设置为主库,另一台设置为从库,实现主从同步. 操作系统:CentOS 7.0 64位 MongoDB数据库版本:mongodb-linux-x86_64-2.6.5 准备工作:MongoDB数据库安装 具体操作: 一.配置MongoDB主库 以下操作在MongoDB主库服务器上进行 1.cd  /usr/local/mongodb/ #进入MongoDB安装目录 vi /usr/local/mongodb/mongodb.co

Linux下Qt的安装与配置

参考资料:http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html Linux 下编译.安装.配置 QT 下载qt 这里用的是4.7.0版本 qt-everywhere-opensource-src-4.7.0.tar.gz 拷贝并解压 这里我装的是Vmware上面的linux,所以windows与linux直接的文件共享,建议用samba,Samba我前面的笔记有介绍,这里不详谈. 拷贝到下面这个目录下 解压用 : tar zx

linux下mysql数据库主从同步配置

说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备篇: 说明:在两台MySQL服务器192.168.21.128和192.168.21.129上分别进行如下操作 备注: 作为主从服务器的MySQL版本建议使用同一版本! 或者必须保证主服务器的MySQL版本要高于从服务器的MySQL版本! 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接

linux下so动态库一些不为人知的秘密

linux 下有动态库和静态库,动态库以.so为扩展名,静态库以.a为扩展名.二者都使用广泛.本文主要讲动态库方面知识.    基本上每一个linux 程序都至少会有一个动态库,查看某个程序使用了那些动态库,使用ldd命令查看 # ldd /bin/ls linux-vdso.so.1 => (0x00007fff597ff000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00000036c2e00000) librt.so.1 => /

Linux下 静态链接库 和 动态链接库

先来说说C/C++编译过程 编译: 检查语句符号定义,将C/C++代码翻译生成中间语言. 链接: 将中间代码整合,生成可执行的二进制代码. 简单的说,库文件都是一种特殊的中间语言文件,静态库还是一种特殊格式的归档文件(打包的文件). 使用静态库: 1. 先编写库函数 1 #ifndef _PRINT_TEST_H_ 2 3 #define _PRINT_TEST_H_ 4 #ifdef __cplusplus 5 extern "C" 6 { 7 #endif 8 9 extern i

linux 下安装 jdk tomcat 并配置

linux下安装TOMCAT与JDK 第1步:新建boss用户,用户名:boss,密码:boss 第2步:将安装用户路径设置成/app/boss [[email protected]]# useradd-d /app/boss -m boss [[email protected]]# passwdboss Changing password for user boss. New password:  boss passwd: all authentication tokens updated s

linux下的静态库创建与查看,及如何查看某个可执行依赖于哪些动态库

linux下的静态库创建与查看,及如何查看某个可执行依赖于哪些动态库 创建静态库:ar -rcs test.a *.o查看静态库:ar -tv test.a解压静态库:ar -x test.a 查看程序依赖的动态库:readelf -a xxx|grep library如:可以看到,下面的交叉程序hello执行依赖于如下两个动态库.[email protected]:~/test$ arm-none-linux-gnueabi-readelf -a hello|grep "library&quo

linux下安装oracle需要的配置

1.检查系统包安装情况 rpm -qa|grep binutils rpm -ivh sysstat-7.0.2.rpm binutils-2.17.50.0.6 compat-libstdc++-33-3.2.3 compat-libstdc++-33-3.2.3 (32 bit) elfutils-libelf-0.125 elfutils-libelf-devel-0.125 gcc-4.1.2 gcc-c++-4.1.2 glibc-2.5-24 glibc-2.5-24 (32 bit