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.

530 lines
15 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using S71500.Enum;
namespace S71500.Impl
{
[ClassInterface(ClassInterfaceType.None)]
public class Adapter
{
static SiemensS7Net s7 = null;
public static UInt16 m_iDeviceId = 0;
private static System.Net.IPAddress address;
private static SiemensPLCS type;
public Adapter()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
// //LogHelper.Info("HslCommunication激活失败");
return;
}
s7 = new SiemensS7Net(SiemensPLCS.S1500);
s7.ConnectTimeOut = 2000;
// //LogHelper.Info("HslCommunication 11.0.6.0激活成功");
}
public static bool Registe()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
return false;
}
s7 = new SiemensS7Net(SiemensPLCS.S1500);
s7.ConnectTimeOut = 2000;
return true;
}
public static bool Flage { get; set; } = true;
#region 是否建立连接
/// <summary>
/// 是否连接
/// </summary>
public static bool IsConnected { get; set; }
#endregion
#region 连接
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public static bool Connect(string IP, int Port)
{
try
{
s7 = new SiemensS7Net(SiemensPLCS.S1500);
s7.ConnectTimeOut = 2000;
s7.IpAddress = IP;
s7.Port = Port;
OperateResult connect = s7.ConnectServer();
if (connect.IsSuccess)
{
IsConnected = true;
////LogHelper.Info("西门子PLC建立连接成功");
return true;
}
else
{
IsConnected = false;
return false;
}
}
catch (Exception ex)
{
IsConnected = false;
//LogHelper.Error("西门子PLC异常" + ex.ToString());
return false;
}
}
#endregion
#region 关闭连接
/// <summary>
/// 关闭连接
/// </summary>
public static bool Device_Destroy()
{
OperateResult or = s7.ConnectClose();
s7.Dispose();
IsConnected = false;
return or.IsSuccess;
}
#endregion
#region 获取PLC状态
/// <summary>
/// 获取PLC状态
/// </summary>
/// <returns></returns>
public static bool Device_GetState(string address)
{
try
{
OperateResult read = s7.Read(address, 10);
if (read.IsSuccess)
{
IsConnected = true;
}
else
{
IsConnected = read.ErrorCode < 0 ? false : true;
}
return IsConnected;
}
catch (Exception)
{
IsConnected = false;
return false;
}
}
#endregion
#region 初始化
/// <summary>
/// 初始化
/// </summary>
/// <param name="iCommType"></param>
/// <param name="pUrl"></param>
/// <param name="iDeviceId"></param>
/// <param name="deviceType"></param>
/// <returns></returns>
public static bool Device_Init(string IP, int Port,CommType iCommType, string pUrl, ushort iDeviceId, string deviceType)
{
try
{
type = (SiemensPLCS)System.Enum.Parse(typeof(SiemensPLCS), deviceType);
m_iDeviceId = iDeviceId;
if (iCommType == CommType.RJ45) //网口
{
string[] split = pUrl.Split(new Char[] { ':' });
IP = split[0];
if (!System.Net.IPAddress.TryParse(IP, out address))
{
IsConnected = false;
return false;
}
string strTemp = split[1];
Port = Convert.ToInt32(strTemp);
IsConnected = true;
}
}
catch (Exception ex)
{
IsConnected = false;
//LogHelper.Error("连接设备异常:" + ex.ToString());
return false;
}
return true;
}
#endregion
#region 断开重连
/// <summary>
/// 断开重连
/// </summary>
/// <returns></returns>
public static bool DisConnect()
{
return s7.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 = s7.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 = s7.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 = s7.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 = s7.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 = s7.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 = s7.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 = s7.Write(address, short.Parse(value));
//Task<OperateResult<TimeSpan>> operateResult = s7.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 = s7.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 = s7.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 = s7.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 = s7.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>
/// 读取int32
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static bool Read(string address)
{
try
{
OperateResult<Int32> read = s7.ReadInt32(address);
if (read.IsSuccess)
{
return true;
}
else
{
var r = read.ErrorCode < 0 ? false : true;
return r;
}
}
catch (Exception ex)
{
//LogHelper.Error("ReadInt32方法异常" + ex.ToString());
}
return false;
}
#endregion
}
}