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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章