データ収集・保存

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();
}
}
}

テキストファイルをカンマ区切りで読み込み、配列に格納する

private void button6_Click(object sender, EventArgs e)
{
int time = 2;
int Count = 0;
string[,] list = new string[10,10];
string listGyo = new string[10];

// CSVファイルの読み込み
string filePath = Application.StartupPath + "\\aaa.txt";

// StreamReaderクラスをインスタンス
StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("UTF-8"));


while (!sr.EndOfStream)
{

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

for (int i = 0; i < listGyo.Length; i++)
{
if (listGyo[i] == null) break;

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

string[,] table = new string[listGyo.Length, line.Length];

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

Count++;
}

}

ファイル読み書き

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim inputFile As String = Path1 & file1 ' 入力用ファイル
Dim outputFile As String = Path1 & file2 ' 出力用ファイル

' 出力用ファイルをオープンする
Dim writer As New System.IO.StreamWriter(outputFile, False, System.Text.Encoding.GetEncoding("Shift_JIS"))

' 入力用ファイルをオープンする
Dim reader As New System.IO.StreamReader(inputFile, System.Text.Encoding.GetEncoding("Shift_JIS"))

' 読み取り可能文字が存在しない(ファイルの末尾に到着)すると -1 が返される
While (reader.Peek() > -1)

' 1 行ずつ読み込み、出力ファイルに書き込む
' このときの改行コードは Windows標準の CRLF となる
writer.WriteLine(reader.ReadLine())

End While

' ファイルを閉じる
reader.Close()

' ファイルを閉じる
writer.Close()

End Sub

Windowsフォームでコンソール出力を取り込む

Windowsフォーム>

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim command As String = "C:\Users\naoki\Documents\Visual Studio 2010\Projects\consoleInput1\consoleInput1\bin\Debug\consoleInput1.exe"

Dim p As New Process()

p.StartInfo.FileName = command ' 実行するファイル
p.StartInfo.CreateNoWindow = True ' コンソールを開かない
p.StartInfo.UseShellExecute = False ' シェル機能を使用しない

p.StartInfo.RedirectStandardOutput = True ' 標準出力をリダイレクト

p.Start() ' アプリの実行開始

Dim output As String = p.StandardOutput.ReadToEnd() ' 標準出力の読み取り

'output = output.Replace(vbCr + vbCrLf, vbLf) ' 改行コードの修正
'Debug.Write(output)

TextBox1.Text = output

End Sub
End Class

 

<コンソール>

Module Module1

Sub Main()

Console.WriteLine("スタート")

End Sub

End Module

データグリッドビューテキストファイル読み込み

Imports Microsoft.VisualBasic.FileIO

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
''データテーブル
Dim dt As New DataTable
Dim row As DataRow

''フィールド(列)を追加
dt.Columns.Add("filename", Type.GetType("System.String"))
dt.Columns.Add("env", Type.GetType("System.Int16"))
dt.Columns.Add("class", Type.GetType("System.Int16"))

''カンマ区切りテキストを読み込み、データテーブルに追加
Dim parser As TextFieldParser
parser = My.Computer.FileSystem.OpenTextFieldParser("C:\Users\naoki\Desktop\ttt.txt", ",")

While Not parser.EndOfData
row = dt.NewRow
row.ItemArray = parser.ReadFields
dt.Rows.Add(row)
End While
Call parser.Close()

''データグリッドビューにセット
DataGridView1.DataSource = dt
End Sub

テキストファイル印刷

Imports System.IO
Imports System.Text

Public Class Form2

Private Const ERROR_MESSAGE1 = "パス名を指定してください。"

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' プレビューダイアログのサイズを大きくする。
PrintPreviewDialog1.Size = New Size(640, 800)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
txtPath.Text = OpenFileDialog1.FileName
End If

End Sub

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' テキストファイルのパスが設定されているかチェックする。
ErrorProvider1.Clear()
If String.IsNullOrEmpty(txtPath.Text) Then
ErrorProvider1.SetError(txtPath, ERROR_MESSAGE1)
Return
End If

' プレビューを表示する。
PrintPreviewDialog1.ShowDialog()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' テキストファイルのパスが設定されているかチェックする。
ErrorProvider1.Clear()
If String.IsNullOrEmpty(txtPath.Text) Then
ErrorProvider1.SetError(txtPath, ERROR_MESSAGE1)

Return
End If
' 印刷開始
Try
PrintDocument1.DocumentName = New FileInfo(txtPath.Text).Name
PrintDocument1.Print()
Catch ex As Exception
MessageBox.Show(ex.Message, "致命的エラー")
End Try

End Sub

 

''' 印刷イベントハンドラ
''' <remarks>印刷行数は1ページに収まる長さでなければならない。</remarks>
Private Sub PrintDocument(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim g As Graphics = e.Graphics
Dim line As String
Dim font As Font = New Font("メイリオ", 11.0)
Dim brush As Brush = New SolidBrush(Color.Black)
Dim x, y As Single

' 行の印刷位置の初期値
x = 20.0F
y = 20.0F
' テキストファイルを1行づつ印刷する。
Using reader = New IO.StreamReader(txtPath.Text, System.Text.Encoding.Default)
' ファイルの終わりまで繰り返す。
While Not reader.EndOfStream
line = reader.ReadLine()
g.DrawString(line, font, brush, x, y)
y += 20.0F
End While
End Using
End Sub


End Class