You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
6.0 KiB
C#
176 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Linq;
|
|
using System.Data;
|
|
using System.Threading;
|
|
using Mesnac.Action.Base;
|
|
using Mesnac.Controls.Base;
|
|
using System.Windows.Forms;
|
|
using Mesnac.Codd.Session;
|
|
using System.IO;
|
|
using Mesnac.Equips;
|
|
using Mesnac.Action.Feeding.Qingquan.Sys;
|
|
|
|
namespace Mesnac.Action.Feeding.Qingquan.BasicInfo
|
|
{
|
|
/// <summary>
|
|
/// 把PLCData表中PLC点的值写入PLC
|
|
/// </summary>
|
|
public class DatabaseToPlc
|
|
{
|
|
/// <summary>
|
|
/// 获取本地连接
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private DbHelper getLocalHelper()
|
|
{
|
|
return new DatabaseAction().NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
|
|
}
|
|
private class PlcData
|
|
{
|
|
public PlcData(string plcKey)
|
|
{
|
|
this.PlcKey = plcKey;
|
|
this.PlcName = string.Empty;
|
|
this.DataLen = 0;
|
|
this.Data = new int[0];
|
|
}
|
|
public string PlcKey { get; private set; }
|
|
public string PlcName { get; set; }
|
|
public int DataLen { get; set; }
|
|
public int[] Data { get; set; }
|
|
}
|
|
private string RowValue(object obj, string defaultValue)
|
|
{
|
|
string Result = defaultValue;
|
|
if (obj != null && obj != DBNull.Value)
|
|
{
|
|
return obj.ToString();
|
|
}
|
|
return defaultValue;
|
|
}
|
|
private int RowValue(object obj, int defaultValue)
|
|
{
|
|
int Result = defaultValue;
|
|
if (obj != null && obj != DBNull.Value && int.TryParse(obj.ToString(), out Result))
|
|
{
|
|
return Result;
|
|
}
|
|
return Result;
|
|
}
|
|
/// <summary>
|
|
/// 把PLCData表中PLC点的值写入PLC业务入口
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Run()
|
|
{
|
|
//1、从PlCdata表中读取要写入PLC的数据项
|
|
BaseAction action = new BaseAction();
|
|
Dictionary<string, PlcData> plcDataLst = new Dictionary<string, PlcData>();
|
|
DbHelper dbHelper = getLocalHelper();
|
|
dbHelper.CommandType = CommandType.Text;
|
|
dbHelper.CommandText = "SELECT * FROM PlcData ORDER BY ObjID";
|
|
DataTable dt = dbHelper.ToDataTable();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
string plcKey = RowValue(dr["EquipRunName"], string.Empty);
|
|
PlcData plcData = null;
|
|
if (plcDataLst.ContainsKey(plcKey))
|
|
{
|
|
plcDataLst.TryGetValue(plcKey, out plcData);
|
|
}
|
|
else
|
|
{
|
|
plcData = new PlcData(plcKey);
|
|
plcData.PlcName = RowValue(dr["EquipRunName"], string.Empty);
|
|
plcDataLst.Add(plcKey, plcData);
|
|
}
|
|
int ilen = RowValue(dr["PlcDataIndex"], 0) + 1;
|
|
if (ilen > plcData.DataLen)
|
|
{
|
|
plcData.DataLen = ilen;
|
|
}
|
|
}
|
|
foreach (PlcData plcData in plcDataLst.Values)
|
|
{
|
|
plcData.Data = new int[plcData.DataLen];
|
|
}
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
string plcKey = RowValue(dr["EquipRunName"], string.Empty);
|
|
PlcData plcData = null;
|
|
if (plcDataLst.ContainsKey(plcKey))
|
|
{
|
|
plcDataLst.TryGetValue(plcKey, out plcData);
|
|
}
|
|
else
|
|
{
|
|
plcData = new PlcData(plcKey);
|
|
plcData.PlcName = RowValue(dr["EquipRunName"], string.Empty);
|
|
plcDataLst.Add(plcKey, plcData);
|
|
}
|
|
|
|
int index = RowValue(dr["PlcDataIndex"], 0);
|
|
plcData.Data[index] = RowValue(dr["PlcDataValue"], 0);
|
|
}
|
|
foreach (KeyValuePair<string, PlcData> plcData in plcDataLst)
|
|
{
|
|
BasicInfo.PlcData.DataKeyValue dataKey = null;
|
|
if (string.IsNullOrWhiteSpace(plcData.Value.PlcName))
|
|
{
|
|
dataKey = BasicInfo.PlcData.Instance.GetDataKeyValue(plcData.Key);
|
|
if (dataKey == null)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
int[] data = plcData.Value.Data;
|
|
int len = 0;
|
|
const int onelen = 100;
|
|
while (true)
|
|
{
|
|
int sendlen = 0;
|
|
if (data.Length - len > onelen)
|
|
{
|
|
sendlen = onelen;
|
|
}
|
|
else
|
|
{
|
|
sendlen = data.Length - len;
|
|
}
|
|
if (sendlen <= 0)
|
|
{
|
|
break;
|
|
}
|
|
object[] downdata = new object[sendlen];
|
|
for (int i = 0; i < sendlen; i++)
|
|
{
|
|
downdata[i] = data[len + i];
|
|
}
|
|
if (dataKey != null)
|
|
{
|
|
if (!BasicInfo.PlcData.Instance.PlcWriteByDataKey(dataKey, len, downdata))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!BasicInfo.PlcData.Instance.PlcWriteByRunName(plcData.Value.PlcName, len, downdata))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
len += sendlen;
|
|
}
|
|
}
|
|
|
|
dbHelper.CommandText = "TRUNCATE TABLE PlcData";
|
|
dbHelper.ExecuteNonQuery();
|
|
return true;
|
|
}
|
|
}
|
|
}
|