動作確認
Unity 5.1.3-f1 on MacOS X 10.8.5
機能
udpTimeGraph (Qiita投稿)
と
udpMonitor (Qiita投稿)
に対して、export機能を用いて格納データを取り出してcsvファイル保存する。
使用例
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の関連付け
- fromUdpMonitorの関連づけ