4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > udpImport > SOTからEOTまでのデータ受信 > udpTimeGraph / udpMonitor(udpのRelay)のExportデータ取り込み > udpの送受信 / csv出力

Last updated at Posted at 2015-09-13
動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5

v0.2 @ github

機能

udpTimeGraph (Qiita投稿)

udpMonitor (Qiita投稿)
に対して、export機能を用いて格納データを取り出してcsvファイル保存する。

使用例

Main_unity_-150913-udpImport-PC__Mac___Linux_Standalone__Personal.jpg

IPとポート番号を押して、いずれかのボタンを押す。

接続先が応答すれば"recv to csv"のメッセージが表示され import.csvファイルにインポートデータ(以下例)が格納される。

import.csv
SOT
2015/09/13 00:00:00,0.1
2015/09/13 02:10:00,0.45
2015/09/13 03:40:00,-0.45
2015/09/13 05:30:00,-0.45
2015/09/13 09:30:00,0.2
2015/09/13 11:30:00,0.4
2015/09/13 13:30:00,0.1
2015/09/13 16:30:00,0.5
2015/09/13 18:30:00,0.9
2015/09/13 22:00:00,-0.9
EOT

実装

スクリプトは以下。

importScript.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

/*
 * v0.1 2015/09/13
 *  - checked with udpTimeGraph import
 */ 

public class ImportScript : MonoBehaviour {
	
	public InputField IFipadr;
	public InputField IFport;
	public Text rcvText; // recieved text
	public string sendCommand;

	int getPort() {
		int res = Convert.ToInt16 (IFport.text);
		if (res < 0) {
			res = 0;
		}
		return res;
	}

	bool SendCommand(ref UdpClient client, string ipadr, int port) {
		string sendstr = sendCommand + System.Environment.NewLine;
		byte[] data = ASCIIEncoding.ASCII.GetBytes (sendstr);

		try {
			client.Send (data, data.Length, ipadr, port);
		}
		catch (Exception e) {
			rcvText.text = "snd:" + e.Message;
			return false;
		}
		return true;
	}
	
	void procComm() {
		UdpClient client = new UdpClient ();
		client.Client.SendTimeout = 1000; // msec
		client.Client.ReceiveTimeout = 2000; // msec

		if (SendCommand (ref client, (IFipadr.text), getPort()) == false) {
			return;
		}

		// receive
		IPEndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
		string rcvdstr = "";
		byte [] data;

		while (true) {
			try {
				data = client.Receive (ref remoteIP);
				if (data.Length == 0) {
					break; // no response
				}
				string text = Encoding.ASCII.GetString (data);
				rcvdstr += text;
				if (text.Contains("EOT")) { // End of Table
					break;
				}
			} catch (Exception err) {
				Debug.Log(err.Message);
				rcvText.text = "no response";
				break;
			}
		}
		
		client.Close ();

		if (rcvdstr.Length > 0) {
			System.IO.File.WriteAllText("import.csv", rcvdstr);
			rcvText.text = "recvd to csv";
		}
	}
	
	public void onClick() {
		procComm ();
	}
}

上記スクリプトを2つのボタンに貼り付ける。
sendCommandだけ異なるようにしている。

  • fromUdpTimeGraphの関連付け

Main_unity_-150913-udpImport-PC__Mac___Linux_Standalone__Personal.jpg

  • fromUdpMonitorの関連づけ

Main_unity_-150913-udpImport-PC__Mac___Linux_Standalone__Personal.jpg

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?