0% found this document useful (0 votes)
14 views

CSDL

The document defines code for connecting to a database and OPC server to retrieve real-time production data from a PLC. It includes functions for connecting to the database and OPC server, reading tags from the PLC, and updating the database with the new values.

Uploaded by

Trần Tuấn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CSDL

The document defines code for connecting to a database and OPC server to retrieve real-time production data from a PLC. It includes functions for connecting to the database and OPC server, reading tags from the PLC, and updating the database with the new values.

Uploaded by

Trần Tuấn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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;
using OPCAutomation;

namespace QUẢN_LÍ_WEBSEVER
{
public partial class CSDL : Form
{
public CSDL()
{
InitializeComponent();
}

private void CSDL_Load(object sender, EventArgs e)


{
class_Status_Display statusDisplay = new class_Status_Display();
class_Login fn_login = new class_Login();
KEPServerEX_Connect();
fn_login.Not_Login();

private void btt_Login_Click(object sender, EventArgs e)


{
form_Login loginForm = new form_Login();
loginForm.Show();
tbx_DBName.Text = Properties.Settings.Default.SQL_DBName;
}
class_Login fn_login = new class_Login();
private void btt_Logout_Click(object sender, EventArgs e)
{
fn_login.Not_Login();

private void button1_Click(object sender, EventArgs e)


{
KEPServerEX_Connect();
tbx_DBName.Text = Properties.Settings.Default.SQL_DBName;
string sqlSelect = "SELECT * FROM Production_data;";
class_Database.sqlDisplay(sqlSelect, dataGridView1);
// Đặt tên cho các cột datagridview
dataGridView1.Columns[0].HeaderText = "Ngày Tháng";
dataGridView1.Columns[1].HeaderText = "SỐ KG";
dataGridView1.Columns[2].HeaderText = "SỐ SP Nhẹ";
dataGridView1.Columns[3].HeaderText = "SỐ SP Trung Bình";
dataGridView1.Columns[4].HeaderText = "SỐ SP Nặng";
}
static int tagNumber = 13; // Cài đặt số lượng tag của project
static int PLCscantime = 1000; // Cài đặt thời gian quét PLC
// Gọi các kết nối OPC
public OPCAutomation.OPCServer AnOPCServer;
public OPCAutomation.OPCServer OPCServer;
public OPCAutomation.OPCGroups OPCGroup;
public OPCAutomation.OPCGroup PLC;
public string Groupname;

static int arrlength = tagNumber + 1;


Array OPtags = class_KEPServerEX.tagread(arrlength);
Array tagID = class_KEPServerEX.tagID(arrlength);
Array WriteItems = Array.CreateInstance(typeof(object), arrlength);
Array tagHandles = Array.CreateInstance(typeof(Int32), arrlength);
Array OPCError = Array.CreateInstance(typeof(Int32), arrlength);
Array dataType = Array.CreateInstance(typeof(Int16), arrlength);
Array AccessPaths = Array.CreateInstance(typeof(string), arrlength);
// Chương trình con kết nối (Connect)
private void KEPServerEX_Connect()
{
string IOServer = "Kepware.KEPServerEX.V6";
string IOGroup = "OPCGroup1";
OPCServer = new OPCAutomation.OPCServer();
OPCServer.Connect(IOServer, "");
PLC = OPCServer.OPCGroups.Add(IOGroup);
PLC.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(dataScan);
PLC.UpdateRate = PLCscantime;
PLC.IsSubscribed = PLC.IsActive;
PLC.OPCItems.DefaultIsActive = true;
PLC.OPCItems.AddItems(tagNumber, ref OPtags, ref tagID,
out tagHandles, out OPCError, dataType, AccessPaths);
}

string sql_Real;
string sql_Float;
string sql_TB;
string sql_Nang;
private void dataScan(int ID, int NumItems, ref Array tagID,
ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
for (int i = 1; i <= NumItems; i++)
{
// Khai báo biến chung
int getTagID = Convert.ToInt32(tagID.GetValue(i));
string tagValue = ItemValues.GetValue(i)?.ToString();

Console.WriteLine(tagID.Length);
// Lấy giá trị tag
string sqltable_name = "Production_data";
string collum1 = "date_time";
string collum2 = "Act_Loadcell";
string collum3 = "Act_Count_Nhe";
string collum4 = "Act_Count_TB";
string collum5 = "Act_Count_Nang";
string data1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
string data2 = sql_Float;
string data3 = sql_Real;
string data4 = sql_TB;
string data5 = sql_Nang;
if (getTagID == 8)
{
sql_Real = tagValue;
tbx_nhe.Text = tagValue;

}
if (getTagID == 9)
{
sql_TB = tagValue;
tbx_tb.Text = tagValue;
}
if (getTagID == 7)
{
sql_Nang = tagValue;
tbx_nang.Text = tagValue;
}
if (getTagID == 2 )
{
sql_Float = tagValue;
tbx_loadcell.Text = tagValue;
}

textBox1.Text = tagValue;

}
}

private void btt_Showdata_Click(object sender, EventArgs e)


{

}
}

You might also like