|
private void btnHome_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
private void btnStart1_Click(object sender, EventArgs e)
{
if (chkRandom.Checked) {}
else
{
if (client == null || client.State != OpcClientState.Connecting)
{
try
{
string url = "opc.tcp://192.168.0.1:4840";
string user = "admin";
string password = "admin";
client = new OpcClient(url);
client.Security.UserIdentity = new OpcClientIdentity(user, password);
lbltext.Text = "通信開始";
client.Connect();
lbltext.Text = "通信OK";
}
catch (Exception ex)
{
lbltext.Text = "通信NG";
}
}
}
startTime = DateTime.UtcNow;
dataLoadTimer = new Timer();
dataLoadTimer.Interval = 1000;
dataLoadTimer.Tick += (s, a) => DataLoadTimer_Tick();////// a でいいのか❓
dataLoadTimer.Start();
}
private void DataLoadTimer_Tick()
{
LoadData();
}
private void btnStop1_Click(object sender, EventArgs e)
{
dataLoadTimer.Stop();
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private Chart CreateChart(IList<double> doubles)
{
Chart chart = new Chart();
chart.Size = new System.Drawing.Size(980, 300);
chart.ChartAreas.Add(new ChartArea());
chart.Series.Add("DataSeries");
chart.Series["DataSeries"].ChartType = SeriesChartType.Line;
chart.Series["DataSeries"].BorderWidth = 2;
chart.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
for (int i = 0; i < doubles.Count; i++)
{
chart.Series["DataSeries"].Points.AddXY(i, doubles[i]);
}
chart.ChartAreas[0].AxisX.Minimum = 0;
chart.ChartAreas[0].AxisX.Maximum = doubles.Count - 1;
return chart;
}
private void SlideData(Chart chart, List<double> list, double newData)
{
list.Add(newData);
list.RemoveAt(0);
chart.Series["DataSeries"].Points.Clear();
for (int x = 0; x < list.Count; x++)
{
chart.Series["DataSeries"].Points.AddXY(x, list[x]);
}
}
private void LoadData()
{
try
{
var currentTime = DateTime.UtcNow;
var elapsedTime = currentTime - startTime;
var flowNode = "ns=3;i=16844032";
var accumflowNode = "ns=3;i=16843776";
var pressureNode = "ns=3;i=16844544";
var tempNode = "ns=3;i=16844288";
var flow = client.ReadNode(flowNode);
var accumFlow = client.ReadNode(accumflowNode);
var pressure = client.ReadNode(pressureNode);
var temp = client.ReadNode(tempNode);
double flowData = Convert.ToInt32(flow.Value) / 1000.0;
double accumeData = Convert.ToInt32(accumFlow.Value) / 1000.0;
double pressureData = Convert.ToInt32(pressure.Value) / 1000.0;
double tempData = Convert.ToInt32(temp.Value) / 10.0;
SlideData(upperChart, upperList, pressureData);
SlideData(lowerChart, lowerList, tempData);
//-------保存内容--------
DateTime dt = DateTime.Now;
string date = dt.ToString("yyyy/M/d");
string time = dt.ToString("HH:mm:ss");
string data1 = flowData.ToString();
string data2 = tempData.ToString();
string data3 = pressureData.ToString();
string data4 = accumeData.ToString();
string price = G.priceData.ToString();
SaveDataToCSV(date, time, data1, data2,data3,data4,price);
upperChart.ChartAreas[0].AxisX.Minimum = dataCounter >= cycletime ? dataCounter - cycletime : 0;
upperChart.ChartAreas[0].AxisX.Maximum = dataCounter;
lowerChart.ChartAreas[0].AxisX.Minimum = dataCounter >= cycletime ? dataCounter - cycletime : 0;
lowerChart.ChartAreas[0].AxisX.Maximum = dataCounter;
while (upperChart.Series["DataSeries"].Points.Count > 0 && upperChart.Series["DataSeries"].Points[0].XValue < dataCounter - cycletime)
{
upperChart.Series["DataSeries"].Points.RemoveAt(0);
lowerChart.Series["DataSeries"].Points.RemoveAt(0);
upperChart.ChartAreas[0].AxisX.Maximum = dataCounter;
lowerChart.ChartAreas[0].AxisX.Maximum = dataCounter;
}
lbltext.Text = "通信中";
}
catch (Exception ex)
{
lbltext.Text = ex.Message;
lbltext.Text = ex.StackTrace;
}
}
private void SaveDataToCSV(string date, string time, string data1,string data2, string data3, string data4, string price)
{
try
{
string filePath = G.csvDataFilePath + DateTime.Now.ToString(@"yyyy\-M\-d") + ".csv";
using (StreamWriter sw = File.AppendText(filePath))
{
sw.WriteLine($"{date},{time},{data1},{data2},{data3},{data4},{price}");
}
}
catch (Exception ex)
{
timer.Enabled = false;
MessageBox.Show("以下の理由でファイルが開けません。\n" + ex.Message);
timer.Enabled = true;
}
}
}
}
|