データ収集・保存

using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace Timer01
{
public partial class Form1 : Form
{
int TimeCount;
int Suint;
string Su;
bool timeflag = false;
string recieved_data = new string[10] {"あ","り","が","と","う","げ","ん","き","で","な"};
string
savedata = new string[10];
string[,] list = new string[14400, 11];
bool saveflag = false; //データ保存しない場合は削除(false)
public static int itemselect = new int[10];


public savelog saveMonitor;
//private static readonly TimeSpan PollingInterval = TimeSpan.FromMilliseconds(1);
private static readonly TimeSpan PollingInterval;

public Form1()
{
InitializeComponent();

TimeCount = 0;

saveMonitor = new savelog();
saveMonitor.setFileName("Monitor_");

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void TImerStart_Click(object sender, EventArgs e)
{
textBox1.Clear();
TimeCount = 0;
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
if(timeflag == true)
{
button1.Text = "開始";
TimeCount++;
//textBox1.AppendText(TimeCount.ToString());
//textBox1.AppendText(" [" + DateTime.Now.ToString("ss:fff") + "]");
//textBox1.AppendText("\r\n");

saveMonitor.addText(TimeCount.ToString() + " ::: " + DateTime.Now.ToString("s:fff"));
saveMonitor.addText("\r\n");

if (Suint*100 <= TimeCount)
{
saveMonitor.save1("");
button1.Text = "終了";
timeflag = false;
timer1.Stop();

}
}
}

private void TimerStop_Click(object sender, EventArgs e)
{
timer1.Stop();
}

private void button1_Click(object sender, EventArgs e)
{
Su = Interaction.InputBox("収集時間", "収集する時間を「秒」で入力してください(1~14400)","10", 200, 100);

if(Su != "")
{

Suint = int.Parse(Su);
button1.Text = "スタンバイ";
timeflag = true;
}

 

}

private void button2_Click(object sender, EventArgs e)
{
//ストップウォッチを開始する
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();


Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();

//10秒待機
System.Threading.Thread.Sleep(10000);


//ストップウォッチを止める
sw.Stop();

t.Abort();

//結果を表示する
textBox1.Text = sw.Elapsed.ToString();
}

private void ThreadProc()
{

int timecheck1 = int.Parse(DateTime.Now.ToString("ff"));
int timecheck2;

saveMonitor.addText("時間=" + Su + "[s]\r\n");

 

for (int i = 1; i <= Suint * 100; i++)
{
loop:
timecheck2 = int.Parse(DateTime.Now.ToString("ff"));

if (timecheck1 == timecheck2) goto loop;

timecheck1 = timecheck2;

saveMonitor.addText(i.ToString() + ":受信");

for (int j = 0; j < 10; j++)
{
saveMonitor.addText("," + recieved_data[j].ToString());
}
saveMonitor.addText(DateTime.Now.ToString("mm:ss:fff") + "\r\n");
}
saveMonitor.save1("");
button1.Text = "終了";

}

private void button2_Click_1(object sender, EventArgs e)
{
int Count = 0;
string listGyo = new string[14400];

//選択項目クリア
var length = itemselect.Length;
Array.Clear(itemselect, 0, length);

SelectItem seletitme = new SelectItem();
seletitme.Show();

//フォームが閉じるまで止める方法**************************************************************************

//選択後
saveMonitor.setFileName("Result_");
saveMonitor.addText("時間=" + Su + "[s]\r\n");

//収集データ取得
try
{
StreamReader sr = new StreamReader(Application.StartupPath + "\\data\\" + savelog.saveFileName, System.Text.Encoding.GetEncoding("shift_jis"));

while (!sr.EndOfStream)
{
// ファイルの一行を読み込む
listGyo[Count] = sr.ReadLine();

// 1行分のデーターを、カンマ区切りで分離
string[] line = listGyo[Count].Split(',');

// 出力用配列に格納
for (int j = 0; j < line.Length; j++)
{
list[ Count, j] = line[j];
}
Count++;
}

for (int x = 0; x < Count; x++)
{
if (itemselect[0] == 1) saveMonitor.addText(list[Count, 0] + ",");
if (itemselect[1] == 1) saveMonitor.addText(list[Count, 1] + ",");
if (itemselect[2] == 1) saveMonitor.addText(list[Count, 2] + ",");
if (itemselect[3] == 1) saveMonitor.addText(list[Count, 3] + ",");
if (itemselect[4] == 1) saveMonitor.addText(list[Count, 4] + ",");
if (itemselect[5] == 1) saveMonitor.addText(list[Count, 5] + ",");
if (itemselect[6] == 1) saveMonitor.addText(list[Count, 6] + ",");
if (itemselect[7] == 1) saveMonitor.addText(list[Count, 7] + ",");
if (itemselect[8] == 1) saveMonitor.addText(list[Count, 8]);
saveMonitor.addText("\r\n");
}

saveMonitor.save2("");


}
catch
{

}

 

}
}
}

//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Timer01
{
public partial class SelectItem : Form
{

public SelectItem()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

//項目選択
if (checkBox1.Checked == true) Form1.itemselect[0] = 1;
if (checkBox2.Checked == true) Form1.itemselect[1] = 1;
if (checkBox3.Checked == true) Form1.itemselect[2] = 1;
if (checkBox4.Checked == true) Form1.itemselect[3] = 1;
if (checkBox5.Checked == true) Form1.itemselect[4] = 1;
if (checkBox6.Checked == true) Form1.itemselect[5] = 1;
if (checkBox7.Checked == true) Form1.itemselect[6] = 1;
if (checkBox8.Checked == true) Form1.itemselect[7] = 1;
if (checkBox9.Checked == true) Form1.itemselect[8] = 1;

this.Close();
}
}
}