|
|
@@ -31,6 +31,10 @@ namespace UAS_PLCDataReader
|
|
|
List<string> SQL = new List<string>();
|
|
|
//存放所有的设备返回的数据,用于比较
|
|
|
public static Dictionary<string, Entity.Device> ReturnData = new Dictionary<string, Entity.Device>();
|
|
|
+ /// <summary>
|
|
|
+ /// 需要更新的设备列表
|
|
|
+ /// </summary>
|
|
|
+ public static Queue<Entity.Device> QueueUpdateDevice = new Queue<Entity.Device>();
|
|
|
|
|
|
#region 初始化代码
|
|
|
public Main()
|
|
|
@@ -124,6 +128,8 @@ namespace UAS_PLCDataReader
|
|
|
ButtonAddNetConfig.Grid = GridDeviceNetSetting;
|
|
|
ButtonSaveNetConfig.Grid = GridDeviceNetSetting;
|
|
|
|
|
|
+ TimerUpdateDevice.Start();
|
|
|
+
|
|
|
Ptime = new Dictionary<int, PollingTimer>();
|
|
|
}
|
|
|
/// <summary>
|
|
|
@@ -276,6 +282,32 @@ namespace UAS_PLCDataReader
|
|
|
}
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设备数据更新列表
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
+ private void TimerUpdateDevice_Tick(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ Console.WriteLine("Begin" + QueueUpdateDevice.Count);
|
|
|
+ while (QueueUpdateDevice.Count > 0)
|
|
|
+ {
|
|
|
+ Entity.Device item = QueueUpdateDevice.Dequeue();
|
|
|
+ SQL.Clear();
|
|
|
+ //更新轮询状态
|
|
|
+ sql.Clear();
|
|
|
+ sql.Append("update DEVICEPOLLINGCONFIG set dpc_status='Running' where dpc_decode='" + item.Decode + "' and dpc_dccode='" + item.Dccode + "'");
|
|
|
+ dh.ExecuteSql(sql.ToString(), "update");
|
|
|
+ //更新轮询日志状态
|
|
|
+ sql.Clear();
|
|
|
+ sql.Append("update DEVICEPOLLINGLOG set dpg_status='Running',dpg_senddatasize=nvl(dpg_senddatasize,0)+" + item.SendDataSize);
|
|
|
+ sql.Append(",dpg_receivedatasize=nvl(dpg_receivedatasize,0)+" + item.ReceiveDataSize + ",dpg_count=nvl(dpg_count,0)+1 ");
|
|
|
+ sql.Append(" where dpg_id=(select max(dpg_id) from DEVICEPOLLINGLOG where dpg_decode='" + item.Decode + "')");
|
|
|
+ dh.ExecuteSql(sql.ToString(), "update");
|
|
|
+ }
|
|
|
+ Console.WriteLine("End" + QueueUpdateDevice.Count);
|
|
|
+ }
|
|
|
#endregion
|
|
|
|
|
|
#region PageCommandSet业务代码(指令设置)
|
|
|
@@ -398,6 +430,7 @@ namespace UAS_PLCDataReader
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ Dictionary<string, ModBusTCPClient> client = new Dictionary<string, ModBusTCPClient>();
|
|
|
/// <summary>
|
|
|
/// 轮询执行的业务
|
|
|
/// </summary>
|
|
|
@@ -410,6 +443,7 @@ namespace UAS_PLCDataReader
|
|
|
DataHelper dh = pl.Dh;
|
|
|
string Decode = pl.DeviceCode;
|
|
|
string Dccode = pl.CommandCode;
|
|
|
+ string DpcID = pl.Id.ToString();
|
|
|
DataTable dt = (DataTable)dh.ExecuteSql("select dnc_ip,dnc_port from DEVICENETCONFIG where dnc_decode='" + Decode + "'", "select");
|
|
|
DataTable dt1 = (DataTable)dh.ExecuteSql("select dc_sendcoding,dc_value,dc_receivecoding from devicecommand where dc_code='" + Dccode + "'", "select");
|
|
|
if (dt.Rows.Count > 0)
|
|
|
@@ -426,20 +460,49 @@ namespace UAS_PLCDataReader
|
|
|
Command = dt1.Rows[0]["dc_value"].ToString();
|
|
|
SendCommandByteSize = Encoding.Default.GetBytes(Command.ToCharArray()).Length;
|
|
|
}
|
|
|
+ if (client.ContainsKey(DpcID))
|
|
|
+ {
|
|
|
+ client[DpcID].Send(Command);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ModBusTCPClient modclient = new ModBusTCPClient(dt.Rows[0]["dnc_ip"].ToString(), int.Parse(dt.Rows[0]["dnc_port"].ToString()));
|
|
|
+ client.Add(DpcID, modclient);
|
|
|
+ }
|
|
|
//发送指令
|
|
|
- mbt.Send(IP, SendCoding, ReceiveCoding, Command);
|
|
|
+ //mbt.Send(IP, SendCoding, ReceiveCoding, Command);
|
|
|
+ //如果不包含该项数据则在键值对中添加
|
|
|
+ if (!ReturnData.ContainsKey(Decode))
|
|
|
+ {
|
|
|
+ //添加本地数据内容
|
|
|
+ Entity.Device device = new Entity.Device();
|
|
|
+ device.Decode = pl.DeviceCode;
|
|
|
+ device.Dename = pl.DeviceName;
|
|
|
+ device.StartTime = DateTime.Now;
|
|
|
+ device.RunStatus = "Running";
|
|
|
+ device.SendDataSize += SendCommandByteSize;
|
|
|
+ device.SendCount += 1;
|
|
|
+ device.Dccode = pl.CommandCode;
|
|
|
+ ReturnData.Add(Decode, device);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //每次更新轮询的数据
|
|
|
+ ReturnData[Decode].SendDataSize += SendCommandByteSize;
|
|
|
+ ReturnData[Decode].SendCount += 1;
|
|
|
+ //检测键值对是否发生变化,发生变化时赋予新值
|
|
|
+ }
|
|
|
//成功返回了信息
|
|
|
- if (mbt.Returnvalue.ContainsKey(IP))
|
|
|
+ if (client[DpcID].Returnvalue.ContainsKey(IP))
|
|
|
{
|
|
|
- int ReceiveCommandByteSize = Encoding.Default.GetBytes(mbt.Returnvalue[IP].ToCharArray()).Length;
|
|
|
+ int ReceiveCommandByteSize = Encoding.Default.GetBytes(client[DpcID].Returnvalue[IP].ToCharArray()).Length;
|
|
|
//存放返回的所有数据
|
|
|
Dictionary<string, string> ItemData = new Dictionary<string, string>();
|
|
|
- int[] Arr = BaseUtil.GetDecimalData(BaseUtil.ASCIIToString(mbt.Returnvalue[IP]), 8);
|
|
|
+ int[] Arr = BaseUtil.GetDecimalData(BaseUtil.ASCIIToString(client[DpcID].Returnvalue[IP]), 8);
|
|
|
for (int i = 0; i < Arr.Length; i++)
|
|
|
{
|
|
|
ItemData.Add("Item" + i, Arr[i].ToString());
|
|
|
}
|
|
|
- //如果不包含该项数据则在键值对中添加
|
|
|
if (!ReturnData.ContainsKey(Decode))
|
|
|
{
|
|
|
//添加本地数据内容
|
|
|
@@ -449,23 +512,27 @@ namespace UAS_PLCDataReader
|
|
|
device.StartTime = DateTime.Now;
|
|
|
device.RunStatus = "Running";
|
|
|
device.ReceiveDataSize += ReceiveCommandByteSize;
|
|
|
- device.SendDataSize += SendCommandByteSize;
|
|
|
- device.SendCount += 1;
|
|
|
+ device.Dccode = pl.CommandCode;
|
|
|
device.ItemData = ItemData;
|
|
|
ReturnData.Add(Decode, device);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ if (ReturnData[Decode].ItemData == null)
|
|
|
+ {
|
|
|
+ ReturnData[Decode].ItemData = ItemData;
|
|
|
+ }
|
|
|
+ //每次更新轮询的数据
|
|
|
ReturnData[Decode].ReceiveDataSize += ReceiveCommandByteSize;
|
|
|
- ReturnData[Decode].SendDataSize += SendCommandByteSize;
|
|
|
- ReturnData[Decode].SendCount += 1;
|
|
|
//检测键值对是否发生变化,发生变化时赋予新值
|
|
|
if ((BaseUtil.CheckDicDiff(ReturnData[Decode].ItemData, ItemData)))
|
|
|
{
|
|
|
ReturnData[Decode].ItemData = ItemData;
|
|
|
+ QueueUpdateDevice.Enqueue(ReturnData[Decode]);
|
|
|
+ LogicHandler.DoDeviceDataDiffLog(pl.DeviceCode, pl.DeviceName, pl.CommandCode, User.UserName);
|
|
|
}
|
|
|
}
|
|
|
- mbt.Returnvalue.Remove(IP);
|
|
|
+ client[DpcID].Returnvalue.Remove(IP);
|
|
|
//SQL.Clear();
|
|
|
////更新轮询状态
|
|
|
//sql.Clear();
|
|
|
@@ -508,6 +575,7 @@ namespace UAS_PLCDataReader
|
|
|
}
|
|
|
PollSettingPaintRowIndex.Clear();
|
|
|
GridPollingSetting.Focus();
|
|
|
+ client.Clear();
|
|
|
Ptime.Clear();
|
|
|
}
|
|
|
|
|
|
@@ -518,22 +586,14 @@ namespace UAS_PLCDataReader
|
|
|
GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value);
|
|
|
if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0")
|
|
|
{
|
|
|
- //DialogResult cancel = XtraMessageBox.Show("确认禁用该轮询?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
|
|
- //if (cancel.ToString() == "Yes")
|
|
|
- //{
|
|
|
int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
|
|
|
if (Ptime.ContainsKey(id))
|
|
|
{
|
|
|
Ptime[id].Stop();
|
|
|
Ptime.Remove(id);
|
|
|
+ client.Remove(id.ToString());
|
|
|
PollSettingPaintRowIndex.Remove(e.RowHandle);
|
|
|
}
|
|
|
- //}
|
|
|
- //else
|
|
|
- //{
|
|
|
- // Cancel = false;
|
|
|
- // GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, -1);
|
|
|
- //}
|
|
|
}
|
|
|
else
|
|
|
{
|