[성현모] CPXV2 Init
This commit is contained in:
@ -0,0 +1,420 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Net.Platform.Common.Event;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
using static SystemX.PLC.Interface.PLCCommDefinition;
|
||||
|
||||
namespace SystemX.PLC.Interface.MxComponent
|
||||
{
|
||||
public class PLCCommMxCMgr : PLCCommManager
|
||||
{
|
||||
private int TimeOut;
|
||||
|
||||
public Dictionary<ePLCRWType, PLCModMxCMgr> PLCManagers { get; set; } = new Dictionary<ePLCRWType, PLCModMxCMgr>();
|
||||
public FunctionEventHandler EventAlwaysUpdate { get; set; }
|
||||
|
||||
public PLCCommMxCMgr(string strPLCConnInfoXmlPath) : base(strPLCConnInfoXmlPath)
|
||||
{
|
||||
AddrMgr = new PLCAddressManager(strPLCConnInfoXmlPath);
|
||||
|
||||
EventAlwaysUpdate = new FunctionEventHandler();
|
||||
EventChangeUpdate = new FunctionEventHandler();
|
||||
|
||||
TimeOut = 0;
|
||||
|
||||
foreach (PLCConnectionInfo coninfo in AddrMgr.ConnInfoDictionary.Values)
|
||||
{
|
||||
if (TimeOut < int.Parse(coninfo.Timeout))
|
||||
TimeOut = int.Parse(coninfo.Timeout);
|
||||
|
||||
PLCManagers.Add((ePLCRWType)Enum.Parse(typeof(ePLCRWType), coninfo.RWType), new PLCModMxCMgr(coninfo, EventAlwaysUpdate, EventChangeUpdate));
|
||||
}
|
||||
}
|
||||
|
||||
public override bool OpenPLCConnection()
|
||||
{
|
||||
Task<bool> openconn = new Task<bool>(() => TryOpenPLCConnection());
|
||||
|
||||
openconn.Start();
|
||||
|
||||
bool bResult = openconn.Result;
|
||||
|
||||
if (bResult)
|
||||
{
|
||||
//Task plcoperation = openconn.ContinueWith(x => StartPLCOperation());
|
||||
Task<bool> plcoperation = new Task<bool>(() => StartPLCOperation());
|
||||
|
||||
plcoperation.Start();
|
||||
|
||||
bResult = plcoperation.Result;
|
||||
}
|
||||
|
||||
return bResult;
|
||||
|
||||
/*
|
||||
if (plcoperation.Wait(TimeOut))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
*/
|
||||
}
|
||||
|
||||
public bool TryOpenPLCConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (PLCModMxCMgr conmgr in PLCManagers.Values)
|
||||
{
|
||||
if (!conmgr.OpenDevice())
|
||||
return false;
|
||||
|
||||
Stopwatch stTimeOut = new Stopwatch();
|
||||
stTimeOut.Start();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (conmgr.IsOpened == true)
|
||||
break;
|
||||
|
||||
if (stTimeOut.ElapsedMilliseconds >= TimeOut)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("PLC Connection Waiting TimeOut.", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
|
||||
SetAddress(conmgr, AddrMgr.OperationDictionary, true);
|
||||
SetAddress(conmgr, AddrMgr.DataDictionary, false);
|
||||
}
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite("PLC Connection Waiting for Success.", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Open Error.\n - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool findType(ePLCRWType type)
|
||||
{
|
||||
var findValue = PLCManagers.FirstOrDefault(t => t.Key == type);
|
||||
|
||||
if (findValue.Value != null)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
private PLCModMxCMgr findMgr(ePLCRWType type)
|
||||
{
|
||||
var findValue = PLCManagers.FirstOrDefault(t => t.Key == type);
|
||||
|
||||
if (findValue.Value != null)
|
||||
return findValue.Value;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public override bool StartPLCOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
OperationStarted = true;
|
||||
|
||||
if(findType(ePLCRWType.Read))
|
||||
{
|
||||
findMgr(ePLCRWType.Read).SingleScanStartAsync();
|
||||
findMgr(ePLCRWType.Read).StartMonitor();
|
||||
}
|
||||
if (findType(ePLCRWType.ReadWrite))
|
||||
{
|
||||
findMgr(ePLCRWType.ReadWrite).SingleScanStartAsync();
|
||||
findMgr(ePLCRWType.ReadWrite).StartMonitor();
|
||||
}
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite("PLC Operation has been Started.", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Open Error.\n - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void NowScanDataArea()
|
||||
{
|
||||
if (findType(ePLCRWType.Read))
|
||||
findMgr(ePLCRWType.Read).NowScanDataArea();
|
||||
if (findType(ePLCRWType.ReadWrite))
|
||||
findMgr(ePLCRWType.ReadWrite).NowScanDataArea();
|
||||
}
|
||||
|
||||
public override bool StopPLCOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
OperationStarted = false;
|
||||
|
||||
if (findType(ePLCRWType.Read))
|
||||
findMgr(ePLCRWType.Read).StopMonitor();
|
||||
if (findType(ePLCRWType.ReadWrite))
|
||||
findMgr(ePLCRWType.ReadWrite).StopMonitor();
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite("PLC Operation has been Stopped.", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Open Error.\n - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ClosePLCConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (PLCModMxCMgr conmgr in PLCManagers.Values)
|
||||
conmgr.CloseDevice();
|
||||
|
||||
OperationStarted = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Close Error.\n - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceivePLCEvent(PLCModMxCMgr sender, PLCAddressTemplate template)
|
||||
{
|
||||
if (template == null)
|
||||
return;
|
||||
|
||||
if (IsMySubscription(template))
|
||||
{
|
||||
ReceivePLCEvent(template.Address, template.RawValue[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReceivePLCEvent(string address, int value)
|
||||
{
|
||||
}
|
||||
|
||||
public bool IsMySubscription(PLCAddressTemplate template)
|
||||
{
|
||||
if (AddrMgr.OperationDictionary.ContainsValue(template))
|
||||
return true;
|
||||
if (AddrMgr.DataDictionary.ContainsValue(template))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void SetAddress(PLCModMxCMgr module, Dictionary<string, PLCAddressTemplate> addressInfo, bool bOperation)
|
||||
{
|
||||
foreach (PLCAddressTemplate addrinfo in addressInfo.Values)
|
||||
module.AddDevices(addrinfo, bOperation);
|
||||
}
|
||||
|
||||
public override void UpdateAll(bool bRefineWrongChar)
|
||||
{
|
||||
foreach (PLCModMxCMgr conmgr in PLCManagers.Values)
|
||||
{
|
||||
conmgr.UpdateArea(AddrMgr.OperationDictionary.Values.ToList(), bRefineWrongChar);
|
||||
conmgr.UpdateArea(AddrMgr.DataDictionary.Values.ToList(), bRefineWrongChar);
|
||||
}
|
||||
}
|
||||
|
||||
public override void SelectUpdate(ePLCAreaType pat, PLCAddressTemplate tmplt, bool bRefineWrongChar)
|
||||
{
|
||||
if (pat == ePLCAreaType.Operation)
|
||||
{
|
||||
foreach (PLCModMxCMgr conmgr in PLCManagers.Values)
|
||||
{
|
||||
conmgr.UpdateSelectArea(tmplt, bRefineWrongChar);
|
||||
//conmgr.UpdateArea(AddrMgr.DataDictionary.Values.ToList(), bRefineWrongChar);
|
||||
}
|
||||
}
|
||||
if (pat == ePLCAreaType.Data)
|
||||
{
|
||||
foreach (PLCModMxCMgr conmgr in PLCManagers.Values)
|
||||
{
|
||||
//conmgr.UpdateArea(AddrMgr.OperationDictionary.Values.ToList(), bRefineWrongChar);
|
||||
conmgr.UpdateSelectArea(tmplt, bRefineWrongChar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string GetDeviceValue(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckConnectionState(tmplt))
|
||||
return string.Empty;
|
||||
|
||||
string strReadValue = "";
|
||||
|
||||
if (findType(ePLCRWType.Read))
|
||||
strReadValue = findMgr(ePLCRWType.Read).GetDeviceValue(tmplt);
|
||||
else if (findType(ePLCRWType.ReadWrite))
|
||||
strReadValue = findMgr(ePLCRWType.ReadWrite).GetDeviceValue(tmplt);
|
||||
|
||||
return strReadValue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Manager is failed to Get Device Value. The Address: {tmplt.AddressKey} has not been read by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ReadDevice(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckConnectionState(tmplt))
|
||||
return string.Empty;
|
||||
|
||||
string strReadValue = "";
|
||||
|
||||
if (findType(ePLCRWType.Read))
|
||||
strReadValue = findMgr(ePLCRWType.Read).ReadDevice(tmplt);
|
||||
else if (findType(ePLCRWType.ReadWrite))
|
||||
strReadValue = findMgr(ePLCRWType.ReadWrite).ReadDevice(tmplt);
|
||||
|
||||
return strReadValue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Manager is failed to Read Device. The Address: {tmplt.AddressKey} has not been read by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ReadDevice(PLCAddressTemplate tmplt, bool bAsciiReverse = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckConnectionState(tmplt))
|
||||
return string.Empty;
|
||||
|
||||
string strReadValue = "";
|
||||
|
||||
if (findType(ePLCRWType.Read))
|
||||
strReadValue = findMgr(ePLCRWType.Read).ReadDevice(tmplt, bAsciiReverse);
|
||||
else if (findType(ePLCRWType.ReadWrite))
|
||||
strReadValue = findMgr(ePLCRWType.ReadWrite).ReadDevice(tmplt, bAsciiReverse);
|
||||
|
||||
return strReadValue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Manager is failed to Read Device. The Address: {tmplt.AddressKey} has not been read by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool WriteDevice(PLCAddressTemplate tmplt, string strValue, bool bVerification = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!CheckConnectionState(tmplt))
|
||||
return false;
|
||||
|
||||
if (findType(ePLCRWType.Write))
|
||||
findMgr(ePLCRWType.Write).WriteDevice(tmplt, strValue);
|
||||
else if (findType(ePLCRWType.ReadWrite))
|
||||
findMgr(ePLCRWType.ReadWrite).WriteDevice(tmplt, strValue);
|
||||
|
||||
if (bVerification)
|
||||
{
|
||||
string strReadValue = "";
|
||||
|
||||
if (findType(ePLCRWType.Read))
|
||||
strReadValue = findMgr(ePLCRWType.Read).ReadDevice(tmplt);
|
||||
else if (findType(ePLCRWType.ReadWrite))
|
||||
strReadValue = findMgr(ePLCRWType.ReadWrite).ReadDevice(tmplt);
|
||||
|
||||
if (strReadValue == strValue)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"The Address: {tmplt.PLCAddress} has not been written #Intent:{strValue} - #Actual:{tmplt.Value} by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Manager is failed to Write Device. The Address: {tmplt.AddressKey} has not been written by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Error Message: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckConnectionState(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
int nCount = 0;
|
||||
bool bConError = false;
|
||||
if (!OperationStarted)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (OperationStarted || nCount > 100)
|
||||
{
|
||||
bConError = true;
|
||||
break;
|
||||
}
|
||||
|
||||
nCount++;
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
if (bConError == true)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Trouble has been detected. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($"The Address: {tmplt.PLCAddress} has not been written. ", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,314 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Net.Platform.Common.Event;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
using static SystemX.PLC.Interface.PLCCommDefinition;
|
||||
|
||||
namespace SystemX.PLC.Interface.MxComponent
|
||||
{
|
||||
public class PLCDeviceMXC
|
||||
{
|
||||
public FunctionEventHandler EventAlwaysUpdate { get; set; }
|
||||
public FunctionEventHandler EventChangeUpdate { get; set; }
|
||||
|
||||
public int STATION_ID { set; get; } //PLC STATION
|
||||
|
||||
private int ScanningIntervalOperation = 100;
|
||||
private int ScanningIntervalDataTime = 500;
|
||||
|
||||
private bool MonitorExe = true;
|
||||
|
||||
private Stopwatch stReadTimeCheck;
|
||||
|
||||
private PLCMxComponent _conn; //MxComponent PLC Instance
|
||||
|
||||
Dictionary<string, PLCAddressTemplate> DicOperationAddresses = new Dictionary<string, PLCAddressTemplate>();
|
||||
Dictionary<string, PLCAddressTemplate> DicDataAddresses = new Dictionary<string, PLCAddressTemplate>();
|
||||
|
||||
Stopwatch xSwDiag = new Stopwatch();
|
||||
|
||||
private static object objLock = new object();
|
||||
|
||||
public PLCDeviceMXC()
|
||||
{
|
||||
stReadTimeCheck = new Stopwatch();
|
||||
stReadTimeCheck.Start();
|
||||
}
|
||||
|
||||
public bool DevClose()
|
||||
{
|
||||
if (_conn != null)
|
||||
{
|
||||
StopMonitor();
|
||||
|
||||
return _conn.Close();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DevOpen()
|
||||
{
|
||||
try
|
||||
{
|
||||
_conn = new PLCMxComponent();
|
||||
|
||||
_conn.Initialize();
|
||||
|
||||
return _conn.Open(STATION_ID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Error during PLC Open : {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddOperationDevices(PLCAddressTemplate deviceReg)
|
||||
{
|
||||
DicOperationAddresses.Add(deviceReg.PLCAddress, deviceReg);
|
||||
|
||||
return true;
|
||||
}
|
||||
public bool AddDataDevices(PLCAddressTemplate deviceReg)
|
||||
{
|
||||
DicDataAddresses.Add(deviceReg.PLCAddress, deviceReg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SingleScanStartAsync()
|
||||
{
|
||||
lock (objLock)
|
||||
{
|
||||
ScanDataArea();
|
||||
|
||||
ScanOperationArea(true);
|
||||
}
|
||||
}
|
||||
public void NowDataAreaScan()
|
||||
{
|
||||
lock (objLock)
|
||||
{
|
||||
ScanDataArea();
|
||||
}
|
||||
}
|
||||
public void StartMonitor()
|
||||
{
|
||||
MonitorExe = true;
|
||||
|
||||
Task<int> monitor = new Task<int>(() => ExecuteMonitor().Result);
|
||||
|
||||
monitor.Start();
|
||||
}
|
||||
|
||||
public void StopMonitor()
|
||||
{
|
||||
MonitorExe = false;
|
||||
}
|
||||
|
||||
private async Task<int> ExecuteMonitor()
|
||||
{
|
||||
//int nCountTime = 0;
|
||||
|
||||
stReadTimeCheck.Restart();
|
||||
|
||||
while (MonitorExe)
|
||||
{
|
||||
lock (objLock)
|
||||
{
|
||||
ScanOperationArea();
|
||||
|
||||
/*
|
||||
if (stReadTimeCheck.ElapsedMilliseconds >= ScanningIntervalDataTime)
|
||||
{
|
||||
ScanDataArea();
|
||||
|
||||
stReadTimeCheck.Restart();
|
||||
}
|
||||
*/
|
||||
|
||||
//
|
||||
|
||||
/*
|
||||
if (nCountTime % ScanningIntervalDataTime == 0)
|
||||
{
|
||||
nCountTime = 1;
|
||||
|
||||
ScanDataArea();
|
||||
}
|
||||
|
||||
nCountTime++;
|
||||
*/
|
||||
}
|
||||
|
||||
await Task.Delay(ScanningIntervalOperation);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ScanDataArea()
|
||||
{
|
||||
foreach (PLCAddressTemplate addrTemplate in DicDataAddresses.Values)
|
||||
UpdateDevice(addrTemplate, true);
|
||||
}
|
||||
|
||||
void ScanOperationArea(bool bSingleScan = false)
|
||||
{
|
||||
foreach (PLCAddressTemplate addrTemplate in DicOperationAddresses.Values)
|
||||
{
|
||||
string strFormerValue = addrTemplate.Value;
|
||||
|
||||
UpdateDevice(addrTemplate, true);
|
||||
|
||||
EventAlwaysUpdate.DoReceive(this, addrTemplate);
|
||||
|
||||
if (addrTemplate.Value != strFormerValue)
|
||||
EventChangeUpdate.DoReceive(this, addrTemplate);
|
||||
|
||||
if (bSingleScan)
|
||||
EventChangeUpdate.DoReceive(this, addrTemplate);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetModuleName()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().ManifestModule.Name.Replace(".dll", string.Empty);
|
||||
}
|
||||
|
||||
Int16[] ReadPLCData(PLCAddressTemplate template)
|
||||
{
|
||||
if (_conn == null)
|
||||
return null;
|
||||
|
||||
int nSize = Convert.ToInt32(template.Length);
|
||||
|
||||
Int16[] readData = new Int16[nSize];
|
||||
|
||||
_conn.BlockReadData(template.PLCAddress, nSize, ref readData);
|
||||
|
||||
return readData;
|
||||
}
|
||||
|
||||
bool WritePLCData(PLCAddressTemplate template, Int16[] arnWriteData)
|
||||
{
|
||||
if (_conn == null)
|
||||
return false;
|
||||
|
||||
int nSize = Convert.ToInt32(template.Length);
|
||||
|
||||
bool ret = _conn.BlockWriteData(template.PLCAddress, nSize, arnWriteData);
|
||||
|
||||
if (ret == false)
|
||||
{
|
||||
_conn.BlockWriteData(template.PLCAddress, nSize, arnWriteData);
|
||||
|
||||
Thread.Sleep(30);
|
||||
}
|
||||
|
||||
Thread.Sleep(30);
|
||||
|
||||
//bool ret = PLC_COMM.WriteData(addr, nSize, arnWriteData);
|
||||
return ret;
|
||||
}
|
||||
|
||||
List<int> GetReadValues(PLCAddressTemplate tmplt)
|
||||
{
|
||||
int nLength = Convert.ToInt32(tmplt.Length);
|
||||
|
||||
List<int> vnValues = (from value in ReadPLCData(tmplt) let value32 = Convert.ToInt32(value) select value32).ToList();
|
||||
|
||||
return vnValues;
|
||||
}
|
||||
|
||||
List<short> GetWriteValues(PLCAddressTemplate tmplt, string strValue)
|
||||
{
|
||||
List<short> vnValues = new List<short>();
|
||||
|
||||
int nLength = Convert.ToInt32(tmplt.Length);
|
||||
|
||||
eDataType eType = (eDataType)Enum.Parse(typeof(eDataType), tmplt.DataType);
|
||||
|
||||
if (eType == eDataType.ASCII)
|
||||
vnValues = (from value in CommonUtil.HexStringToBytes(strValue) select Convert.ToInt16(value)).ToList();
|
||||
else if (eType == eDataType.DECIMAL)
|
||||
{
|
||||
int nInputValue = string.IsNullOrWhiteSpace(strValue) ? 0 : Convert.ToInt16(strValue);
|
||||
vnValues.Add(Convert.ToInt16(nInputValue));
|
||||
}
|
||||
|
||||
return vnValues;
|
||||
}
|
||||
|
||||
void UpdateValue(PLCAddressTemplate tmplt, bool bRefineWrongChar, bool bAsciiReverseBit = false)
|
||||
{
|
||||
List<int> vValues = GetReadValues(tmplt);
|
||||
|
||||
eDataType eType = (eDataType)Enum.Parse(typeof(eDataType), tmplt.DataType);
|
||||
|
||||
string strData = string.Empty;
|
||||
|
||||
if (eType == eDataType.ASCII)
|
||||
strData = CommonUtil.GetStringFromASCII(vValues.ToArray(), bAsciiReverseBit);
|
||||
else if (eType == eDataType.BIT)
|
||||
strData = vValues[0].ToString();
|
||||
else if (eType == eDataType.DECIMAL)
|
||||
strData = vValues[0].ToString();
|
||||
|
||||
string strResult = bRefineWrongChar ? RefineInvalidChars(strData) : strData;
|
||||
|
||||
tmplt.SetStringValue(strResult);
|
||||
}
|
||||
|
||||
string RefineInvalidChars(string strSource)
|
||||
{
|
||||
string strReturn = strSource.Trim(InvalidChars);
|
||||
|
||||
strReturn = new string((from cCode in strSource.ToCharArray() where (48 <= Convert.ToInt32(cCode) && Convert.ToInt32(cCode) <= 95) select cCode).ToArray());
|
||||
|
||||
return strReturn;
|
||||
}
|
||||
|
||||
public string GetDeviceValue(PLCAddressTemplate tmplt)
|
||||
{
|
||||
List<PLCAddressTemplate> Oper = DicOperationAddresses.Values.Where(x => x.DataName == tmplt.DataName).ToList();
|
||||
List<PLCAddressTemplate> Data = DicDataAddresses.Values.Where(x => x.DataName == tmplt.DataName).ToList();
|
||||
|
||||
if (Oper.Count > 0)
|
||||
return Oper.First().Value;
|
||||
else if (Data.Count > 0)
|
||||
return Data.First().Value;
|
||||
else
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public PLCAddressTemplate UpdateDevice(PLCAddressTemplate tmplt, bool bRefineWrongChar, bool bAsciiReverse = false)
|
||||
{
|
||||
if (tmplt == null)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Cannot Find a Corresponding Data.");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
UpdateValue(tmplt, bRefineWrongChar, bAsciiReverse);
|
||||
|
||||
return tmplt;
|
||||
}
|
||||
|
||||
public bool WriteDevice(PLCAddressTemplate tmplt, string strValue)
|
||||
{
|
||||
List<short> vValues = GetWriteValues(tmplt, strValue);
|
||||
|
||||
return WritePLCData(tmplt, vValues.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,228 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Threading;
|
||||
using SystemX.Net.Platform.Common.Event;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
|
||||
namespace SystemX.PLC.Interface.MxComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Example to represent how to build a device manager.
|
||||
/// A Device Manager constructs a device in run-time environment using a defined dll name in the configuration xml.
|
||||
/// The named dll for a device is bound in run-time environment as well.
|
||||
/// Device manager is an inherited class from a CpDeviceManagerBase and a corresponding interface.
|
||||
/// </summary>
|
||||
public class PLCModMxCMgr
|
||||
{
|
||||
public FunctionEventHandler EventAlwaysUpdate { get; set; }
|
||||
public FunctionEventHandler EventChangeUpdate { get; set; }
|
||||
public PLCDeviceMXC plcMXCompDev;
|
||||
public PLCConnectionInfo PLCInfo;
|
||||
|
||||
public bool IsOpened { get; set; } = false;
|
||||
|
||||
public PLCModMxCMgr(PLCConnectionInfo infoinput, FunctionEventHandler evtHndlAlways, FunctionEventHandler evtHndlChanges)
|
||||
{
|
||||
PLCInfo = infoinput;
|
||||
|
||||
EventAlwaysUpdate = evtHndlAlways;
|
||||
EventChangeUpdate = evtHndlChanges;
|
||||
}
|
||||
|
||||
public bool CloseDevice()
|
||||
{
|
||||
if (plcMXCompDev == null)
|
||||
return true;
|
||||
|
||||
bool bResult = plcMXCompDev.DevClose();
|
||||
|
||||
IsOpened = !bResult;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OpenDevice()
|
||||
{
|
||||
if (IsOpened == true)
|
||||
return IsOpened;
|
||||
|
||||
plcMXCompDev = new PLCDeviceMXC();
|
||||
|
||||
plcMXCompDev.STATION_ID = Convert.ToInt32(PLCInfo.Port);
|
||||
plcMXCompDev.EventAlwaysUpdate = EventAlwaysUpdate;
|
||||
plcMXCompDev.EventChangeUpdate = EventChangeUpdate;
|
||||
|
||||
Task<bool> conPing = new Task<bool>(() => PingToPLC());
|
||||
|
||||
conPing.Start();
|
||||
|
||||
Task<bool> conPLC = conPing.ContinueWith(x => ConnectToPLC());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool PingToPLC()
|
||||
{
|
||||
try
|
||||
{
|
||||
Ping pingtoPLC = new Ping();
|
||||
int nCount = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
PingReply reply = pingtoPLC.Send(PLCInfo.IP);
|
||||
|
||||
if (reply.Status == IPStatus.Success)
|
||||
break;
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Failed. Retry #{nCount} - IP:{PLCInfo.IP}, Station ID:{PLCInfo.Port}", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
nCount++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - PLC Connection error: {PLCInfo.Name}, {PLCInfo.IP}:{PLCInfo.Port} - {PLCInfo.ConnectionType}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" -> Reason: {ex.Message}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ConnectToPLC()
|
||||
{
|
||||
try
|
||||
{
|
||||
IsOpened = plcMXCompDev.DevOpen();
|
||||
|
||||
if (IsOpened == false)
|
||||
plcMXCompDev = null;
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Success. - IP:{PLCInfo.IP}, Port:{PLCInfo.Port} ", ConsoleColor.Green, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return IsOpened;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - PLC Connection error: {PLCInfo.Name}, {PLCInfo.IP}:{PLCInfo.Port} - {PLCInfo.ConnectionType}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" -> Reason: {ex.Message}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void SingleScanStartAsync()
|
||||
{
|
||||
if (!IsOpened) return;
|
||||
|
||||
plcMXCompDev.SingleScanStartAsync();
|
||||
}
|
||||
|
||||
public void NowScanDataArea()
|
||||
{
|
||||
if (!IsOpened) return;
|
||||
|
||||
plcMXCompDev.NowDataAreaScan();
|
||||
}
|
||||
|
||||
public void StartMonitor()
|
||||
{
|
||||
plcMXCompDev.StartMonitor();
|
||||
}
|
||||
|
||||
public void StopMonitor()
|
||||
{
|
||||
plcMXCompDev.StopMonitor();
|
||||
}
|
||||
|
||||
public bool AddDevices(PLCAddressTemplate deviceReg, bool bOperation)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
|
||||
if (bOperation)
|
||||
plcMXCompDev.AddOperationDevices(deviceReg);
|
||||
else
|
||||
plcMXCompDev.AddDataDevices(deviceReg);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - PLC Connection AddDevices: {PLCInfo.Name}, {PLCInfo.IP}:{PLCInfo.Port} - {PLCInfo.ConnectionType}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" -> Reason: {ex.Message}", ConsoleColor.Blue, LogMessage.LogMessageLevel.NONE);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateArea(List<PLCAddressTemplate> vTemplates, bool bRefineWrongChar)
|
||||
{
|
||||
foreach (PLCAddressTemplate keyTemplate in vTemplates)
|
||||
plcMXCompDev.UpdateDevice(keyTemplate, bRefineWrongChar);
|
||||
}
|
||||
|
||||
public void UpdateSelectArea(PLCAddressTemplate keyTemplate, bool bRefineWrongChar)
|
||||
{
|
||||
plcMXCompDev.UpdateDevice(keyTemplate, bRefineWrongChar);
|
||||
}
|
||||
|
||||
public string GetDeviceValue(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return string.Empty;
|
||||
|
||||
return plcMXCompDev.GetDeviceValue(tmplt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Device Get Value Error: {tmplt.PLCAddress}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Detail: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public string ReadDevice(PLCAddressTemplate tmplt, bool bAsciiReverse = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return string.Empty;
|
||||
|
||||
plcMXCompDev.UpdateDevice(tmplt, true, bAsciiReverse);
|
||||
|
||||
return tmplt.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Device Reading Error: {tmplt.PLCAddress}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Detail: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public bool WriteDevice(PLCAddressTemplate tmplt, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
|
||||
return plcMXCompDev.WriteDevice(tmplt, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Device Writting Error: {tmplt.PLCAddress}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Detail: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,222 @@
|
||||
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
|
||||
using ActUtlTypeLib;
|
||||
//using MITSUBISHI.Component;
|
||||
|
||||
namespace SystemX.PLC.Interface.MxComponent
|
||||
{
|
||||
class PLCMxComponent
|
||||
{
|
||||
//DotUtlType _DotUtlType;
|
||||
ActUtlTypeLib.ActUtlType _ActUtlType;
|
||||
|
||||
public bool m_IsPlcConnected;
|
||||
bool m_bLockFin = false;
|
||||
|
||||
// Set the read/write timeouts
|
||||
int m_nReadTimeOut = 500;
|
||||
int m_nWriteTimeOut = 500;
|
||||
|
||||
public PLCMxComponent()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void SetTimeOut(int nWriteTimeOut, int nReadTimeOut)
|
||||
{
|
||||
m_nReadTimeOut = nReadTimeOut;
|
||||
m_nWriteTimeOut = nWriteTimeOut;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ActUtlType = new ActUtlTypeLib.ActUtlType();
|
||||
//_DotUtlType = new DotUtlType();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error in Initialization for plc: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Open(string localStationNum)
|
||||
{
|
||||
int lRet;
|
||||
try
|
||||
{
|
||||
_ActUtlType.ActLogicalStationNumber = Convert.ToInt32(localStationNum);
|
||||
//_DotUtlType.ActLogicalStationNumber = Convert.ToInt32(localStationNum);
|
||||
|
||||
lRet = _ActUtlType.Open();
|
||||
//lRet = _DotUtlType.Open();
|
||||
|
||||
if (lRet == 0)
|
||||
{
|
||||
m_IsPlcConnected = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("Error Open PLC Connection.", ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
m_IsPlcConnected = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("Error during logical station opening: " + ex.Message, ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (m_IsPlcConnected)
|
||||
{
|
||||
//_DotUtlType.Close();
|
||||
_ActUtlType.Close();
|
||||
}
|
||||
|
||||
m_IsPlcConnected = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("Error during plc close: " + ex.Message, ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ReadData(string deviceName, int size, ref short[] data)
|
||||
{
|
||||
bool ret = false;
|
||||
int iRet = 0;
|
||||
|
||||
try
|
||||
{
|
||||
iRet = _ActUtlType.ReadDeviceRandom2(deviceName, size, out data[0]);
|
||||
//iRet = _DotUtlType.ReadDeviceRandom2(ref deviceName, size, ref data);
|
||||
|
||||
if (iRet == 0)
|
||||
ret = true;
|
||||
else
|
||||
ret = false;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
MessageBox.Show(ex.ToString());
|
||||
#endif
|
||||
ret = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool BlockReadData(string deviceName, int size, ref short[] data)
|
||||
{
|
||||
bool ret = false;
|
||||
int iRet = 0;
|
||||
|
||||
try
|
||||
{
|
||||
iRet = _ActUtlType.ReadDeviceBlock2(deviceName, size, out data[0]);
|
||||
//iRet = _DotUtlType.ReadDeviceBlock2(ref deviceName, size, ref data);
|
||||
|
||||
if (iRet == 0)
|
||||
ret = true;
|
||||
else
|
||||
ret = false;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
MessageBox.Show(ex.ToString());
|
||||
#endif
|
||||
ret = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public bool WriteData(string deviceName, int size, short[] data)
|
||||
{
|
||||
bool ret = false;
|
||||
int iRet = 0;
|
||||
|
||||
try
|
||||
{
|
||||
iRet = _ActUtlType.WriteDeviceRandom2(deviceName, size, data[0]);
|
||||
//iRet = _DotUtlType.WriteDeviceRandom2(ref deviceName, size, ref data);
|
||||
|
||||
if (iRet == 0)
|
||||
ret = true;
|
||||
else
|
||||
ret = false;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
MessageBox.Show(ex.ToString());
|
||||
#endif
|
||||
ret = false;
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool BlockWriteData(string deviceName, int size, short[] data)
|
||||
{
|
||||
bool ret = false;
|
||||
int iRet = 0;
|
||||
|
||||
try
|
||||
{
|
||||
short[] blockValue = new short[size];
|
||||
Array.Copy(data, blockValue, data.Length);
|
||||
//Buffer.BlockCopy(data, 0, blockValue, 0, size);
|
||||
|
||||
iRet = _ActUtlType.WriteDeviceBlock2(deviceName, size, blockValue[0]);
|
||||
//iRet = _DotUtlType.WriteDeviceBlock2(ref deviceName, size, blockValue);
|
||||
|
||||
if (iRet == 0)
|
||||
ret = true;
|
||||
else
|
||||
ret = false;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
MessageBox.Show(ex.ToString());
|
||||
#endif
|
||||
ret = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void ReleaseLock()
|
||||
{
|
||||
m_bLockFin = false;
|
||||
}
|
||||
|
||||
public bool GetLockState()
|
||||
{
|
||||
return m_bLockFin;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user