この文書の現在のバージョンと選択したバージョンの差分を表示します。
次のリビジョン | 前のリビジョン | ||
mae3xx_devel:java_serial_programming:start [2014/04/23 11:13] admin 作成 |
mae3xx_devel:java_serial_programming:start [2014/09/04 16:18] (現在) admin |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
+ | ====== シリアルポート通信のプログラミング (Java) ====== | ||
+ | |||
+ | Java SEでシリアルポートを扱うためには、Java CommAPI に準拠した追加のライブラリ "RXTXcomm" を使用します。 | ||
+ | |||
+ | * [[https://blogs.oracle.com/jtc/entry/serial_port_communication_for_java|Jim Connors' Weblog - Serial Port Communication for Java SE Embedded]] | ||
+ | * [[http://en.wikibooks.org/wiki/Serial_Programming/Serial_Java|WIKIBOOKS - Serial Programming/Serial Java]] | ||
+ | |||
+ | ※ 『[[mae3xx_tips:use_socat:start]]』のページで、socat を使用してシリアルポートアクセスをソケットプログラミングにしてしまう方法の紹介を追加しました。 | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ===== RXTXcomm のインストール ===== | ||
+ | |||
+ | Ubuntu Linuxには、標準でパッケージが用意されていますので、それを導入します。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo apt-cache search librxtx | ||
+ | librxtx-java - Full Java CommAPI implementation | ||
+ | librxtx-java-dbg - Full Java CommAPI implementation, Debugging Symbols | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | \\ | ||
+ | |||
+ | "apt-get install" でインストールします。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo apt-get install librxtx-java | ||
+ | Reading package lists... Done | ||
+ | Building dependency tree | ||
+ | Reading state information... Done | ||
+ | The following NEW packages will be installed: | ||
+ | librxtx-java | ||
+ | 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. | ||
+ | Need to get 185 kB of archives. | ||
+ | After this operation, 884 kB of additional disk space will be used. | ||
+ | Get:1 http://ports.ubuntu.com/ubuntu-ports/ trusty/universe librxtx-java armhf 2.2pre2-11 [185 kB] | ||
+ | Fetched 185 kB in 1s (104 kB/s) | ||
+ | Selecting previously unselected package librxtx-java. | ||
+ | (Reading database ... 17679 files and directories currently installed.) | ||
+ | Preparing to unpack .../librxtx-java_2.2pre2-11_armhf.deb ... | ||
+ | Unpacking librxtx-java (2.2pre2-11) ... | ||
+ | Setting up librxtx-java (2.2pre2-11) ... | ||
+ | Processing triggers for libc-bin (2.19-0ubuntu6) ... | ||
+ | localepurge: Disk space freed in /usr/share/locale: 0 KiB | ||
+ | localepurge: Disk space freed in /usr/share/man: 0 KiB | ||
+ | |||
+ | Total disk space freed by localepurge: 0 KiB | ||
+ | |||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | \\ | ||
+ | |||
+ | どこにライブラリが導入されたか、確認してみます。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo dpkg -L librxtx-java | egrep "\.so$|\.jar" | ||
+ | /usr/share/java/RXTXcomm-2.2pre2.jar | ||
+ | /usr/lib/jni/librxtxRS485-2.2pre1.so | ||
+ | /usr/lib/jni/librxtxSerial-2.2pre1.so | ||
+ | /usr/lib/jni/librxtxParallel-2.2pre1.so | ||
+ | /usr/lib/jni/librxtxRaw-2.2pre1.so | ||
+ | /usr/lib/jni/librxtxI2C-2.2pre1.so | ||
+ | /usr/share/java/RXTXcomm.jar | ||
+ | /usr/lib/jni/librxtxParallel.so | ||
+ | /usr/lib/jni/librxtxRaw.so | ||
+ | /usr/lib/jni/librxtxI2C.so | ||
+ | /usr/lib/jni/librxtxSerial.so | ||
+ | /usr/lib/jni/librxtxRS485.so | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | * classpath に /usr/share/java/RXTXcomm.jar | ||
+ | * java.library.path に /usr/lib/jni | ||
+ | |||
+ | をそれぞれ指定すれば動作しそうです。 | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ===== 実行 ===== | ||
+ | |||
+ | [[http://d.hatena.ne.jp/talisker_ZQN/20130508/1368017726|Cults !! - ARMアーキテクチャ環境のJava7でRXTXをビルドする。]] のサンプルコードを実行してみます。\\ | ||
+ | |||
+ | <file java Test.java> | ||
+ | import gnu.io.CommPortIdentifier; | ||
+ | import java.util.Enumeration; | ||
+ | |||
+ | public class Test { | ||
+ | public static void main(String[] args) { | ||
+ | System.out.println("main method start."); | ||
+ | try { | ||
+ | Enumeration portList = CommPortIdentifier.getPortIdentifiers(); | ||
+ | CommPortIdentifier port = null; | ||
+ | while (portList.hasMoreElements()) { | ||
+ | port = (CommPortIdentifier)portList.nextElement(); | ||
+ | System.out.println(port.getName()); | ||
+ | } | ||
+ | } catch(Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | \\ | ||
+ | |||
+ | JDK の入ったマシンで .class ファイルを作成し、実機へ転送してから実行します。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo java -cp .:/usr/share/java/RXTXcomm.jar -Djava.library.path=/usr/lib/jni Test | ||
+ | main method start. | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | \\ | ||
+ | |||
+ | 動作はしているようですが、シリアルポート (ttyO0/ttyO1/ttyO2) が見つからないようです。\\ | ||
+ | RXTXcomm ライブラリで "ttyO" がシリアルポートであると登録されていないことが原因ですので、{{:mae3xx_devel:java_serial_programming:librxtx-java_2.2pre2-11mae1_armhf.deb|パッチをあてたパッケージ}} を作りました。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ ls -l *deb | ||
+ | -rw-r--r-- 1 user1 user1 100048 Apr 24 10:00 librxtx-java-dbg_2.2pre2-11mae1_armhf.deb | ||
+ | -rw-r--r-- 1 user1 user1 167170 Apr 24 10:00 librxtx-java_2.2pre2-11mae1_armhf.deb | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | これをインストールします。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo dpkg -i librxtx-java_2.2pre2-11mae1_armhf.deb | ||
+ | (Reading database ... 17751 files and directories currently installed.) | ||
+ | Preparing to unpack librxtx-java_2.2pre2-11mae1_armhf.deb ... | ||
+ | Unpacking librxtx-java (2.2pre2-11mae1) over (2.2pre2-11) ... | ||
+ | Setting up librxtx-java (2.2pre2-11mae1) ... | ||
+ | Processing triggers for libc-bin (2.19-0ubuntu6) ... | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ふたたび、サンプルを実行してみます。 | ||
+ | |||
+ | <code> | ||
+ | user1@plum:~$ sudo java -cp .:/usr/share/java/RXTXcomm.jar -Djava.library.path=/usr/lib/jni Test | ||
+ | main method start. | ||
+ | /dev/ttyO2 | ||
+ | /dev/ttyO1 | ||
+ | /dev/ttyO0 | ||
+ | user1@plum:~$ | ||
+ | </code> | ||
+ | |||
+ | こんどは ttyO0/ttyO1/ttyO2 がシリアルポートとして認識されました。 | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ===== ダウンロード ===== | ||
+ | |||
+ | ^ File ^ Stat ^ SHA1SUM ^ Info | | ||
+ | |{{:mae3xx_devel:java_serial_programming:librxtx-java_2.2pre2-11mae1_armhf.deb|}}|2014/04/24 10:04 163.3 KB|4aa348c80b8d264f882685bed1f0e3e295f79bec|Full Java CommAPI implementation| | ||
+ | |{{:mae3xx_devel:java_serial_programming:librxtx-java-dbg_2.2pre2-11mae1_armhf.deb|}}|2014/04/24 10:04 97.7 KB|80c414e75c278d9d4823b8d58045dd41bb79f8c9|Full Java CommAPI implementation, Debugging Symbols| | ||