Files
2024-06-26 10:30:00 +09:00

223 lines
5.6 KiB
C#

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;
}
}
}