这个方法的好处是读写不同的DB块,只需要新建不同的类就可以了,非常方便。类的结构只需要和DB块的结构相同即可。
例如这里针对DB142建立的类DB_PreCommand.cs
using S7.Net;using System;using System.Collections.Generic;
using System.Linq;using System.Text;using System.Threading.Tasks;
namespaceS7net_Class{publicclassDB_PreCommand {
/* PLC与C# 常用数据类型转换PLC C#Bool BoolWord ushortInt ushortDword uint32Dint uintbyte byteReal double */publicuint Task_No { get; set; }publicushort Job_Style { get; set; }publicushort Inlet_Station_No { get; set; }
publicushort Load_Laneway_No { get; set; }
publicushort UnLoad_Laneway_No { get; set; }
publicushort Outlet_Station_No { get; set; }publicushort Load_Line { get; set; }
publicushort Load_Col { get; set; }publicushort Load_Layer { get; set; }publicushort Unload_Line { get; set; }
publicushort Unload_Col { get; set; }publicushort Unload_Layer { get; set; }
publicushort Product_Size { get; set; }
publicushort Product_Weight { get; set; }publicushort Verify { get; set; }
// 定义一个静态变量来保存类的实例privatestatic DB_PreCommand uniqueInstance;// 定义一个标识确保线程同步privatestaticreadonlyobject locker = newobject();// 定义私有构造函数,使外界不能创建该类实例privateDB_PreCommand() {
}///<summary>/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点///</summary>///<returns></returns>publicstatic DB_PreCommand GetInstance() {// 当第一个线程运行到这里时,此时会对locker对象 "加锁",// 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁// lock语句运行完之后(即线程运行完之后)会对该对象"解锁"if (uniqueInstance == null) {lock (locker) {// 如果类的实例不存在则创建,否则直接返回if (uniqueInstance == null) { uniqueInstance = new DB_PreCommand(); } } }return uniqueInstance; }
}}
调用也是非常的方便
//读取var MCP11 = PLC_Connect.Instance("6.6.6.1");if (MCP11 != null) {DB_PreCommand entity1 = DB_PreCommand.GetInstance();CustomerTaskControl taskControl = new CustomerTaskControl();DB_PreCommand entity = (DB_PreCommand)taskControl.GetClassAll(MCP11, entity1, 142);
//写入private void button1_Click(object sender, EventArgs e) {var MCP11 = PLC_Connect.Instance("6.6.6.1");
if (MCP11 != null) {DB_PreCommand entity1 = DB_PreCommand.GetInstance();CustomerTaskControl taskControl = new CustomerTaskControl();
entity1.Task_No = (uint)Convert.ToInt16(textBox1.Text); taskControl.SetClassAll(MCP11, entity1,142);
}
}
类传到列表或Datatable中的方法还不了解,现在用的最笨的方法
DB_PreCommand et = null; et = entity; DataTable dt = new DataTable();// 1、第一步:创建 DataTable 变量 dt.Columns.Add("列1");//2、第二步:添加表头 dt.Columns.Add("列2");//2、第二步:添加表头
try { dt.Rows.Add("Task_No", et.Task_No.ToString()); dt.Rows.Add("Job_Style", et.Job_Style.ToString()); dt.Rows.Add("Inlet_Station_No", et.Inlet_Station_No.ToString()); dt.Rows.Add("Load_Laneway_No", et.Load_Laneway_No.ToString()); dt.Rows.Add(" UnLoad_Laneway_No", et.UnLoad_Laneway_No.ToString()); dt.Rows.Add("Outlet_Station_No", et.Outlet_Station_No.ToString()); dt.Rows.Add("Load_Line", et.Load_Line.ToString()); dt.Rows.Add("Load_Col", et.Load_Col.ToString()); dt.Rows.Add("Load_Layer", et.Load_Layer.ToString());
dt.Rows.Add("Unload_Line", et.Unload_Line.ToString()); dt.Rows.Add("Unload_Col", et.Unload_Col.ToString()); dt.Rows.Add("Unload_Layer", et.Load_Layer.ToString());
dt.Rows.Add("Product_Size", et.Product_Size.ToString()); dt.Rows.Add("Product_Weight", et.Product_Weight.ToString()); dt.Rows.Add("Verify", et.Verify.ToString());
dataGridView1.DataSource = dt;//4、第四步:绑定数据源 }catch (Exception) {
MessageBox.Show("检查输入"); }// 让 DataGridView1 的所有列宽自动调整一下。 dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); |