1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
nodyang@aliyun.com 7ca80af180 Merge branch 'master' into nodyang 2 months ago
nodyang@aliyun.com 3a2364d2e5 logn 2 months ago
nodyang@aliyun.com b40ae28a60 和谐 2 months ago
nodyang@aliyun.com 8a5ea7686c 和谐plc 2 months ago

File diff suppressed because it is too large Load Diff

@ -113,7 +113,7 @@ namespace HighWayIot.Log4net
/// <summary>
/// 记录Error日志
/// </summary>
/// <param name="errorMsg"></param>
/// <param name="info"></param>
/// <param name="ex"></param>
public void Error(string info, Exception ex = null)
{

@ -0,0 +1,71 @@
using System.ComponentModel;
namespace HighWayIot.Plc
{
/// <summary>
/// 数据类型
/// </summary>
public enum DataTypeEnum
{
/// <summary>
/// 未定义
/// </summary>
[Description("未定义")]
None = 0,
/// <summary>
/// Bool
/// </summary>
[Description("Bool")]
Bool = 1,
/// <summary>
/// Byte
/// </summary>
[Description("Byte")]
Byte = 2,
/// <summary>
/// Int16
/// </summary>
[Description("Int16")]
Int16 = 3,
/// <summary>
/// UInt16
/// </summary>
[Description("UInt16")]
UInt16 = 4,
/// <summary>
/// Int32
/// </summary>
[Description("Int32")]
Int32 = 5,
/// <summary>
/// UInt32
/// </summary>
[Description("UInt32")]
UInt32 = 6,
/// <summary>
/// Int64
/// </summary>
[Description("Int64")]
Int64 = 7,
/// <summary>
/// UInt64
/// </summary>
[Description("UInt64")]
UInt64 = 8,
/// <summary>
/// Float
/// </summary>
[Description("Float")]
Float = 9,
/// <summary>
/// Double
/// </summary>
[Description("Double")]
Double = 10,
/// <summary>
/// String
/// </summary>
[Description("String")]
String = 11,
}
}

@ -45,10 +45,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Impl\InovancePlc.cs" />
<Compile Include="Impl\OmronNJPlc.CS" />
<Compile Include="Impl\SiemensPlc.cs" />
<Compile Include="IPlc.cs" />
<Compile Include="DataTypeEnum.cs" />
<Compile Include="PlcConnect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@ -61,5 +59,6 @@
<Name>HighWayIot.Log4net</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -1,117 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Plc
{
public interface IPlc
{
bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
bool Connect(string IP, int port);
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
bool DisConnect();
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
byte[] readValueByAddress(int len, string address);
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
bool writeValueByAddress(int value, string address);
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
bool resetByAddress(string address, int len);
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
string readEaByAddress(string address);
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
int readInteractiveSignal(string address);
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
int readInt32ByAddress(string address);
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeInt32ByAddress(string address, int value);
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
string readStringByAddress(string address, ushort length);
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="SFC"></param>
/// <returns></returns>
bool writeStringByAddress(string address, string value);
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool readBoolByAddress(string address);
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool writeBoolByAddress(string address, bool value);
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeDoubleByAddress(string address, int value);
}
}

@ -1,487 +0,0 @@
using HslCommunication;
using HslCommunication.Profinet.Inovance;
using HighWayIot.Common;
using System;
using HighWayIot.Log4net;
namespace HighWayIot.Plc.Impl
{
/// <summary>
/// 汇川PLC
/// </summary>
public class InovancePlc : IPlc
{
private LogHelper log = LogHelper.Instance;
private StringChange stringChange = StringChange.Instance;
private InovanceTcpNet inovanceTcp = null;
public InovancePlc()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
log.Info("HslCommunication激活失败");
return;
}
log.Info("HslCommunication 11.0.6.0激活成功");
this.inovanceTcp = new InovanceTcpNet();
this.inovanceTcp.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string IP, int port)
{
inovanceTcp?.ConnectClose();
log.PlcLog("汇川PLC连接开始");
inovanceTcp.IpAddress = IP;
inovanceTcp.Port = 502;
inovanceTcp.DataFormat = HslCommunication.Core.DataFormat.CDAB;
try
{
OperateResult connect = inovanceTcp.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
log.PlcLog("汇川PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
log.PlcLog("汇川PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
log.Error("欧姆龙NJ系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return inovanceTcp.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
//log.PlcLog("开始通过PLC地址和长度读取PLC数据");
try
{
OperateResult<byte[]> read = inovanceTcp.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = stringChange.ConvertFloatToINt(read.Content);
log.PlcLog(String.Format("通过地址和长度读取PLC数据成功{0}",stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
log.PlcLog("通过地址和长度读取PLC数据失败");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
log.Error("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据,模切汇川PLC复位逻辑
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(int value, string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}", address, value));
OperateResult operateResult = new OperateResult();
try
{
int s = 0;
string[] strArry = address.Split('.');
//先读取整个块的内容
var info = inovanceTcp.ReadInt16(strArry[0]);
if (info.Content == 0)
{
int length = stringChange.ParseToInt(strArry[1]) + 1;
string[] array = new string[length];
for(int i=0;i< length;i++)
{
if(i == stringChange.ParseToInt(strArry[1]))
{
array[i] = value.ToString();
}
else
{
array[i] = "0";
}
}
//反转
Array.Reverse(array);
byte[] buffer = new byte[array.Length];
string result = "";
for(int i = 0; i < array.Length; i++)
{
result += (byte)Convert.ToInt32(array[i], 16);
}
s = Convert.ToInt32(result.Trim(), 2);
operateResult = inovanceTcp.Write(strArry[0], (ushort)s);
}
else
{
var inf2 = Convert.ToString(info.Content, 2);
string[] infoArray = new string[inf2.Length];
for (int i = 0; i < inf2.Length; i++)
{
infoArray[i] = inf2.Substring(i, 1);
}
Array.Reverse(infoArray);
infoArray[stringChange.ParseToInt(strArry[1])] = value.ToString();
string result = "";
foreach (var item in infoArray)
{
result = result + item;
}
s = Convert.ToInt32(result.Trim(), 10);
operateResult = inovanceTcp.Write(strArry[0], s);
}
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error("通过PLC地址写入int类型数据", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool resetByAddress(string address, int len)
{
// log.PlcLog(String.Format("开发通过PLC地址{0}清零数据", address));
try
{
byte[] write = new byte[len * 2];
for (int i = 0; i < len * 2; i++)
{
write[i] = 0;
}
OperateResult operateResult = inovanceTcp.Write(address, write);
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}清零数据成功", address));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}清零数据失败!!!", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}清零数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readEaByAddress(string address)
{
//log.PlcLog(String.Format("通过PLC地址{0}读取EA值", address));
try
{
OperateResult<Byte[]> read = inovanceTcp.Read(address, (ushort)(8));
if (read.IsSuccess && read.Content != null)
{
string result = Convert.ToString(read.Content);
log.PlcLog(String.Format("通过PLC地址{0}读取EA值成功{1}", address, result));
return result;
}
else
{
log.PlcLog(String.Format("通过PLC地址{0}读取EA值失败", address));
this.IsConnected = false;
return "";
}
}
catch (Exception ex)
{
log.Error("通过PLC地址读取EA值异常", ex);
this.IsConnected = false;
return "";
}
}
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInteractiveSignal(string address)
{
// log.PlcLog(String.Format("开始通过PLC地址{0}读取交互信号", address));
try
{
OperateResult<short> read = inovanceTcp.ReadInt16(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取交互信号异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt32ByAddress(string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取int32类型数据", address));
try
{
OperateResult<short> read = inovanceTcp.ReadInt16(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeInt32ByAddress(string address, int value)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
try
{
OperateResult write = inovanceTcp.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
//log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}", address, value));
try
{
OperateResult operateResult = inovanceTcp.Write(address, value);
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = inovanceTcp.ReadString(address, length);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取string类型数据失败", address));
return "";
}
catch (Exception ex)
{
log.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = inovanceTcp.ReadBool(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
return false;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取bool类型数据异常", ex);
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeBoolByAddress(string address, bool value)
{
// log.PlcLog(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = inovanceTcp.Write(address, value);
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult write = inovanceTcp.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
}
}

@ -1,444 +0,0 @@
using HighWayIot.Common;
using HighWayIot.Log4net;
using HslCommunication;
using HslCommunication.Profinet.Omron;
using System;
namespace HighWayIot.Plc.Impl
{
/// <summary>
/// 欧姆龙NJ系列PLC
/// </summary>
public class OmronNJPlc : IPlc
{
private LogHelper log = LogHelper.Instance;
private StringChange stringChange = StringChange.Instance;
private OmronFinsNet omronFinsNet = null;
public OmronNJPlc()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
log.Info("HslCommunication 11.0.6.0激活失败");
return;
}
log.Info("HslCommunication 11.0.6.0激活成功");
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
log.PlcLog("欧姆龙NJ系列PLC连接开始");
omronFinsNet.IpAddress = IP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
try
{
OperateResult connect = omronFinsNet.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
log.PlcLog("欧姆龙NJ系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
log.PlcLog("欧姆龙NJ系列PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
log.Error("欧姆龙NJ系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return omronFinsNet.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
//log.PlcLog("开始通过PLC地址和长度读取PLC数据");
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = stringChange.ConvertFloatToINt(read.Content);
log.PlcLog(String.Format("通过地址和长度读取PLC数据成功{0}", stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
log.PlcLog("通过地址和长度读取PLC数据失败");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
log.Error("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(int value, string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error("通过PLC地址写入int类型数据", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool resetByAddress(string address, int len)
{
//log.PlcLog(String.Format("开发通过PLC地址{0}清零数据", address));
try
{
byte[] write = new byte[len * 2];
for (int i = 0; i < len * 2; i++)
{
write[i] = 0;
}
OperateResult operateResult = omronFinsNet.Write(address, write);
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}清零数据成功", address));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}清零数据失败!!!", address));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}清零数据异常",address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readEaByAddress(string address)
{
//log.PlcLog(String.Format("通过PLC地址{0}读取EA值", address));
try
{
OperateResult<Byte[]> read = omronFinsNet.Read(address, (ushort)(8));
if (read.IsSuccess && read.Content != null)
{
string result = Convert.ToString(read.Content);
log.PlcLog(String.Format("通过PLC地址{0}读取EA值成功{1}", address,result));
return result;
}
else
{
log.PlcLog(String.Format("通过PLC地址{0}读取EA值失败", address));
this.IsConnected = false;
return "";
}
}
catch (Exception ex)
{
log.Error("通过PLC地址读取EA值异常", ex);
this.IsConnected = false;
return "";
}
}
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInteractiveSignal(string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取交互信号", address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取交互信号异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt32ByAddress(string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeInt32ByAddress(string address, int value)
{
log.PlcLog(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address,value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address,value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address,value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address),ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
//log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (operateResult.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address),ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address,ushort length)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
log.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (read.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
log.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
//log.PlcLog(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult write = omronFinsNet.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
log.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
log.PlcLog(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
log.Error(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
}
}

@ -1,405 +0,0 @@
using HighWayIot.Common;
using HighWayIot.Log4net;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HighWayIot.Plc.Impl
{
public class SiemensPlc
{
private LogHelper logHelper = LogHelper.Instance;
private StringChange stringChange = StringChange.Instance;
private const SiemensPLCS type = SiemensPLCS.S200Smart;
private SiemensS7Net s7 = new SiemensS7Net(type);
public SiemensPlc()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("30c15272-3960-4853-9fab-3087392ee5cd"))
{
logHelper.Info("HslCommunication激活失败");
return;
}
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
logHelper.PlcLog("西门子S7系列PLC连接开始");
s7.IpAddress = IP;
s7.Port = 102;
try
{
OperateResult connect = s7.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
logHelper.PlcLog("西门子S7系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
logHelper.PlcLog("西门子S7系列PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
logHelper.Error("西门子S7系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
//logHelper.PlcLog("开始通过PLC地址和长度读取PLC数据");
try
{
OperateResult<byte[]> read = s7.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = stringChange.ConvertFloatToINt(read.Content);
logHelper.PlcLog(String.Format("通过地址和长度读取PLC数据成功{0}", stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
logHelper.PlcLog("通过地址和长度读取PLC数据失败");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
logHelper.Error("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="value"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(int value, string address)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
logHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
logHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址写入int类型数据", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址清零数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool resetByAddress(string address, int len)
{
//logHelper.PlcLog(String.Format("开发通过PLC地址{0}清零数据", address));
try
{
byte[] write = new byte[len * 2];
for (int i = 0; i < len * 2; i++)
{
write[i] = 0;
}
OperateResult operateResult = s7.Write(address, write);
if (operateResult.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}清零数据成功", address));
return true;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}清零数据失败!!!", address));
return false;
}
catch (Exception ex)
{
logHelper.Error(String.Format("通过PLC地址{0}清零数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取EA值
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readEaByAddress(string address)
{
//logHelper.PlcLog(String.Format("通过PLC地址{0}读取EA值", address));
try
{
OperateResult<Byte[]> read = s7.Read(address, (ushort)(8));
if (read.IsSuccess && read.Content != null)
{
string result = Convert.ToString(read.Content);
logHelper.PlcLog(String.Format("通过PLC地址{0}读取EA值成功{1}", address, result));
return result;
}
else
{
logHelper.PlcLog(String.Format("通过PLC地址{0}读取EA值失败", address));
this.IsConnected = false;
return "";
}
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址读取EA值异常", ex);
this.IsConnected = false;
return "";
}
}
/// <summary>
/// 通过PLC地址读取交互信号
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInteractiveSignal(string address)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}读取交互信号", address));
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
return read.Content;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址读取交互信号异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址读取int32类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt32ByAddress(string address)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据成功{1}", address, read.Content));
return read.Content;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}读取int32类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入int32类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeInt32ByAddress(string address, int value)
{
logHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value));
return true;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
logHelper.Error(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
//logHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, value);
if (operateResult.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
logHelper.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = s7.ReadString(address, length);
if (read.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = s7.ReadBool(address);
if (read.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
logHelper.Error("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
//logHelper.PlcLog(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
logHelper.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
logHelper.PlcLog(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
logHelper.Error(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
}
}

@ -0,0 +1,125 @@
using System;
using HighWayIot.Log4net;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
namespace HighWayIot.Plc
{ public class PlcConnect
{
private static LogHelper logNet = LogHelper.Instance;
/// <summary>
/// 静态懒加载
/// </summary>
private static readonly MelsecMcNet Instance = new PlcConnect().CreateAb();
private PlcConnect()
{
}
/// <summary>
/// 初始化三菱的服务器
/// </summary>
/// <returns></returns>
private MelsecMcNet CreateAb()
{
string Ip = "";
MelsecMcNet plc = new MelsecMcNet();
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(Ip, 0)
{
ConnectTimeOut = 1000, // 连接超时时间,单位毫秒
SleepTime = 0,
SocketKeepAliveTime = -1,
IsPersistentConnection = true,
};
return plc;
}
/// <summary>
/// 读取bool 页面
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static bool ReadBool(string address)
{
var result = Instance.ReadBool(address);
return result.IsSuccess && result.Content;
}
/// <summary>
/// plc 是不是保持链接
/// </summary>
public static bool IsConnect
{
get
{
var result = Instance.ReadPlcType();
return result.IsSuccess;
}
}
/// <summary>
/// 写入数据
/// </summary>
/// <param name="address">地址</param>
/// <param name="value">值</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public static OperateResult Write(string address, object value, DataTypeEnum type)
{
var result = new OperateResult() { IsSuccess = false };
switch (type)
{
case DataTypeEnum.Bool:
result= Instance.Write(address, Convert.ToBoolean(value));
break;
case DataTypeEnum.Byte:
result = Instance.Write(address, Convert.ToByte(value));
break;
case DataTypeEnum.Int16:
result = Instance.Write(address, Convert.ToInt16(value));
break;
case DataTypeEnum.UInt16:
result = Instance.Write(address, Convert.ToUInt16(value));
break;
case DataTypeEnum.Int32:
result = Instance.Write(address, Convert.ToInt32(value));
break;
case DataTypeEnum.UInt32:
result = Instance.Write(address, Convert.ToUInt32(value));
break;
case DataTypeEnum.Int64:
result = Instance.Write(address, Convert.ToInt64(value));
break;
case DataTypeEnum.UInt64:
result = Instance.Write(address, Convert.ToUInt64(value));
break;
case DataTypeEnum.Float:
result = Instance.Write(address, Convert.ToSingle(value));
break;
case DataTypeEnum.Double:
result = Instance.Write(address, Convert.ToDouble(value));
break;
}
logNet.Info($"write 地址[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]");
return result;
}
}
}
Loading…
Cancel
Save