今回行いたいのは,RaspberryPiにRXTXライブラリを入れ,USB-シリアルに接続したXBeeを操作するアプリケーションの開発をパソコン上のeclipseで行うというものです.eclipseでリモートコントロールするには,RemoteVMを用います.RemoteVMの使い方についてはこちらをご覧ください.今回の環境を下に示します.


RaspberryPi側の設定

こちらを参照してください.


パソコン側の設定

まず,パソコンにRXTXライブラリをダウンロードとインストールをしておきましょう.こちらのサイトからRXTXをダウンロードし,インストールをしてください.インストール方法はダウンロードファイルに入っているINSTALL.txtを参照してください.

次にこちらを参照し,パソコンの設定を行ってください.ただし,途中でRXTXのための設定を行いますので,追加設定のみこれから説明します.

RXTXライブラリをeclipseのプロジェクトに追加します.プロジェクトを右クリックするとポップアップメニューが現れますので,その中にあるプロパティーを選択してください.そして,左側にあるJava⇒Javaのビルドバスを選択し,ライブラリタブを選択すると下のようなダイアログが現れますので,外部Jar追加を押します.

 

あらかじめインストールしておいたRXTXcomm.jarを選択します.

 

次に,新たなクラスSampleを追加します.

 

 

下のように名前をSampleにします.

 

プログラムには下のようにします.

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Sample {

	void connect(String portName) throws Exception {
		CommPortIdentifier portIdentifier = CommPortIdentifier
				.getPortIdentifier(portName);
		if (portIdentifier.isCurrentlyOwned()) {
			System.out.println("Error: Port is currently in use");
		} else {
			int timeout = 2000;
			CommPort commPort = portIdentifier.open(this.getClass().getName(),
					timeout);

			if (commPort instanceof SerialPort) {
				SerialPort serialPort = (SerialPort) commPort;
				serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
						SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

				InputStream in = serialPort.getInputStream();
				OutputStream out = serialPort.getOutputStream();

				(new Thread(new SerialReader(in))).start();
				(new Thread(new SerialWriter(out))).start();

			} else {
				System.out
						.println("Error: Only serial ports are handled by this example.");
			}
		}
	}

	public static class SerialReader implements Runnable {

		InputStream in;

		public SerialReader(InputStream in) {
			this.in = in;
		}

		public void run() {
			byte[] buffer = new byte[1024];
			int len = -1;
			try {
				while ((len = this.in.read(buffer)) > -1) {
					System.out.print(new String(buffer, 0, len));
				}
			} 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();
			}
		}
	}

	public static void main(String[] args) {
		try {
			(new Sample()).connect("/dev/ttyUSB0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

次に下のように実行します.ただし,実行構成を直さなければならないため,1回目の実行では動作しません.

 

clientを実行するため,LaunchRemoteを選択します.

 

次に,実行時の引数を修正します.「引数」タブを選択し,下記のようにプログラムの引数を書いてください.

RaspberryPiのIPアドレス:8999
Sample
-rcp /usr/share/java/RXTXcomm.jar
-rcp .
--

最後に,ダイアログ下側にある「実行」ボタンを押してください.


トラブルシューティング

下図のようなエラーが発生することがあります.これは,ルートユーザが最後に実行したため,そのロックファイルが一般ユーザには消せない時に発生します.

 

このような場合には,以下のようにしてロックファイルを削除してください.

sudo rm /var/lock/LCK..ttyUSB0