1003 lines
38 KiB
C#
1003 lines
38 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using BaseFunction;
|
|
using HslCommunication.Profinet.Siemens;
|
|
using System.Drawing;
|
|
using PLCModule;
|
|
namespace NSAnalysis
|
|
{
|
|
class PlcObject
|
|
{
|
|
public static PlcAddrSetup m_FormPlcAddr = new PlcAddrSetup(3, ConfigDfn.strConfigFile);
|
|
//=================
|
|
#region 外部操作指令
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="strName"></param>
|
|
/// <param name="PlcType">1:Modebus TCP;2:FINS TCP;3:S7</param>
|
|
/// <returns></returns>
|
|
public static object GetAddr_W(string strName)
|
|
{
|
|
if (ConfigPlc.PlcType == 1 || ConfigPlc.PlcType == 2)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Write.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Write[strName];
|
|
int Result = GetIntFromString(Value[1]);
|
|
if (Result == -1)
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[2]);
|
|
return Result;
|
|
}
|
|
else if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
int Result = GetIntFromString(Value[1]);
|
|
if (Result == -1)
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[2]);
|
|
return Result;
|
|
}
|
|
else
|
|
Console.WriteLine("获取PLC地址失败:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
else if (ConfigPlc.PlcType == 3)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Write.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Write[strName];
|
|
string Result = Value[1];
|
|
if (!string.IsNullOrEmpty(Value[2]))
|
|
{
|
|
Result += "." + Value[2];
|
|
}
|
|
if (string.IsNullOrEmpty(Result))
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Result);
|
|
return Result;
|
|
}
|
|
else if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
string Result = Value[1];
|
|
if (string.IsNullOrEmpty(Result))
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[2]);
|
|
return Result;
|
|
}
|
|
else
|
|
Console.WriteLine("获取PLC地址失败:变量名不存在:" + strName);
|
|
return "";
|
|
}
|
|
else
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public static int GetIndexLength_W(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Write.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Write[strName];
|
|
int Result = GetIntFromString(Value[2]);
|
|
if (Result == -1)
|
|
Console.WriteLine("获取PLC写地址Index或长度出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
return Result;
|
|
}
|
|
else
|
|
Console.WriteLine("获取PLC写地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
|
|
public static int GetAddr_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
int Result = GetIntFromString(Value[1]);
|
|
if (Result == -1)
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[2]);
|
|
return Result;
|
|
}
|
|
else if (m_FormPlcAddr.DictPlcAddr_Write.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Write[strName];
|
|
int Result = GetIntFromString(Value[1]);
|
|
if (Result == -1)
|
|
Console.WriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[2]);
|
|
return Result;
|
|
}
|
|
else
|
|
Console.WriteLine("获取PLC地址失败:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
|
|
public static int GetIndexLength_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
int IndLength = GetIntFromString(Value[2]);
|
|
if (IndLength == -1)
|
|
Console.WriteLine("获取PLC读地址Index或长度出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
}
|
|
else
|
|
Console.WriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
#endregion
|
|
//=============================================================================================================
|
|
static int GetIntFromString(string strInput)
|
|
{
|
|
if (strInput.Length > 0)
|
|
{
|
|
try
|
|
{
|
|
int temp = Convert.ToInt32(strInput, 10);
|
|
return temp;
|
|
}
|
|
catch
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static byte[] GetPlcReadDatas()
|
|
{
|
|
switch (ConfigPlc.PlcType)
|
|
{
|
|
case 1: return HslModbusTcpObj.ReadDataBytes;
|
|
case 3: return HslSiemensS7Obj.ReadDataBytes;
|
|
default: return null;
|
|
}
|
|
}
|
|
|
|
public static int GetData_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.Bool)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
int index = GetIntFromString(Value[2]);
|
|
if (addr >= 0 && index >= 0)
|
|
{
|
|
//bool bResult = PlcMath.GetPlcBitH2Low(GetPlcReadDatas(), addr, index, PlcAddrSetup.StartAddr);
|
|
bool bResult = PlcMath.GetPlcBit(GetPlcReadDatas(), addr, index, PlcAddrSetup.StartAddr);
|
|
if (bResult)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Word)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
int Result = PlcMath.GetPlcData(GetPlcReadDatas(), addr, PlcAddrSetup.StartAddr);
|
|
return Result;
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == -1)
|
|
MyBase.TraceWriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
|
|
public static string GetString_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.String)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
int Length = GetIntFromString(Value[2]);
|
|
if (addr >= 0 && Length >= 0)
|
|
{
|
|
string Result = PlcMath.GetPlcString(GetPlcReadDatas(), addr, PlcAddrSetup.StartAddr, Length);
|
|
Result = Result.Replace('\0', ' ');
|
|
return Result;
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Word)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
int Result = PlcMath.GetPlcData(GetPlcReadDatas(), addr, PlcAddrSetup.StartAddr);
|
|
return Result.ToString();
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == -1)
|
|
MyBase.TraceWriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return "";
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================================================
|
|
#region ModbusTCP协议写入操作
|
|
public static bool PlcWriteInt(object Addr, int data)
|
|
{
|
|
try
|
|
{
|
|
if ((int)Addr >= 0)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.bConnected)
|
|
{
|
|
bool bResult = HslModbusTcpObj.WriteShort(((int)Addr).ToString(), (short)data);
|
|
if (bResult == false)
|
|
{
|
|
MyBase.TraceWriteLine("写入PLC失败:地址为" + ((int)Addr).ToString() + " ;值为:" + data.ToString());
|
|
}
|
|
return bResult;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:地址=" + Addr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(object StartAddr, string Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if ((ushort)StartAddr > 0)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.bConnected)
|
|
{
|
|
return HslModbusTcpObj.WriteString(((ushort)StartAddr).ToString(), Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:StartAddr=" + StartAddr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(object StartAddr, string Value, int length, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if ((ushort)StartAddr > 0)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.bConnected)
|
|
{
|
|
return HslModbusTcpObj.WriteString(((ushort)StartAddr).ToString(), Value, length);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:StartAddr=" + StartAddr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(object StartAddr, float Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
PositiveSequence = ConfigPlc.bPositiveAequence;
|
|
|
|
if ((int)StartAddr > 0)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.bConnected)
|
|
{
|
|
return HslModbusTcpObj.WriteFloat(StartAddr.ToString(), Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:StartAddr=" + StartAddr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(object StartAddr, double Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
PositiveSequence = ConfigPlc.bPositiveAequence;
|
|
|
|
if ((ushort)StartAddr > 0)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.bConnected)
|
|
{
|
|
return HslModbusTcpObj.WriteDouble(StartAddr.ToString(), Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:StartAddr=" + StartAddr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(string StartAddrName, string Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
object Addr = GetAddr_W(StartAddrName);
|
|
return WritePlcValue(Addr, Value, PositiveSequence);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(string StartAddrName, float Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
object Addr = GetAddr_W(StartAddrName);
|
|
return WritePlcValue(Addr, Value, ConfigPlc.bPositiveAequence);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool WritePlcValue(string StartAddrName, double Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
object Addr = GetAddr_W(StartAddrName);
|
|
return WritePlcValue(Addr, Value, PositiveSequence);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
|
|
//=============================================================================================================
|
|
#region S7协议读取操作
|
|
public static int GetS7Data_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.Bool)
|
|
{
|
|
string address = Value[1];
|
|
bool result = false;
|
|
if (HslSiemensS7Obj.ReadBool(address, out result))
|
|
{
|
|
return result ? 1 : 0;
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("读PLC读地址" + address + "失败!");
|
|
return -1;
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Word)
|
|
{
|
|
string address = Value[1];
|
|
short result = 0;
|
|
if (HslSiemensS7Obj.ReadShort(address, out result))
|
|
{
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("读PLC读地址" + address + "失败!");
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
|
|
public static string GetS7String_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.String)
|
|
{
|
|
string addr = Value[1];
|
|
ushort Length = (ushort)GetIntFromString(Value[2]);
|
|
if (!string.IsNullOrEmpty(addr) && Length >= 0)
|
|
{
|
|
string Result = "";
|
|
if (HslSiemensS7Obj.ReadString(addr, Length, out Result))
|
|
{
|
|
Result = Result.Replace('\0', ' ');
|
|
return Result;
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("读PLC读地址" + addr + "失败!");
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Word)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
int Result = PlcMath.GetPlcData(GetPlcReadDatas(), addr, PlcAddrSetup.StartAddr);
|
|
return Result.ToString();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return "";
|
|
}
|
|
|
|
public static byte GetS7ByteData(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.Bool)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
int index = GetIntFromString(Value[2]);
|
|
if (addr >= 0 && index >= 0)
|
|
{
|
|
bool bResult = PlcMath.GetS7BoolData(GetPlcReadDatas(), addr, int.Parse(PlcAddrSetup.S7ReadOffsetAddr), index);
|
|
if (bResult)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Byte)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
byte Result = PlcMath.GetS7ByteData(GetPlcReadDatas(), addr, int.Parse(PlcAddrSetup.S7ReadOffsetAddr));
|
|
return Result;
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == 0)
|
|
MyBase.TraceWriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return 0;
|
|
}
|
|
|
|
public static string GetS7StringData(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.String)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
int Length = GetIntFromString(Value[2]);
|
|
if (addr >= 0 && Length >= 0)
|
|
{
|
|
string Result = PlcMath.GetS7StringData(GetPlcReadDatas(), addr, int.Parse(PlcAddrSetup.S7ReadOffsetAddr), Length);
|
|
Result = Result.Replace('\0', ' ');
|
|
return Result;
|
|
}
|
|
}
|
|
else if (Value[0] == SignalTypeDfn.Word)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
int Result = PlcMath.GetPlcData(GetPlcReadDatas(), addr, PlcAddrSetup.StartAddr);
|
|
return Result.ToString();
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == -1)
|
|
MyBase.TraceWriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return "";
|
|
}
|
|
|
|
public static float GetS7Float_R(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.Single)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
float Result = PlcMath.GetPlcSingleS7(GetPlcReadDatas(), addr, int.Parse(PlcAddrSetup.S7ReadOffsetAddr), false);
|
|
return Result;
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == -1)
|
|
MyBase.TraceWriteLine("获取PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
return -1;
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取PLC读地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
public static double GetS7Double(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Read.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Read[strName];
|
|
if (Value[0] == SignalTypeDfn.Double)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
if (addr >= 0)
|
|
{
|
|
double Result = PlcMath.GetPlcDoubleS7(GetPlcReadDatas(), addr, int.Parse(PlcAddrSetup.S7ReadOffsetAddr), false);
|
|
return Result;
|
|
}
|
|
}
|
|
int m_Addr = GetIntFromString(Value[1]);
|
|
if (m_Addr == -1)
|
|
MyBase.TraceWriteLine("获取Double 类型 PLC读地址出错:名字=" + strName + ", 地址=" + Value[1]);
|
|
return -1;
|
|
}
|
|
else
|
|
MyBase.TraceWriteLine("获取Double 类型 PLC读地址:变量名不存在:" + strName);
|
|
return -1;
|
|
}
|
|
#endregion
|
|
|
|
|
|
//=============================================================================================================
|
|
#region S7协议写入操作
|
|
public static bool S7WriteBool(string Addr, bool data)
|
|
{
|
|
|
|
try
|
|
{
|
|
if (ConfigPlc.PlcType == 3)
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteBool(Addr, data);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:地址=" + Addr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WriteByte(string Addr, byte data)
|
|
{
|
|
try
|
|
{
|
|
if (ConfigPlc.PlcType == 3)
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
bool bWResult= HslSiemensS7Obj.WriteByte(Addr.ToString(), (byte)data);
|
|
if (bWResult == false)
|
|
{
|
|
MyBase.TraceWriteLine("写入PLC失败:地址为:" + Addr + "; 值为:" + data.ToString());
|
|
}
|
|
return bWResult;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:地址=" + Addr + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WriteInt(string StartAddrName, float Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty((string)StartAddrName))
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteShort((string)StartAddrName, (short)Value);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WritePlcValue(string StartAddrName, float Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteFloat(StartAddrName, Value);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WritePlcValue(string StartAddrName, double Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteDouble(StartAddrName, Value);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WritePlcValue(string StartAddrName, string Value, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteString(StartAddrName, Value);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool S7WritePlcValue(string StartAddrName, string Value, int length, bool PositiveSequence = true)
|
|
{
|
|
try
|
|
{
|
|
if (HslSiemensS7Obj.bConnected)
|
|
{
|
|
return HslSiemensS7Obj.WriteString(StartAddrName, Value, length);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("写PLC参数出错:SignalName=" + StartAddrName + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
//=============================================================================================================
|
|
public static bool PlcReadBuffer(int StartAddr, int Length)
|
|
{
|
|
try
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
HslModbusTcpObj.ReadDatas(StartAddr.ToString(), (ushort)Length);
|
|
}
|
|
else if (ConfigPlc.PlcType == 3)
|
|
{
|
|
HslSiemensS7Obj.ReadDatas(StartAddr.ToString(), (ushort)Length);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine("读PLC数据出错:StartAddr=" + PlcAddrSetup.StartAddr + ",长度=" + PlcAddrSetup.Length + "\r\n ex=" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static bool IsPlcConnected()
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
return HslModbusTcpObj.GetConnectStatus();
|
|
}
|
|
else if (ConfigPlc.PlcType == 3)
|
|
{
|
|
return HslSiemensS7Obj.GetConnectStatus();
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool ConnectPlc(bool bShowTip = true)
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
if (HslModbusTcpObj.ConnectPlc(ConfigPlc.strIP, Convert.ToUInt16(ConfigPlc.strPort), ConfigPlc.strMac, bShowTip))
|
|
{
|
|
MyBase.TraceWriteLine("PLC Modbus TCP连接成功。");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("PLC Modbus TCP 连接失败");
|
|
}
|
|
}
|
|
else if (ConfigPlc.PlcType == 3)
|
|
{
|
|
if (HslSiemensS7Obj.ConnectPlc(SiemensPLCS.S1200, ConfigPlc.strIP))
|
|
{
|
|
MyBase.TraceWriteLine("PLC S7连接成功。");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("PLC S7连接失败。");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MyBase.TraceWriteLine("PLC Type is not existed! Type=" + ConfigPlc.PlcType.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static void ClosePlc()
|
|
{
|
|
if (ConfigPlc.PlcType == 1)
|
|
{
|
|
HslModbusTcpObj.ClosePlc();
|
|
}
|
|
else if (ConfigPlc.PlcType == 3)
|
|
{
|
|
HslSiemensS7Obj.ClosePlc();
|
|
}
|
|
}
|
|
|
|
//=============================================================================================================
|
|
public static void InsertWriteQueue(string strAddrName, object Value)
|
|
{
|
|
PlcWriteObj m_DataObj = new PlcWriteObj();
|
|
m_DataObj.WriteAddr = GetAddr_W(strAddrName);
|
|
m_DataObj.WriteValue = Value;
|
|
PlcWriteManager.Instance.InsertQueue(m_DataObj);
|
|
}
|
|
public static void InsertWriteQueue(string strAddrName, object Value, int iLength)
|
|
{
|
|
PlcWriteObj m_DataObj = new PlcWriteObj();
|
|
m_DataObj.WriteAddr = GetAddr_W(strAddrName);
|
|
m_DataObj.WriteValue = Value;
|
|
m_DataObj.WriteLength = iLength;
|
|
PlcWriteManager.Instance.InsertQueue(m_DataObj);
|
|
}
|
|
public static int GetWriteStringLength(string strName)
|
|
{
|
|
if (m_FormPlcAddr.DictPlcAddr_Write.ContainsKey(strName))
|
|
{
|
|
string[] Value = m_FormPlcAddr.DictPlcAddr_Write[strName];
|
|
if (Value[0] == SignalTypeDfn.String)
|
|
{
|
|
int addr = GetIntFromString(Value[1]);
|
|
int Length = GetIntFromString(Value[2]);
|
|
if (addr >= 0 && Length >= 0)
|
|
{
|
|
return Length;
|
|
}
|
|
else
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return -2;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return -3;
|
|
}
|
|
}
|
|
|
|
public static void InsertWriteQueue(object Addr, object Value)
|
|
{
|
|
PlcWriteObj m_DataObj = new PlcWriteObj();
|
|
m_DataObj.WriteAddr = Addr;
|
|
m_DataObj.WriteValue = Value;
|
|
PlcWriteManager.Instance.InsertQueue(m_DataObj);
|
|
}
|
|
|
|
public static void WriteIntoPLC()
|
|
{
|
|
try
|
|
{
|
|
if (PlcWriteManager.Instance.isExitData())
|
|
{
|
|
if (IsPlcConnected())
|
|
{
|
|
PlcWriteObj m_Data = PlcWriteManager.Instance.OutQueue();
|
|
|
|
if (ConfigPlc.PlcType == 1 || ConfigPlc.PlcType == 2)
|
|
{
|
|
if (m_Data != null)
|
|
{
|
|
string strType = m_Data.WriteValue.GetType().Name;
|
|
string strValue = m_Data.WriteValue.ToString();
|
|
switch (strType.ToLower())
|
|
{
|
|
case "byte":
|
|
case "int":
|
|
case "int32":
|
|
case "int16":
|
|
case "uint":
|
|
case "uint16":
|
|
case "uint32":
|
|
PlcWriteInt((int)m_Data.WriteAddr, Int32.Parse(strValue)); break;
|
|
case "float":
|
|
WritePlcValue(Convert.ToUInt16(m_Data.WriteAddr), (float)m_Data.WriteValue); break;
|
|
case "double":
|
|
WritePlcValue(Convert.ToUInt16(m_Data.WriteAddr), (double)m_Data.WriteValue); break;
|
|
case "string":
|
|
int iLen = m_Data.WriteLength;
|
|
WritePlcValue(Convert.ToUInt16(m_Data.WriteAddr), m_Data.WriteValue.ToString(), iLen);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if ((int)m_Data.WriteAddr < 0)
|
|
{
|
|
System.Windows.Forms.MessageBox.Show("PLC地址格式错误,地址小于0");
|
|
}
|
|
}
|
|
}
|
|
if (ConfigPlc.PlcType == 3)
|
|
{
|
|
if (m_Data != null)
|
|
{
|
|
string strType = m_Data.WriteValue.GetType().Name;
|
|
string strValue = m_Data.WriteValue.ToString();
|
|
string strWriteAddr = PLCModule.PlcAddrSetup.GetS7WriteAddr((string)(m_Data.WriteAddr));
|
|
switch (strType.ToLower())
|
|
{
|
|
case "bool":
|
|
case "boolean":
|
|
S7WriteBool(strWriteAddr, bool.Parse(strValue)); break;
|
|
case "byte":
|
|
S7WriteByte(strWriteAddr, byte.Parse(strValue)); break;
|
|
case "int":
|
|
case "int16":
|
|
case "uint":
|
|
case "uint16":
|
|
case "int32":
|
|
case "uint32":
|
|
S7WriteInt(strWriteAddr, Int32.Parse(strValue)); break;
|
|
case "single":
|
|
case "float":
|
|
S7WritePlcValue(strWriteAddr, (float)m_Data.WriteValue); break;
|
|
case "double":
|
|
S7WritePlcValue(strWriteAddr, (double)m_Data.WriteValue); break;
|
|
case "string":
|
|
int iLen = m_Data.WriteLength;
|
|
S7WritePlcValue(strWriteAddr, (string)m_Data.WriteValue, iLen); break;
|
|
default:
|
|
break;
|
|
}
|
|
if (string.IsNullOrEmpty(strWriteAddr))
|
|
{
|
|
System.Windows.Forms.MessageBox.Show("PLC地址格式错误,地址小于0");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
System.Threading.Thread.Sleep(5);
|
|
}
|
|
else
|
|
{
|
|
System.Threading.Thread.Sleep(100);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MyBase.TraceWriteLine(ex.ToString());
|
|
}
|
|
}
|
|
|
|
static System.Threading.Thread m_ThreadWritePLC = null;
|
|
public static bool bStartPlcThread = false;
|
|
public static void InitWritePlcThread()
|
|
{
|
|
bStartPlcThread = true;
|
|
m_ThreadWritePLC = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadWritePLC));
|
|
if (!m_ThreadWritePLC.IsAlive)
|
|
{
|
|
m_ThreadWritePLC.IsBackground = true;
|
|
m_ThreadWritePLC.Start();
|
|
MyBase.TraceWriteLine("启动PLC写线程!");
|
|
}
|
|
}
|
|
|
|
static void ThreadWritePLC()
|
|
{
|
|
while (bStartPlcThread)
|
|
{
|
|
WriteIntoPLC();
|
|
}
|
|
}
|
|
|
|
public static bool bWritePlcThreadStatus
|
|
{
|
|
get
|
|
{
|
|
return m_ThreadWritePLC.IsAlive && bStartPlcThread;
|
|
}
|
|
}
|
|
}
|
|
|
|
class SignalTypeDfn
|
|
{
|
|
public const string Bool = "bool";
|
|
public const string Byte = "byte";
|
|
public const string Word = "word";
|
|
public const string DWord = "dword";
|
|
public const string Single = "single";
|
|
public const string String = "string";
|
|
public const string Double = "double";
|
|
}
|
|
}
|