CONMISANMA的正确答案——JAVA使用RXTX读写ARDUINO串口(COM口)

部分内容转载自:使用Java实现简单串口通信

测试系统:DEEPIN LINX 15.6

测试软件:NETBEANS 8.2 & arduino 1.8.4 

测试硬件:ARDUINO UNO R3

JAVA的conn包依旧很久都不更新了,所以这里使用的是RXTX包

(官方)下载地址:

官网:RXTX for Java

(我只能进入那个官网,迅雷有时能下载,听说要FQ,没能FQ的同学可以用下面的原作者的博客园地址,linux的话要看运气,运气好的点多几次就能下了)

Version File Information
RXTX-2-2-20081207 Windows-x64

Windows-x86
Windows-ia64
Linux-x86_64
Linux-i386

Based on CVS snapshot of RXTX taken on 2008-12-07

(原版主)下载地址:

http://files.cnblogs.com/files/Dreamer-1/mfz-rxtx-2.2-20081207-win-x86.zip (32位)

http://files.cnblogs.com/files/Dreamer-1/mfz-rxtx-2.2-20081207-win-x64.zip (64位)

本文提供的下载地址:

LINUX i386       (CSDN)

LINUX X86_64 (CSDN)

如果上面的jar包你无法使用,那就源码编译吧。树莓派就是要源码编译才能用。

--------------------------------------------------------------------------------------------------------------

(摘自官网:http://rxtx.qbang.org/wiki/index.php/Download)

Pre-Built Binaries

Release Binary Source
rxtx 2.1-7r2 (stable) rxtx-2.1-7-bins-r2.zip rxtx-2.1-7r2.zip
rxtx 2.0-7pre2 (stable) Linux/x86 Win32 (incomplete) source

RXTX 2.2 will replace RXTX 2.1 once it is stable.

Release Binary Source
rxtx 2.2pre2 (prerelease) rxtx-2.2pre2-bins.zip rxtx-2.2pre2.zip

TODO: The 2.2pre2 bins contain the 2.2pre1 jar file and the 2.2pre2 native lib which causes a mismatch warning

Other releases can be found in the archive and you can also check the change history in the change log.

--------------------------------------------------------------------------------------------------------------

不懂编译的请点这:在RaspberryPi树莓派上使用RXTX(RXTX的源码安装)(文中部分转载)

1、解包

2、 ./configure  

make前获取“uname -r”的信息,然后修改/usr/include/linux/version.h 

例如我的 uname -r 信息为 4.15.0-21deepin-generic

需要在/usr/include/linux/version.h 文本面添加 #define UTS_RELEASE "4.15.0-21deepin-generic"

3、make

4、make install

我的是LINUX系统,安装方法大同小异。

WINDOW解压后运行BAT文件(win8以上可能需要管理员身份运行)

LINUX解压后运行sudo sh run-install.sh

有可能会出现的错误:

1、输出unknown错误,需要打开sh文件把uname -i改为uname -m。

2、JAVA_HOME无法找到,需要文件头部的 #!/bin/sh 下添加你的JAVA_HOME地址,

      例如:JAVA_HOME=/home/HUAQ/jdk1.8.0_151

最直接的方法(仅限JRE运行的用户,JDK用下面的方法):

JAVA_HOME=/home/HUAQ/jdk1.8.0_151
HWVER=amd64    

#这里是sh文件里的内容
#echo "Installing Cloudhopper RXTX Build to JAVA_HOME=${JAVA_HOME}"
#export HWVER=$(uname -i)
#
#
#if [ $HWVER = "x86_64" ]
#then
#  # rename to what java uses
#  export HWVER="amd64"
#fi

cp /home/HUAQ/Desktop/mfz-rxtx-2.2-20081207-linux-x86_64/RXTXcomm.jar $JAVA_HOME/jre/lib/ext/

cp /home/HUAQ/Desktop/mfz-rxtx-2.2-20081207-linux-x86_64/librxtxSerial.so $JAVA_HOME/jre/lib/$HWVER/

cp /home/HUAQ/Desktop/mfz-rxtx-2.2-20081207-linux-x86_64/librxtxParallel.so $JAVA_HOME/jre/lib/$HWVER/

JDK方法:


Windows
----------------------------------------------------

Choose your binary build - x64 or x86 (based on which version of
the JVM you are installing to)

NOTE: You MUST match your architecture.  You can't install the i386
version on a 64-bit version of the JDK and vice-versa.

For a JDK installation:

Copy RXTXcomm.jar ---> <JAVA_HOME>\jre\lib\ext
Copy rxtxSerial.dll ---> <JAVA_HOME>\jre\bin
Copy rxtxParallel.dll ---> <JAVA_HOME>\jre\bin

Linux
----------------------------------------------------

Choose your binary build - x86_64 or i386 (based on which version of
the JVM you are installing to)

NOTE: You MUST match your architecture.  You can't install the i386
version on a 64-bit version of the JDK and vice-versa.

For a JDK installation on architecture=i386

Copy RXTXcomm.jar ---> <JAVA_HOME>/jre/lib/ext
Copy librxtxSerial.so ---> <JAVA_HOME>/jre/lib/i386/
Copy librxtxParallel.so ---> <JAVA_HOME>/jre/lib/i386/

NOTE: For a JDK installation on architecture=x86_64, just change the
i386 to x86_64 above.

操作串口的顺序:

1、查询串口列表
2、获取串口链接
3、通过串口链接读写COM(serialPort.getInputStream(),serialPort.getOutputStream())
4、关闭串口链接

JAVA代码:

    public static ArrayList<String> 查询串口列表() {
        
        //获得当前所有可用串口
        Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();    
        
        ArrayList<String> portNameList = new ArrayList<>();

        //将可用串口名添加到List并返回该List
        while (portList.hasMoreElements()) {
            String portName = portList.nextElement().getName();
            portNameList.add(portName);
        }

        return portNameList;

    }
    
    
    
    public static SerialPort 获取串口链接(String 串口名, int 波特率){

        SerialPort serialPort = null;
        
        try {

            //通过端口名识别端口
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(串口名);

            //打开端口,并给端口名字和一个timeout(打开操作的超时时间)
            CommPort commPort = portIdentifier.open(串口名, 2000);

            //判断是不是串口
            if (commPort instanceof SerialPort) {
                
                serialPort = (SerialPort) commPort;
                
                //设置一下串口的波特率等参数
                //设置串口参数依次为(波特率,数据位,停止位,奇偶检验) 
                serialPort.setSerialPortParams(
                                               波特率, 
                                               SerialPort.DATABITS_8, 
                                               SerialPort.STOPBITS_1, 
                                               SerialPort.PARITY_NONE
                                               );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            return serialPort;
    }
    
    
    
    public static void 关闭串口链接(SerialPort 串口链接) {
        if (串口链接 != null) {
            串口链接.close();
            串口链接 = null;
        }
    }

猜你喜欢

转载自blog.csdn.net/fengwangzhe000/article/details/80999037