You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
15 KiB
C#

1 year ago
using HwCommunication;
using HwCommunication.Profinet.Melsec;
using HwCommunication.Profinet.Siemens;
using log4net;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Aucma.Core.HwPLc
1 year ago
{
/// <summary>
/// 西门子PLC
/// </summary>
public class SiemensPlc : IPlc
{
private readonly log4net.ILog log = LogManager.GetLogger(typeof(MelsecPlc));
private SiemensS7Net siemensS7Net;
private SiemensPLCS siemensPLCS = SiemensPLCS.S1200;
#region 构造函数
//public MelsecPlc()
//{
// if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
// {
// //log.Info("HslCommunication激活失败");
// return;
// }
// Console.WriteLine("HslCommunication激活成功!");
// siemensS7Net = new SiemensS7Net();
// siemensS7Net.ConnectTimeOut = 2000;
//}
public SiemensPlc(string iP, int port)
{
//if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
//{
// log.Error("HslCommunication激活失败");
// return;
//}
Console.WriteLine("HwCommunication激活成功!");
siemensS7Net = new SiemensS7Net(siemensPLCS);
siemensS7Net.ConnectTimeOut = 2000;
Connect(iP, port);//建立连接
}
#endregion
#region 注册
public bool Registed()
{
//if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
//{
// log.Info("HslCommunication激活失败");
// return false;
//}
Console.WriteLine("HwCommunication激活成功!");
siemensS7Net = new SiemensS7Net(siemensPLCS);
siemensS7Net.ConnectTimeOut = 2000;
return true;
}
#endregion
#region 是否连接
/// <summary>
/// 是否连接
/// </summary>
public bool IsConnected { get; set; }
#endregion
#region 建立连接
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string iP, int port)
{
1 year ago
//siemensS7Net.IpAddress = iP;//正式环境开启
1 year ago
siemensS7Net.Port = port;
try
{
OperateResult connect = siemensS7Net.ConnectServer();
if (connect.IsSuccess)
{
IsConnected = true;
return true;
}
else
{
siemensS7Net.ConnectClose();
IsConnected = false;
return false;
}
}
catch (Exception ex)
{
log.Error($"PLCl连接失败{ex.Message}");
IsConnected = false;
return false;
}
}
#endregion
#region 断开连接
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return siemensS7Net.ConnectClose().IsSuccess;
}
#endregion
#region 读取byte数据
/// <summary>
/// 读取byte数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public byte[] ReadBytes(string address,ushort len)
1 year ago
{
byte[] bytes = null;
try
{
OperateResult<byte[]> read = siemensS7Net.Read(address, len);
1 year ago
if (read.IsSuccess)
{
byte[] code = new byte[read.Content.Length - 2];
//Array.Copy(read.Content, 2, code, 0, 24);
//string scode = Encoding.ASCII.GetString(code, 0, code.Length);
//bytes = code;
bytes = read.Content;
1 year ago
}
}
catch (Exception ex)
{
log.Error("ReadBytes方法异常" + ex.ToString());
}
return bytes;
}
#endregion
#region 读取bool
/// <summary>
/// 读取bool
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public bool ReadBool(string address)
{
bool iflag = false;
try
{
OperateResult<bool> read = siemensS7Net.ReadBool(address);
if (read.IsSuccess)
{
iflag = read.Content;
}
return iflag;
}
catch (Exception ex)
{
log.Error("ReadBool方法异常" + ex.ToString());
}
return iflag;
}
#endregion
#region 读取int16
/// <summary>
/// 读取int16
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int ReadInt16(string address)
{
int returnflag = 0;
try
{
OperateResult<Int16> read = siemensS7Net.ReadInt16(address);
if (read.IsSuccess)
{
returnflag = read.Content;
}
}
catch (Exception ex)
{
log.Error("ReadInt16方法异常" + ex.ToString());
}
return returnflag;
}
#endregion
#region 读取int32
/// <summary>
/// 读取int32
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int ReadInt32(string address)
{
int returnflag = 0;
try
{
OperateResult<Int32> read = siemensS7Net.ReadInt32(address);
if (read.IsSuccess)
{
returnflag = read.Content;
}
}
catch (Exception ex)
{
log.Error("ReadInt32方法异常" + ex.ToString());
}
return returnflag;
}
#endregion
#region 读取string
/// <summary>
/// 读取string
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public string ReadString(string address)
{
string returnflag = "";
try
{
OperateResult<string> read = siemensS7Net.ReadString(address, 10);
if (read.IsSuccess)
{
returnflag = read.Content;
}
}
catch (Exception ex)
{
log.Error("ReadString方法异常" + ex.ToString());
}
return returnflag;
}
#endregion
#region 读取Float
/// <summary>
/// 读取string
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public float ReadFloat(string address)
{
float flag = 0;
try
{
OperateResult<float> read = siemensS7Net.ReadFloat(address);
if (read.IsSuccess)
{
flag = read.Content;
}
}
catch (Exception ex)
{
log.Error("ReadString方法异常" + ex.ToString());
}
return flag;
}
#endregion
#region 读取Double
/// <summary>
/// 读取double
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public double ReadDouble(string address)
{
double flag = 0;
try
{
OperateResult<double> read = siemensS7Net.ReadDouble(address);
if (read.IsSuccess)
{
flag = read.Content;
}
}
catch (Exception ex)
{
log.Error("ReadString方法异常" + ex.ToString());
}
return flag;
}
#endregion
#region 写入int16
/// <summary>
/// 写入int16
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool WriteInt16(string address, string value)
{
bool iflag = false;
try
{
OperateResult write = siemensS7Net.Write(address, short.Parse(value));
//Task<OperateResult<TimeSpan>> operateResult = siemensS7Net.Wait(address, short.Parse(value));
if (write.IsSuccess)
{
iflag = true;
}
else
{
iflag = false;
}
return iflag;
}
catch (Exception ex)
{
log.Error("WriteInt16方法异常" + ex.ToString());
return iflag;
}
}
#endregion
#region 写入int32
/// <summary>
/// 写入int32
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool WriteInt32(string address, int value)
{
bool iflag = false;
try
{
OperateResult write = siemensS7Net.Write(address, value);
if (write.IsSuccess)
{
iflag = true;
}
else
{
iflag = false;
}
return iflag;
}
catch (Exception ex)
{
log.Error("WriteInt32方法异常" + ex.ToString());
return iflag;
}
}
#endregion
#region 写入string
/// <summary>
/// 写入string
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool WriteString(string address, string value)
{
bool iflag = false;
try
{
OperateResult write = siemensS7Net.Write(address, value);
if (write.IsSuccess)
{
iflag = true;
}
else
{
iflag = false;
}
}
catch (Exception ex)
{
log.Error("WriteString方法异常" + ex.ToString());
iflag = false;
}
return iflag;
}
#endregion
#region 写入byte
/// <summary>
/// 写入byte
/// </summary>
/// <param name="address"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public bool WriteByte(string address, byte[] bytes)
{
bool iflag = false;
try
{
OperateResult write = siemensS7Net.Write(address, bytes);
if (write.IsSuccess)
{
iflag = true;
}
else
{
iflag = false;
}
}
catch (Exception ex)
{
log.Error("WriteByte方法异常" + ex.ToString());
iflag = false;
}
return iflag;
}
#endregion
#region 写入Float
/// <summary>
/// 写入byte
/// </summary>
/// <param name="address"></param>
/// <param name="bytes"></param>
/// <returns></returns>
public bool WriteFloat(string address, float value)
{
bool iflag = false;
try
{
OperateResult write = siemensS7Net.Write(address, value);
if (write.IsSuccess)
{
iflag = true;
}
else
{
iflag = false;
}
}
catch (Exception ex)
{
log.Error("WriteByte方法异常" + ex.ToString());
iflag = false;
}
return iflag;
}
#endregion
#region 心跳使用——喂狗
/// <summary>
/// 心跳使用
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public async Task<bool> Read(string address)
{
try
{
siemensS7Net.ReceiveTimeOut = 2000;
OperateResult<bool> read = siemensS7Net.ReadBool(address);
if (read.IsSuccess)
{
IsConnected = true;
return await Task.FromResult(IsConnected);
}
else
{
var k = read.ErrorCode < 0 ? false : true;
if (k)
{
IsConnected = true;
return await Task.FromResult(IsConnected);
}
else
{
IsConnected = false;
return await Task.FromResult(IsConnected);
}
}
}
catch (Exception ex)
{
log.Error("ReadInt32方法异常" + ex.ToString());
}
return false;
}
1 year ago
1 year ago
#endregion
1 year ago
/// <summary>
/// 读取byte
/// </summary>
/// <param name="address">地址</param>
/// <param name="len">长度</param>
/// <returns></returns>
public byte[] Read(string address, ushort len)
1 year ago
{
1 year ago
OperateResult<byte[]> readbyte = siemensS7Net.Read(address, len);
if (readbyte.IsSuccess)
{
IsConnected = true;
return readbyte.Content;
}
else
{
1 year ago
//IsConnected = false;
1 year ago
return null;
}
1 year ago
}
1 year ago
/// <summary>
/// 西门子高低位转化待定
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] ReadByte(string address, ushort len)
{
throw new NotImplementedException();
}
1 year ago
}
}