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.
463 lines
13 KiB
C#
463 lines
13 KiB
C#
using HslCommunication;
|
|
using HslCommunication.Profinet.Melsec;
|
|
using System;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Aucma.Core.MelsecPLc
|
|
{
|
|
/// <summary>
|
|
/// 三菱PLC
|
|
/// </summary>
|
|
public class MelsecPlc //: IMelsecPlc
|
|
{
|
|
private static MelsecMcNet? melsecMc = null;
|
|
|
|
#region 构造函数
|
|
//public MelsecPlc()
|
|
//{
|
|
// if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
|
|
// {
|
|
// //log.Info("HslCommunication激活失败");
|
|
// return;
|
|
// }
|
|
// Console.WriteLine("HslCommunication激活成功!");
|
|
// melsecMc = new MelsecMcNet();
|
|
// melsecMc.ConnectTimeOut = 2000;
|
|
//}
|
|
#endregion
|
|
|
|
#region 注册
|
|
public static bool Registed()
|
|
{
|
|
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
|
|
{
|
|
//log.Info("HslCommunication激活失败");
|
|
return false;
|
|
}
|
|
Console.WriteLine("HslCommunication激活成功!");
|
|
melsecMc = new MelsecMcNet();
|
|
melsecMc.ConnectTimeOut = 2000;
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
#region 是否连接
|
|
/// <summary>
|
|
/// 是否连接
|
|
/// </summary>
|
|
public static bool IsConnected { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region 建立连接
|
|
/// <summary>
|
|
/// 建立连接
|
|
/// </summary>
|
|
/// <param name="IP"></param>
|
|
/// <param name="port"></param>
|
|
/// <returns></returns>
|
|
public static bool Connect(string iP, int port)
|
|
{
|
|
//melsecMc.IpAddress = iP;//正式环境开启
|
|
melsecMc.Port = port;
|
|
|
|
// 如果网络不太理想,配置了两个端口,一个有问题,立即切换另一个的话,可以配置如下的代码
|
|
//melsecMc.GetPipeSocket().SetMultiPorts(new int[] { 6000, 6001 });
|
|
try
|
|
{
|
|
OperateResult connect = melsecMc.ConnectServer();
|
|
if (connect.IsSuccess)
|
|
{
|
|
IsConnected = true;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
melsecMc.ConnectClose();
|
|
IsConnected = false;
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
IsConnected = false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 断开连接
|
|
/// <summary>
|
|
/// 断开连接
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static bool DisConnect()
|
|
{
|
|
return melsecMc.ConnectClose().IsSuccess;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取byte数据
|
|
/// <summary>
|
|
/// 读取byte数据
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static byte[] ReadBytes(string address)
|
|
{
|
|
byte[] bytes = null;
|
|
try
|
|
{
|
|
OperateResult<byte[]> read = melsecMc.Read(address, 26);
|
|
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;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadBytes方法异常" + ex.ToString());
|
|
}
|
|
return bytes;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取bool
|
|
/// <summary>
|
|
/// 读取bool
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static bool ReadBool(string address)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult<bool> read = melsecMc.ReadBool(address);
|
|
if (read.IsSuccess)
|
|
{
|
|
iflag = read.Content;
|
|
}
|
|
return iflag;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadBool方法异常" + ex.ToString());
|
|
}
|
|
return iflag;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取int16
|
|
/// <summary>
|
|
/// 读取int16
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static int ReadInt16(string address)
|
|
{
|
|
int returnflag = 0;
|
|
try
|
|
{
|
|
OperateResult<Int16> read = melsecMc.ReadInt16(address);
|
|
if (read.IsSuccess)
|
|
{
|
|
returnflag = read.Content;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadInt16方法异常" + ex.ToString());
|
|
}
|
|
return returnflag;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取int32
|
|
/// <summary>
|
|
/// 读取int32
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static int ReadInt32(string address)
|
|
{
|
|
int returnflag = 0;
|
|
try
|
|
{
|
|
OperateResult<Int32> read = melsecMc.ReadInt32(address);
|
|
if (read.IsSuccess)
|
|
{
|
|
returnflag = read.Content;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadInt32方法异常" + ex.ToString());
|
|
}
|
|
return returnflag;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取string
|
|
|
|
/// <summary>
|
|
/// 读取string
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static string ReadString(string address)
|
|
{
|
|
string returnflag = "";
|
|
try
|
|
{
|
|
OperateResult<string> read = melsecMc.ReadString(address, 10);
|
|
if (read.IsSuccess)
|
|
{
|
|
returnflag = read.Content;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadString方法异常" + ex.ToString());
|
|
}
|
|
return returnflag;
|
|
}
|
|
#endregion
|
|
|
|
#region 读取Float
|
|
|
|
/// <summary>
|
|
/// 读取string
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static float ReadFloat(string address)
|
|
{
|
|
float flag = 0;
|
|
try
|
|
{
|
|
OperateResult<float> read = melsecMc.ReadFloat(address);
|
|
if (read.IsSuccess)
|
|
{
|
|
flag = read.Content;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadString方法异常" + ex.ToString());
|
|
}
|
|
return flag;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 写入int16
|
|
/// <summary>
|
|
/// 写入int16
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteInt16(string address, string value)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult write = melsecMc.Write(address, short.Parse(value));
|
|
//Task<OperateResult<TimeSpan>> operateResult = melsecMc.Wait(address, short.Parse(value));
|
|
|
|
if (write.IsSuccess)
|
|
{
|
|
iflag = true;
|
|
}
|
|
else
|
|
{
|
|
iflag = false;
|
|
}
|
|
return iflag;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("WriteInt16方法异常" + ex.ToString());
|
|
return iflag;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 写入int32
|
|
/// <summary>
|
|
/// 写入int32
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteInt32(string address, int value)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult write = melsecMc.Write(address, value);
|
|
if (write.IsSuccess)
|
|
{
|
|
iflag = true;
|
|
}
|
|
else
|
|
{
|
|
iflag = false;
|
|
}
|
|
return iflag;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("WriteInt32方法异常" + ex.ToString());
|
|
return iflag;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 写入string
|
|
/// <summary>
|
|
/// 写入string
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteString(string address, string value)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult write = melsecMc.Write(address, value);
|
|
if (write.IsSuccess)
|
|
{
|
|
iflag = true;
|
|
}
|
|
else
|
|
{
|
|
iflag = false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.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 static bool WriteByte(string address, byte[] bytes)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult write = melsecMc.Write(address, bytes);
|
|
if (write.IsSuccess)
|
|
{
|
|
iflag = true;
|
|
}
|
|
else
|
|
{
|
|
iflag = false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.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 static bool WriteFloat(string address, float value)
|
|
{
|
|
bool iflag = false;
|
|
try
|
|
{
|
|
OperateResult write = melsecMc.Write(address, value);
|
|
if (write.IsSuccess)
|
|
{
|
|
iflag = true;
|
|
}
|
|
else
|
|
{
|
|
iflag = false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("WriteByte方法异常" + ex.ToString());
|
|
iflag = false;
|
|
}
|
|
return iflag;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region 心跳使用——喂狗
|
|
/// <summary>
|
|
/// 心跳使用
|
|
/// </summary>
|
|
/// <param name="address"></param>
|
|
/// <returns></returns>
|
|
public static async Task<bool> Read(string address)
|
|
{
|
|
try
|
|
{
|
|
melsecMc.ReceiveTimeOut = 2000;
|
|
OperateResult<bool> read = await melsecMc.ReadBoolAsync(address);
|
|
if (read.IsSuccess)
|
|
{
|
|
IsConnected = true;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
var k = read.ErrorCode < 0 ? false : true;
|
|
if (k)
|
|
{
|
|
IsConnected = true;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
IsConnected = false;
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//LogHelper.Error("ReadInt32方法异常" + ex.ToString());
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|