[성현모] CPXV2 Init
This commit is contained in:
@ -0,0 +1,497 @@
|
||||
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.McProtocol
|
||||
{
|
||||
public class PLCCommMcPMgr : PLCCommManager
|
||||
{
|
||||
private int TimeOut;
|
||||
public Dictionary<ePLCRWType, PLCModMxCMgr> PLCManagers { get; set; } = new Dictionary<ePLCRWType, PLCModMxCMgr>();
|
||||
|
||||
public PLCCommMcPMgr(string strPLCConnInfoXmlPath)
|
||||
: base(strPLCConnInfoXmlPath)
|
||||
{
|
||||
AddrMgr = new PLCAddressManager(strPLCConnInfoXmlPath);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
conmgr.EventPLC += ReceivePLCEvent;
|
||||
|
||||
SetAddress(conmgr, AddrMgr.OperationDictionary);
|
||||
SetAddress(conmgr, AddrMgr.DataDictionary);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool StartPLCOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.Sleep(300);
|
||||
|
||||
PLCManagers[ePLCRWType.Read].SingleScanStartAsync(); // Do not use await
|
||||
|
||||
UpdateAll();
|
||||
|
||||
OperationStarted = true;
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite("PLC Operation 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()
|
||||
{
|
||||
|
||||
}
|
||||
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 override void ReceivePLCEvent(string address, int value)
|
||||
{
|
||||
PLCAddressTemplate tmpltSearch = SearchMySubscription(address);
|
||||
|
||||
if (tmpltSearch == null)
|
||||
return;
|
||||
|
||||
UpdateDevice(tmpltSearch, address, value);
|
||||
|
||||
EventChangeUpdate.DoReceive(this, address);
|
||||
}
|
||||
|
||||
public PLCAddressTemplate SearchMySubscription(string strAddress)
|
||||
{
|
||||
PLCAddressTemplate tmpltOp = AddrMgr.FindPLCAddressElementByAssociatedKeyAddress(strAddress, AddrMgr.OperationDictionary);
|
||||
PLCAddressTemplate tmpltData = AddrMgr.FindPLCAddressElementByAssociatedKeyAddress(strAddress, AddrMgr.DataDictionary);
|
||||
|
||||
if (tmpltOp != null)
|
||||
return tmpltOp;
|
||||
else
|
||||
return tmpltData;
|
||||
}
|
||||
|
||||
void SetAddress(PLCModMxCMgr module, Dictionary<string, PLCAddressTemplate> addressInfo)
|
||||
{
|
||||
foreach (PLCAddressTemplate addrinfo in addressInfo.Values)
|
||||
{
|
||||
int nLength = Convert.ToInt32(addrinfo.Length);
|
||||
int nStartAddr = Convert.ToInt32(addrinfo.Address);
|
||||
|
||||
for (int i = 0; i < nLength; i++)
|
||||
{
|
||||
int nAddr = nStartAddr + i;
|
||||
module.AddDevices(addrinfo.Area + nAddr.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PLCAddressTemplate UpdateDevice(PLCAddressTemplate tmplt, string strDevice, int nValue)
|
||||
{
|
||||
if (tmplt == null)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Cannot Find a Corresponding Data. Device: {strDevice}");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
tmplt.UpdateValue(strDevice, nValue);
|
||||
|
||||
return tmplt;
|
||||
}
|
||||
|
||||
public PLCAddressTemplate UpdateDevice(string strDevice)
|
||||
{
|
||||
PLCAddressTemplate tmplt = AddrMgr.FindPLCAddressElement(strDevice);
|
||||
|
||||
if (tmplt == null)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Cannot Find a Corresponding Data. Device: {strDevice}");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
UpdateValue(tmplt);
|
||||
|
||||
return tmplt;
|
||||
}
|
||||
|
||||
void UpdateValue(PLCAddressTemplate tmplt)
|
||||
{
|
||||
List<int> vValues = GetReadValues(tmplt);
|
||||
eDataType eType = (eDataType)Enum.Parse(typeof(eDataType), tmplt.DataType);
|
||||
string strData = string.Empty;
|
||||
|
||||
tmplt.SetAssociatedValues(vValues);
|
||||
}
|
||||
|
||||
public override string ReadDevice(string strDevice, bool bRefineWrongChar = true)
|
||||
{
|
||||
PLCAddressTemplate tmplt = UpdateDevice(strDevice);
|
||||
|
||||
return (tmplt != null) ? tmplt.Value : string.Empty;
|
||||
}
|
||||
|
||||
public override string ReadDevice(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
tmplt = UpdateDevice(tmplt.AddressKey);
|
||||
|
||||
return (tmplt != null) ? tmplt.Value : string.Empty;
|
||||
}
|
||||
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 void UpdateAll(bool bRefineWrongChar = false)
|
||||
{
|
||||
UpdateArea(AddrMgr.OperationDictionary.Keys.ToList());
|
||||
UpdateArea(AddrMgr.DataDictionary.Keys.ToList());
|
||||
}
|
||||
public virtual void SelectUpdate(PLCAddressTemplate tmplt, bool bRefineWrongChar)
|
||||
{
|
||||
}
|
||||
void UpdateArea(List<string> strKeys)
|
||||
{
|
||||
foreach (string strKey in strKeys)
|
||||
UpdateDevice(strKey);
|
||||
}
|
||||
|
||||
public override bool WriteDevice(PLCAddressTemplate tmplt, string strValue, bool bVerification = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
tmplt.SetStringValue(strValue);
|
||||
|
||||
WriteDevice(tmplt);
|
||||
|
||||
if (bVerification)
|
||||
{
|
||||
string strReadValue = ReadDevice(tmplt.PLCAddress);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void WriteDevice(string strDevice)
|
||||
{
|
||||
int nCount = 0;
|
||||
if (!OperationStarted)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (OperationStarted || nCount > 100)
|
||||
break;
|
||||
|
||||
nCount++;
|
||||
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
PLCAddressTemplate tmplt = AddrMgr.FindPLCAddressElement(strDevice);
|
||||
|
||||
WriteDevice(tmplt);
|
||||
}
|
||||
|
||||
public bool WriteDevice(PLCAddressTemplate tmplt)
|
||||
{
|
||||
try
|
||||
{
|
||||
int nCount = 0;
|
||||
if (!OperationStarted)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (OperationStarted || nCount > 100)
|
||||
break;
|
||||
|
||||
nCount++;
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
int nLength = Convert.ToInt32(tmplt.Length);
|
||||
|
||||
if (nLength > 1)
|
||||
WriteBlockDevice(tmplt, nLength, tmplt.RawValue);
|
||||
else
|
||||
WriteDeviceSequence(tmplt, nLength, tmplt.RawValue);
|
||||
|
||||
//UpdateValue(tmplt, 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 WriteBlockDevice(PLCAddressTemplate tmplt, int nLength, List<int> vnValues)
|
||||
{
|
||||
try
|
||||
{
|
||||
string strAddr = tmplt.Area + (Convert.ToInt32(tmplt.Address)).ToString();
|
||||
bool result = PLCManagers[ePLCRWType.Write].WriteBlockDevice(strAddr, vnValues.Count, vnValues.ToArray());
|
||||
|
||||
return result;
|
||||
|
||||
/*
|
||||
if (result)
|
||||
{
|
||||
int[] anResult = PLCManagers[ePLCRWType.Read].ReadBlockDevice(strAddr, nLength);
|
||||
|
||||
for(int nIdx = 0; nIdx < vnValues.Count; nIdx++)
|
||||
if (anResult[nIdx] != vnValues[nIdx])
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"During: WriteBlockDevice, The Address: {strAddr} + {nIdx} has not been written #Intent:{vnValues[nIdx]} - #Actual:{anResult[nIdx]} by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"During: WriteBlockDevice, 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 WriteDeviceSequence(PLCAddressTemplate tmplt, int nLength, List<int> vnValues)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < nLength; i++)
|
||||
{
|
||||
//LogMessage.MessageOutput.ConsoleWrite($"The Address: {tmplt.AddressKey}. ", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
string strAddr = tmplt.Area + (Convert.ToInt32(tmplt.Address) + i).ToString();
|
||||
|
||||
bool result = PLCManagers[ePLCRWType.Write].WriteDevice(strAddr, vnValues[i]);
|
||||
|
||||
return result;
|
||||
|
||||
/*
|
||||
if (i >= vnValues.Count)
|
||||
PLCManagers[ePLCRWType.Write].WriteDevice(strAddr, 0);
|
||||
else
|
||||
{
|
||||
bool result = PLCManagers[ePLCRWType.Write].WriteDevice(strAddr, vnValues[i]);
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
if (result)
|
||||
{
|
||||
int nResult = PLCManagers[ePLCRWType.Read].ReadDevice(strAddr);
|
||||
|
||||
if (nResult != vnValues[i])
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"During: WriteDeviceSequence, The Address: {strAddr} has not been written #Intent:{vnValues[i]} - #Actual:{nResult} by unidentified errors. ", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}*/
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"During: WriteDeviceSequence, 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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
List<int> GetWriteValues(PLCAddressTemplate tmplt)
|
||||
{
|
||||
List<int> vnValues = new List<int>();
|
||||
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(tmplt.Value) select Convert.ToInt32(value)).ToList();
|
||||
else if (eType == eDataType.DECIMAL)
|
||||
{
|
||||
int nInputValue = string.IsNullOrWhiteSpace(tmplt.Value) ? 0 : Convert.ToInt32(tmplt.Value);
|
||||
vnValues.Add(nInputValue);
|
||||
}
|
||||
|
||||
return vnValues;
|
||||
}
|
||||
|
||||
List<int> GetReadValues(PLCAddressTemplate tmplt)
|
||||
{
|
||||
List<int> vnValues = new List<int>();
|
||||
int nLength = Convert.ToInt32(tmplt.Length);
|
||||
|
||||
/*
|
||||
for (int i = 0; i < nLength; i++)
|
||||
{
|
||||
int nAddress = Convert.ToInt32(tmplt.Address) + i;
|
||||
int nValue = PLCManagers[ePLCRWType.Read].ReadDevice(tmplt.Area + nAddress.ToString());
|
||||
|
||||
vnValues.Add(nValue);
|
||||
}
|
||||
*/
|
||||
|
||||
if (nLength > 1)
|
||||
{
|
||||
int[] anResult = PLCManagers[ePLCRWType.Read].ReadBlockDevice(tmplt.PLCAddress, nLength);
|
||||
|
||||
if (anResult != null)
|
||||
vnValues = anResult.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < nLength; i++)
|
||||
{
|
||||
int nAddress = Convert.ToInt32(tmplt.Address) + i;
|
||||
int nValue = PLCManagers[ePLCRWType.Read].ReadDevice(tmplt.Area + nAddress.ToString());
|
||||
|
||||
vnValues.Add(nValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return vnValues;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,157 @@
|
||||
using Dsu.PLC;
|
||||
using Dsu.PLC.Common;
|
||||
using Dsu.PLC.Melsec;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using SystemX.Net.Platform.Common.Event;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
|
||||
namespace SystemX.PLC.Interface.McProtocol
|
||||
{
|
||||
public class PLCDeviceMXC
|
||||
{
|
||||
public FunctionEventHandler FuncEvtHndl { get; set; }
|
||||
public string CPU_TYPE_STR { set; get; } //Example: "Q03UDECPU";
|
||||
public string PLC_IP_ADDR { set; get; }
|
||||
public int PLC_PORT_NUMBER { set; get; }
|
||||
public string CONNECTION_TYPE_STR { set; get; }
|
||||
public int TIMEOUT { set; get; }
|
||||
public bool READPORT { set; get; }
|
||||
public string DeviceID { set; get; }
|
||||
|
||||
private MxConnection _conn; // Read port UDP Write port TCP
|
||||
|
||||
public bool DevClose()
|
||||
{
|
||||
return _conn.Disconnect();
|
||||
}
|
||||
|
||||
public bool DevOpen()
|
||||
{
|
||||
try
|
||||
{
|
||||
TransportProtocol ConType = CONNECTION_TYPE_STR.ToUpper() == "TCP" ? TransportProtocol.Tcp : TransportProtocol.Udp;
|
||||
_conn = new MxConnection(new MxConnectionParameters(PLC_IP_ADDR, Convert.ToUInt16(PLC_PORT_NUMBER), ConType));
|
||||
return _conn.Connect();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite(ex.Message, ConsoleColor.Red, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddDevices(string deviceNames)
|
||||
{
|
||||
_conn.CreateTag(deviceNames);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task SingleScanStartAsync()
|
||||
{
|
||||
if (_conn.Tags.Count == 0)
|
||||
return;
|
||||
|
||||
int readCount = 0;
|
||||
|
||||
_conn.PerRequestDelay = TIMEOUT;
|
||||
|
||||
_conn.Subject.OfType<TagValueChangedEvent>().Subscribe(evt =>
|
||||
{
|
||||
readCount++;
|
||||
EventTag((MxTag)evt.Tag);
|
||||
});
|
||||
|
||||
_conn.StartDataExchangeLoopAsync(); // Do not use await
|
||||
|
||||
// _conn.Disconnect();
|
||||
|
||||
Stopwatch xSW = new Stopwatch();
|
||||
xSW.Start();
|
||||
bool bSuccess = true;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (_conn.Tags.Count <= readCount)
|
||||
break;
|
||||
else
|
||||
System.Threading.Thread.Sleep(50);
|
||||
|
||||
if (xSW.ElapsedMilliseconds > 5000)
|
||||
{
|
||||
bSuccess = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bSuccess)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite("Trouble in Single Scan Start Async", ConsoleColor.White, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
//await SingleScanStartAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private void EventTag(MxTag tag)
|
||||
{
|
||||
if (tag.IsBitDevice)
|
||||
FuncEvtHndl.DoReceive(this, tag.Name + ";" + (Convert.ToBoolean(tag.Value) ? 1 : 0).ToString());
|
||||
else
|
||||
FuncEvtHndl.DoReceive(this, tag.Name + ";" + tag.Value.ToString());
|
||||
}
|
||||
|
||||
public bool WriteBitDevice(string strDeviceWrite, int[] value)
|
||||
{
|
||||
//Console.WriteLine(string.Format("{0} : {1}", strDeviceWrite, value[0]));
|
||||
return _conn.McProtocol.SetBitDevice(strDeviceWrite, 1, value) == 0;
|
||||
}
|
||||
|
||||
public bool WriteDevice(string strDeviceWrite, int value)
|
||||
{
|
||||
//Console.WriteLine(string.Format("{0} : {1}", strDeviceWrite, value));
|
||||
return _conn.McProtocol.SetDevice(strDeviceWrite, Convert.ToInt32(value)) == 0;
|
||||
}
|
||||
|
||||
public bool WriteBlockDevice(string strDeviceWrite, int nLength, int[] nValues)
|
||||
{
|
||||
//Console.WriteLine(string.Format("{0} : {1}", strDeviceWrite, value));
|
||||
return _conn.McProtocol.WriteDeviceBlock(strDeviceWrite, nLength, nValues) == 0;
|
||||
}
|
||||
|
||||
public int ReadDevice(string strDeviceRead)
|
||||
{
|
||||
int value;
|
||||
_conn.McProtocol.GetDevice(strDeviceRead, out value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public int[] ReadBlockDevice(string strDeviceRead, int nLength)
|
||||
{
|
||||
try
|
||||
{
|
||||
int[] anvalue = new int[nLength];
|
||||
|
||||
int nResult = _conn.McProtocol.ReadDeviceBlock(strDeviceRead, nLength, anvalue);
|
||||
|
||||
return anvalue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"Trouble in McComponent during ReadBlockDevice:{strDeviceRead} - {nLength}", ConsoleColor.White, LogMessage.LogMessageLevel.FATAL);
|
||||
LogMessage.MessageOutput.ConsoleWrite($"\t- Error Message {ex.Message}", ConsoleColor.White, LogMessage.LogMessageLevel.FATAL);
|
||||
|
||||
return new int[1];
|
||||
}
|
||||
}
|
||||
public string GetModuleName()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().ManifestModule.Name.Replace(".dll", string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,199 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Threading;
|
||||
using SystemX.Net.Platform.Common.Event;
|
||||
using SystemX.Net.Platform.Common.Util;
|
||||
using static SystemX.PLC.Interface.PLCCommDefinition;
|
||||
|
||||
namespace SystemX.PLC.Interface.McProtocol
|
||||
{
|
||||
public class PLCModMxCMgr
|
||||
{
|
||||
public event EventPLCHandler EventPLC;
|
||||
public FunctionEventHandler EventAlwaysUpdate { get; set; }
|
||||
public FunctionEventHandler EventChangeUpdate { get; set; }
|
||||
public PLCDeviceMXC plcDev;
|
||||
|
||||
PLCConnectionInfo PLCInfo;
|
||||
|
||||
public string CPU_TYPE_STR { set; get; } //Example: "Q03UDECPU";
|
||||
public string PLC_IP_ADDR { set; get; }
|
||||
public int PLC_PORT_NUMBER { set; get; }
|
||||
public string CONNECTION_TYPE_STR { set; get; }
|
||||
public int TIMEOUT { set; get; }
|
||||
public bool IsOpened { get; set; } = false;
|
||||
|
||||
public PLCModMxCMgr(PLCConnectionInfo infoinput)
|
||||
{
|
||||
PLCInfo = infoinput;
|
||||
}
|
||||
|
||||
public bool CloseDevice()
|
||||
{
|
||||
bool bResult = plcDev.DevClose();
|
||||
|
||||
IsOpened = !bResult;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OpenDevice()
|
||||
{
|
||||
if (IsOpened == true)
|
||||
return IsOpened;
|
||||
|
||||
plcDev = new PLCDeviceMXC();
|
||||
EventChangeUpdate = new FunctionEventHandler();
|
||||
|
||||
plcDev.FuncEvtHndl = EventChangeUpdate;
|
||||
plcDev.DeviceID = PLCInfo.Name;
|
||||
plcDev.CPU_TYPE_STR = PLCInfo.CPUType;
|
||||
plcDev.PLC_IP_ADDR = PLCInfo.IP;
|
||||
plcDev.PLC_PORT_NUMBER = Convert.ToInt32(PLCInfo.Port);
|
||||
plcDev.CONNECTION_TYPE_STR = PLCInfo.ConnectionType != "UDP" ? "TCP" : "UDP";
|
||||
plcDev.READPORT = (PLCInfo.RWType == PLCCommDefinition.ePLCRWType.Read.ToString()) ? true : false;
|
||||
plcDev.TIMEOUT = Convert.ToInt32(PLCInfo.Timeout);
|
||||
|
||||
EventChangeUpdate.OnReceive += FuncEvtHndl_OnPLCReceive;
|
||||
|
||||
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(plcDev.PLC_IP_ADDR);
|
||||
|
||||
if (reply.Status == IPStatus.Success)
|
||||
break;
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Failed. Retry #{nCount} - IP:{plcDev.PLC_IP_ADDR}, Port:{plcDev.PLC_PORT_NUMBER}", 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 = plcDev.DevOpen();
|
||||
|
||||
if (IsOpened == false)
|
||||
plcDev = null;
|
||||
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Connection Success. - IP:{plcDev.PLC_IP_ADDR}, Port:{plcDev.PLC_PORT_NUMBER} ", 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;
|
||||
}
|
||||
}
|
||||
|
||||
private void FuncEvtHndl_OnPLCReceive(object sender, object objdata)
|
||||
{
|
||||
string data = objdata as string;
|
||||
|
||||
if (data == null)
|
||||
return;
|
||||
|
||||
if (data.Contains(";"))
|
||||
{
|
||||
string address = data.Split(';')[0];
|
||||
int value = Convert.ToInt32(data.Split(';')[1]);
|
||||
|
||||
EventPLC?.Invoke(address, value);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SingleScanStartAsync()
|
||||
{
|
||||
if (!IsOpened) return;
|
||||
|
||||
await plcDev.SingleScanStartAsync();
|
||||
}
|
||||
|
||||
public bool AddDevices(string deviceNames)
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
|
||||
return plcDev.AddDevices(deviceNames);
|
||||
}
|
||||
|
||||
public int ReadDevice(string deviceName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return -1;
|
||||
return plcDev.ReadDevice(deviceName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Device Reading Error in PLCModMxCMgr: {deviceName}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Detail: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public int[] ReadBlockDevice(string deviceName, int nLength)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!IsOpened) return null;
|
||||
return plcDev.ReadBlockDevice(deviceName, nLength);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogMessage.MessageOutput.ConsoleWrite($"PLC Device Reading Error in PLCModMxCMgr: {deviceName}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
LogMessage.MessageOutput.ConsoleWrite($" - Detail: {ex.Message}", ConsoleColor.Red, LogMessage.LogMessageLevel.NONE);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public bool WriteDevice(string deviceName, int value)
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
return plcDev.WriteDevice(deviceName, value);
|
||||
}
|
||||
|
||||
public bool WriteBlockDevice(string deviceName, int nLength, int[] aValues)
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
return plcDev.WriteBlockDevice(deviceName, nLength, aValues);
|
||||
}
|
||||
|
||||
public bool WriteBitDevice(string deviceName, int[] value)
|
||||
{
|
||||
if (!IsOpened) return false;
|
||||
return plcDev.WriteBitDevice(deviceName, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user