メニュー 閉じる

065. ProcessingからRFIDデバイスをシリアルで制御してみる

今回はProcessingからRFIDデバイスにアクセスした場合のコードを以下に書いてみました。
やたらと長いのですが、要はデバイスとシリアル通信で繋いで、こっちから命令を送ったり、
デバイスからの返答を受け取ったり、という流れになります。

シリアル接続の場合、データはバイト単位でのやりとりになるので、一つのコマンドに対して
port.writeで数行書く必要があります。また、このコマンドはデバイスによって違うので、
付属しているアプリケーションで、各操作のコマンドを調べておくといいと思います。

import processing.serial.*;

Serial port;

String data;

String tag_id01;
String tag_id02;
String tag_id03;

int tag_num;

PFont font = createFont("Arial Bold", 300);

void setup () {

	size (800, 600);

	for (int i = 0; i < Serial.list ().length; i++) {
  		println (Serial.list ()[i]);
	}

	port = new Serial (this, Serial.list ()[5], 38400);

	tag_id01 = "";
	tag_id02 = "";
	tag_id03 = "";

	tag_num = 0;
	
	//IO制御
	port.write (char (0x02));
	port.write (char (0x00));
	port.write (char (0x75));
	port.write (char (0x02));
	port.write (char (0x02));
	port.write (char (0x0C));
	port.write (char (0x03));
	port.write (char (0x8A));
	port.write (char (0x0D));

	/*
	//連続イベントリモード
	port.write (char (0x02));
	port.write (char (0x00));
	port.write (char (0x4E));
	port.write (char (0x07));
	port.write (char (0x00));
	port.write (char (0x50));
	port.write (char (0x00));
	port.write (char (0x80));
	port.write (char (0x00));
	port.write (char (0x00));
	port.write (char (0x00));
	port.write (char (0x03));
	port.write (char (0x2A));
	port.write (char (0x0D));
	*/

}

void draw () {

	background (0);

	fill (255);
	textFont (font);
	textSize (20);
	textAlign (CENTER, CENTER);

	if (tag_num == 0) {
		text ("Tag01 UID : NO DATA", width / 2, 200);
		text ("Tag02 UID : NO DATA", width / 2, 300);
		text ("Tag03 UID : NO DATA", width / 2, 400);
	}
	if (tag_num == 1) {
		text ("Tag01 UID : " + tag_id01, width / 2, 200);
		text ("Tag02 UID : NO DATA", width / 2, 300);
		text ("Tag03 UID : NO DATA", width / 2, 400);
	}
	if (tag_num == 2) {
		text ("Tag01 UID : " + tag_id01, width / 2, 200);
		text ("Tag02 UID : " + tag_id02, width / 2, 300);
		text ("Tag03 UID : NO DATA", width / 2, 400);
	}
	if (tag_num == 3) {
		text ("Tag01 UID : " + tag_id01, width / 2, 200);
		text ("Tag02 UID : " + tag_id02, width / 2, 300);
		text ("Tag03 UID : " + tag_id03, width / 2, 400);
	}

}

void serialEvent (Serial p) {

	char in_byte = (char) p.read ();

	if (in_byte == '\r' && data.length () > 28) {
		if (tag_num == 1) {
			tag_id01 = data.substring (10, 26);
			tag_id02 = "";
			tag_id03 = "";
			println (tag_id01, tag_id02, tag_id03);
		}
		if (tag_num == 2) {
			tag_id01 = data.substring (26, 42);
			tag_id02 = data.substring (10, 26);
			tag_id03 = "";
			println (tag_id01, tag_id02, tag_id03);
		}
		if (tag_num == 3) {
			tag_id01 = data.substring (42, 58);
			tag_id02 = data.substring (26, 42);
			tag_id03 = data.substring (10, 26);
			println (tag_id01, tag_id02, tag_id03);
		}
		//インベントリコマンド
		port.write (char (0x02));
		port.write (char (0x00));
		port.write (char (0x78));
		port.write (char (0x01));
		port.write (char (0xF0));
		port.write (char (0x03));
		port.write (char (0x6E));
		port.write (char (0x0D));
		data = "";
	} else if (in_byte == '\r') {
		if (data.equals ("null020030010C0342") == true) {
			println ("IO Control Configuration ==========> OK");
			//RF送信信号設定
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x4E));
			port.write (char (0x02));
			port.write (char (0x9E));
			port.write (char (0x00));
			port.write (char (0x03));
			port.write (char (0xF3));
			port.write (char (0x0D));
		} else if (data.equals ("020030029E0103D6") == true) {
			println ("RF Transmission Configuration ==========> OK");
			//コマンドモード設定
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x4E));
			port.write (char (0x07));
			port.write (char (0x00));
			port.write (char (0x00));
			port.write (char (0x00));
			port.write (char (0x80));
			port.write (char (0x00));
			port.write (char (0x00));
			port.write (char (0x00));
			port.write (char (0x03));
			port.write (char (0xDA));
			port.write (char (0x0D));
		} else if (data.equals ("020030000335") == true) {
			println ("Command Mode Configuration ==========> OK");
			//インベントリコマンド
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x78));
			port.write (char (0x01));
			port.write (char (0xF0));
			port.write (char (0x03));
			port.write (char (0x6E));
			port.write (char (0x0D));
		} else if (data.equals ("02003002F0000327") == true) {
			println ("Now Number of Tags ==========> 0 (" + data.substring (14) + ")");
			tag_num = 0;
			//インベントリコマンド
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x78));
			port.write (char (0x01));
			port.write (char (0xF0));
			port.write (char (0x03));
			port.write (char (0x6E));
			port.write (char (0x0D));
		} else if (data.equals ("02003002F0010328") == true) {
			println ("Now Number of Tags ==========> 1 (" + data.substring (14) + ")");
			tag_num = 1;
			//UIDデータ取得コマンド
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x4F));
			port.write (char (0x03));
			port.write (char (0x54));
			port.write (char (0x00));
			port.write (char (0x01));
			port.write (char (0x03));
			port.write (char (0xAC));
			port.write (char (0x0D));
		} else if (data.equals ("02003002F0020329") == true) {
			println ("Now Number of Tags ==========> 2 (" + data.substring (14) + ")");
			tag_num = 2;
			//UIDデータ取得コマンド
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x4F));
			port.write (char (0x03));
			port.write (char (0x54));
			port.write (char (0x00));
			port.write (char (0x02));
			port.write (char (0x03));
			port.write (char (0xAD));
			port.write (char (0x0D));
		} else if (data.equals ("02003002F003032A") == true) {
			println ("Now Number of Tags ==========> 3 (" + data.substring (14) + ")");
			tag_num = 3;
			//UIDデータ取得コマンド
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x4F));
			port.write (char (0x03));
			port.write (char (0x54));
			port.write (char (0x00));
			port.write (char (0x03));
			port.write (char (0x03));
			port.write (char (0xAE));
			port.write (char (0x0D));
		} else {
			//IO制御
			port.write (char (0x02));
			port.write (char (0x00));
			port.write (char (0x75));
			port.write (char (0x02));
			port.write (char (0x02));
			port.write (char (0x0C));
			port.write (char (0x03));
			port.write (char (0x8A));
			port.write (char (0x0D));
		}
		data = "";
		tag_id01 = "";
		tag_id02 = "";
		tag_id03 = "";
	} else {
		data += hex (in_byte, 2);
	}

}

 

Posted in processing