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.

295 lines
9.4 KiB
C#

using AUCMA.STORE.Common;
using AUCMA.STORE.DeviceAdapter;
using AUCMA.STORE.Entity.DTO;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using PlcDirectiveDTO = AUCMA.STORE.Entity.DTO.PlcDirectiveDTO;
namespace DeviceAdapter.Siemens
{
[ClassInterface(ClassInterfaceType.None)]
public class SiemensAdapter : IDeviceAdapter
{
SiemensS7Net s7;
private DeviceType m_iDeviceType;
private string m_strIp; //读写器IP或串口号
private int m_iPort; //读写器端口号或波特率
public UInt16 m_iDeviceId = 0;
private System.Net.IPAddress address;
private SiemensPLCS type;
public bool Device_Connect()
{
try
{
//LogHelper.HeartBeat("设备类型:" + type);
s7 = new SiemensS7Net(type);
s7.IpAddress = m_strIp;
OperateResult connect = s7.ConnectServer();
if (connect.IsSuccess)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogHelper.Error("Device_Connect方法异常" + ex.ToString());
return false;
}
}
public void Device_Destroy()
{
s7.ConnectClose();
}
public bool Device_GetState()
{
throw new NotImplementedException();
}
public bool Device_Init(CommType iCommType, string pUrl, ushort iDeviceId, string deviceType)
{
try
{
#if DEBUG
//LogHelper.Info("函数调用:Device_Init");
#endif
type = (SiemensPLCS)Enum.Parse(typeof(SiemensPLCS), deviceType);
m_iDeviceId = iDeviceId;
if (iCommType == CommType.RJ45) //网口
{
string[] split = pUrl.Split(new Char[] { ':' });
m_strIp = split[0];
if (!System.Net.IPAddress.TryParse(m_strIp, out address))
{
return false;
}
string strTemp = split[1];
m_iPort = Convert.ToInt32(strTemp);
#if DEBUG
//LogHelper.Info("设备初始化成功IP" + m_strIp + "端口号:" + m_iPort);
#endif
}
}
catch (Exception ex)
{
LogHelper.Error("连接设备异常:" + ex.ToString());
return false;
}
return true;
}
public bool Device_ReConnect()
{
return Device_Connect();
}
public byte[] ReadBytesByAddress(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);
}
}
catch (Exception ex)
{
LogHelper.Error("ReadBytesByAddress方法异常" + ex.ToString());
}
return bytes;
}
public bool ReadBoolByAddress(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("ReadBoolByAddress方法异常" + ex.ToString());
}
return iflag;
}
public int ReadInt16ByAddress(string address)
{
int returnflag = 0;
try
{
OperateResult<Int16> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
returnflag = read.Content;
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",读取数据:" + read.Content);
}
//LogHelper.HeartBeat("向设备编号:{0},地址:{1},进行读取:{2}" +",数据:{3}", m_iDeviceId, address, read.IsSuccess == true ? "成功" : "失败", read.Content);
}
catch (Exception ex)
{
LogHelper.Error("ReadInt16ByAddress方法异常" + ex.ToString());
}
return returnflag;
}
public int ReadInt32ByAddress(string address)
{
int returnflag = 0;
try
{
OperateResult<Int32> read = s7.ReadInt32(address);
if (read.IsSuccess)
{
returnflag = read.Content;
}
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",读取数据:" + read.Content);
}
catch (Exception ex)
{
LogHelper.Error("ReadInt32ByAddress方法异常" + ex.ToString());
}
return returnflag;
}
public string ReadStringByAddress(string address)
{
string returnflag = "";
try
{
OperateResult<string> read = s7.ReadString(address,22);
if (read.IsSuccess)
{
returnflag = read.Content;
}
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",读取数据:" + read.Content);
}
catch (Exception ex)
{
LogHelper.Error("ReadStringByAddress方法异常" + ex.ToString());
}
return returnflag;
}
public bool WriteInt16ByAddress(string address, string value)
{
bool iflag = false;
try
{
OperateResult write = s7.Write(address, short.Parse(value));
//Task<OperateResult<TimeSpan>> operateResult = s7.WaitAsync(address, short.Parse(value));
if (write.IsSuccess)
{
if (address != "DB8.DBW24" || address != "DB12.DBW20" || address != "DB18.0")
{
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",写入数据:" + value);
}
iflag = true;
}
else
{
iflag = false;
}
return iflag;
}
catch (Exception ex)
{
LogHelper.Error("WriteInt16ByAddress方法异常" + ex.ToString());
return iflag;
}
}
public bool WriteInt32ByAddress(string address, int value)
{
bool iflag = false;
try
{
OperateResult write = s7.Write(address, value);
if (write.IsSuccess)
{
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",写入数据:" + value);
iflag = true;
}
else
{
iflag = false;
}
return iflag;
}
catch (Exception ex)
{
LogHelper.Error("WriteInt32ByAddress方法异常" + ex.ToString());
return iflag;
}
}
public bool WriteStringByAddress(string address, string value)
{
bool iflag = false;
try
{
OperateResult write = s7.Write(address, value);
if (write.IsSuccess)
{
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address + ",写入数据:" + value);
iflag = true;
}
else
{
iflag = false;
}
}
catch (Exception ex)
{
LogHelper.Error("WriteStringByAddress方法异常" + ex.ToString());
iflag = false;
}
return iflag;
}
public bool WriteByteByAddress(string address, byte[] bytes)
{
bool iflag = false;
try
{
OperateResult write = s7.Write(address, bytes);
if (write.IsSuccess)
{
iflag = true;
}
else
{
LogHelper.HeartBeat("向设备编号[" + m_iDeviceId + "],地址:" + address+"写入失败:"+ write.Message);
iflag = false;
}
}
catch (Exception ex)
{
LogHelper.Error("WriteByteByAddress方法异常" + ex.ToString());
iflag = false;
}
return iflag;
}
}
}