一厂MES,含注塑,喷涂,冲孔
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

344 lines
12 KiB

using OPCAutomation;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
namespace PunchingMistake
{
public class PlcInvoke
{
private OPCServer opcServer;
private OPCGroups opcGroups;
private OPCGroup opcGroup1;
private OPCItems opcItems;
private static OPCItem[] opcItemm1;
public static bool _linkDev = true;
public Action<bool, bool> ShowLinkInfo = null;
//public Action<string> ShowScanInfo = null;
public Action<string, bool> ShowMessage = null;
public event Action<string> DevAfterReturnResult = null;
//private Entity_t_PreAssembleLine t_PreAssembleLine = new Entity_t_PreAssembleLine();
/// <summary>
/// 接收到的值
/// </summary>
public Dictionary<int, string> _OpcAddressDict = new Dictionary<int, string>();
public DataTable _OpcAddressTable = new DataTable();
private string _channelDev = string.Empty;
public bool AllowReceive = false;
public string _stationNo;
private string _lineRowID = "";
public PlcInvoke(string channelDev, string stationNo)
{
_channelDev = channelDev;
_stationNo = stationNo;
}
public void SetLineID(string lineID)
{
_lineRowID = lineID;
}
public bool InitDev(int rate)
{
if (_linkDev == false)
{
return false;
}
try
{
// int rate = 500;
CreateServer();
if (ConnectServer())
{
opcGroups = opcServer.OPCGroups;
opcGroup1 = opcGroups.Add("GRP");
SetGroupProperty(opcGroup1, rate);
opcItems = opcGroup1.OPCItems;
opcItems.DefaultIsActive = true;
LoadOpcItem(_stationNo);
opcGroup1.DataChange += OpcGroup1_DataChange;
ShowLinkInfo(true, true);
}
return true;
}
catch (Exception ex)
{
ShowLinkInfo(false, true);
//MyLogger.Write("设备初始化发生错误,错误原因:" + ex.Message);
LogHelper.WriteLog("设备初始化发生错误,错误原因:" + ex.Message);
//throw new Exception("设备初始化发生错误,错误原因:" + ex.Message);
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="addresses">PLC地址点位</param>
/// <param name="operType">操作类型 冲孔,流钻,EOL,返修</param>
public void SendToPLC( bool isInit,string lineRowID)
{
if(isInit == false)
{
_lineRowID = lineRowID;
foreach (OPCItem item in opcItemm1)
{
if (item.ItemID.Contains("Start"))
{
item.Write(2);
ShowMessage("发送启动信号:" + 2, true);
}
//if (item.ItemID.Contains("caseNo"))
//{
// item.Write(caseNo);
// ShowMessage("发送料盒号:" + caseNo, true);
//}
}
ShowMessage("发送启动信号成功", true);
}
else
{
foreach (OPCItem item in opcItemm1)
{
if (item.ItemID.Contains("Start"))
{
item.Write(1);
}
if (item.ItemID.Contains("ScrewL"))
{
item.Write(0);
}
if (item.ItemID.Contains("ScrewR"))
{
item.Write(0);
}
if (item.ItemID.Contains("RivetL"))
{
item.Write(0);
}
if (item.ItemID.Contains("RivetR"))
{
item.Write(0);
}
if (item.ItemID.Contains("IrIs"))
{
item.Write(0);
}
//if (item.ItemID.Contains("NutL"))
//{
// item.Write(0);
//}
//if (item.ItemID.Contains("NutR"))
//{
// item.Write(0);
//}
//螺钉类型 不用初始化
}
ShowMessage("发送初始化设备信号成功", true);
}
AllowReceive = true;
}
public void WriteToPLCTest()
{
foreach (OPCItem item in opcItemm1)
{
if (item.ItemID.Contains("ScrewL"))
{
item.Write(8);
}
if (item.ItemID.Contains("ScrewR"))
{
item.Write(8);
}
if (item.ItemID.Contains("RivetL"))
{
item.Write(8);
}
if (item.ItemID.Contains("RivetR"))
{
item.Write(8);
}
if (item.ItemID.Contains("IrIs"))
{
item.Write(8);
}
//if (item.ItemID.Contains("NutL"))
//{
// item.Write(8);
//}
//if (item.ItemID.Contains("NutR"))
//{
// item.Write(8);
//}
//螺钉类型 不用初始化
}
}
void LoadOpcItem(string stationNo)
{
Dictionary<int, string> dict = GetPlcAddress();// GetDevInfo();
opcItemm1 = new OPCItem[dict.Count];
int i = 0;
foreach (var dictItem in dict)
{
opcItemm1[i] = opcItems.AddItem(dictItem.Value, dictItem.Key);
i++;
}
}
public Dictionary<int, string> GetPlcAddress()
{
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(0, _channelDev + ".LinkRate");
dict.Add(1, _channelDev + ".Start");
dict.Add(2, _channelDev + ".ScrewL");
dict.Add(3, _channelDev + ".ScrewR");
dict.Add(4, _channelDev + ".RivetL");
dict.Add(5, _channelDev + ".RivetR");
dict.Add(6, _channelDev + ".IrIs");
//dict.Add(7, _channelDev + ".caseNo");
//dict.Add(8, _channelDev + ".NutL");
//dict.Add(9, _channelDev + ".NutR");
return dict;
}
private string GetOpcName(DataRow row)
{
return $"{row["ChannelName"].ToString()}.{row["DevName"].ToString()}.{row["LableName"].ToString()}";
}
private void OpcGroup1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
if (_linkDev == false) return;
for (int i = 1; i <= NumItems; i++)
{
int handle = Convert.ToInt32(ClientHandles.GetValue(i).ToString().Trim());
string itemValue = ItemValues.GetValue(i).ToString();
string address = opcItemm1[(handle)].ItemID;
if (!string.IsNullOrEmpty(_lineRowID))
{
if (address.Contains("ScrewL"))
{
Function2.UpdateLineCount("LeftScrew1=" + itemValue, _lineRowID);
}
else if (address.Contains("ScrewR"))
{
Function2.UpdateLineCount("RightScrew1=" + itemValue, _lineRowID);
}
else if (address.Contains("RivetL"))
{
Function2.UpdateLineCount("LeftRivet1=" + itemValue, _lineRowID);
}
else if (address.Contains("RivetR"))
{
Function2.UpdateLineCount("RightRivet1=" + itemValue, _lineRowID);
}
else if (address.Contains("IrIs"))
{
Function2.UpdateLineCount("IrIs=" + itemValue, _lineRowID);
}
//else if (address.Contains("NutL"))
//{
// Function2.UpdateLineCount("LeftNut=" + itemValue, _lineRowID);
//}
//else if (address.Contains("NutR"))
//{
// Function2.UpdateLineCount("RightNut=" + itemValue, _lineRowID);
//}
}
}
}
catch (Exception ex)
{
//MyLogger.Write("设备返回数据发生异常,错误原因:" + ex.Message);
LogHelper.WriteLog("设备返回数据发生异常,错误原因:" + ex.Message);
ShowMessage("设备返回数据发生异常,错误原因:" + ex.Message, false);
}
}
/// <summary>
/// 创建服务
/// </summary>
/// <returns></returns>
private void CreateServer()
{
try
{
opcServer = new OPCServer();
}
catch (Exception ex)
{
throw new Exception("创建OpcServer时发生异常,异常信息:" + ex.Message);
}
}
/// <summary>
/// 连接到服务器
/// </summary>
/// <param name="strHostIP"></param>
/// <param name="strHostName"></param>
/// <returns></returns>
private bool ConnectServer()
{
try
{
opcServer.Connect("Kepware.KEPServerEX.V6", "");
ShowLinkResult(true, true);
}
catch (Exception ex)
{
ShowLinkResult(false, true);
//ShowScanResult(false);
LogHelper.WriteLog($"设备连接异常,异常原因:" + ex.Message);
throw new Exception($"设备连接异常");
}
return true;
}
/// <summary>
/// 设置组的属性
/// </summary>
/// <param name="opcGroup"></param>
/// <param name="updateRate"></param>
private void SetGroupProperty(OPCGroup opcGroup, int updateRate)
{
opcGroup.IsActive = true;
opcGroup.DeadBand = 0;
opcGroup.UpdateRate = updateRate;
opcGroup.IsSubscribed = true;
}
private void ShowLinkResult(bool isSuccess, bool isWriteLog)
{
if (ShowLinkInfo != null)
{
ShowLinkInfo(isSuccess, isWriteLog);
}
}
}
}