using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
using logtxtWrite;
using iWareModel;
using iWareCommon.Utils;
namespace iWareSda
{
public class SS7 : PLCService
{
private PlcS7 plcS7 = null;
public SS7(SS7Param s7Param)
{
plcS7 = new PlcS7(s7Param.cpu, s7Param.ip, s7Param.rack, s7Param.slot);
_LogType = LogType.SS7;
}
///
/// 打开S7连接
///
///
public override MessageModel OpenService()
{
MessageModel fre = new MessageModel();
try
{
var code = plcS7.Open();
if (code == ErrorCode.NoError)
{
base.IsConnected = true;
fre.result = true;
return fre;
}
base.IsConnected = false;
fre.result = false;
fre.resMsg = "打开失败," + code.ToString();
return fre;
}
catch (Exception ex)
{
base.IsConnected = false;
Log4NetHelper.WriteErrorLog(_LogType, "初始化s7Init出现异常", ex);
throw ex;
}
}
///
/// S7读取
///
/// DB块名
/// 偏移量
///
///
public override MessageModel WriteValuePoint(string dbNumber, string offset, object value, Object proObj)
{
MessageModel fre = new MessageModel();
object objReuslt = new object();
var address = GetAddress(dbNumber, offset, value.GetType());
try
{
if (value.GetType() == typeof(bool))
{//布尔要转换成1和0写入
int myData = Convert.ToInt32(value);
objReuslt = plcS7.Write(address, myData);
}
//浮点
else if (value.GetType() == typeof(double) || value.GetType() == typeof(float))
{
double MyData = Convert.ToDouble(value);
objReuslt = plcS7.Write(address, MyData.ConvertToUInt());
}
//整数
else if (value.GetType() == typeof(Int32) || value.GetType() == typeof(short))
{
short MyData = Convert.ToInt16(value);
objReuslt = plcS7.Write(address, MyData.ConvertToUshort());
}
//双整数
else if (value.GetType() == typeof(Int32))
{
int myData = Convert.ToInt32(value);
objReuslt = plcS7.Write(address, myData);
}
//Char
else if (value.GetType() == typeof(Char))
{//char要转换成int类型,写给PLC
int myData = Convert.ToInt32(value);
byte b = (byte)myData;
objReuslt = plcS7.Write(address, b);
}
else
{
objReuslt = plcS7.Write(address, value);
}
ValidateReusltForWrite(objReuslt, address);
Log4NetHelper.WriteInfoLog(LogType.SS7, String.Format("写入地址:{0},值:{1}", address, value.ToString()));
fre.result = true;
return fre;
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(LogType.SS7, String.Format("写入出现异常:" + ex.Message + ",地址:{0},值:{1}", address, value.ToString()), ex);
fre.result = false;
fre.resMsg = "写入异常:" + ex.Message;
return fre;
}
}
///
/// S7读取
///
/// 地址
///
///
public override object ReadValuePoint(string dbNumber, string offset, Type type)
{
var address = GetAddress(dbNumber, offset, type);
object objReuslt = null;
try
{
objReuslt = plcS7.Read(address);
ValidateReusltForRead(objReuslt, address);
if (type == typeof(Char))
{//特别处理char类型
char _plcData = (char)Convert.ToInt32(objReuslt);
objReuslt = _plcData.ToString();
}
else if (type == typeof(double))
{
objReuslt = ((uint)objReuslt).ConvertToDouble();
}
else if (type == typeof(bool))
{
return Convert.ToBoolean(objReuslt);
}
}
catch (Exception ex)
{
Log4NetHelper.WriteErrorLog(LogType.SS7, String.Format("读取出现异常:" + ex.Message + ",地址:{0}", address), ex);
throw;
}
return objReuslt;
}
public override string GetAddress(string dbNumber, string offset, Type type = default(Type))
{
var address = "";
if (type == typeof(bool))
{//布尔要转换成1和0写入
address = "DB" + dbNumber + "." + "DBX" + offset;
}
//浮点
else if (type == typeof(double) || type == typeof(float))
{
address = "DB" + dbNumber + "." + "DBD" + offset;
}
//整数
else if (type == typeof(Int32) || type == typeof(short))
{
address = "DB" + dbNumber + "." + "DBW" + offset;
}
//双整数
else if (type == typeof(Int32))
{
address = "DB" + dbNumber + "." + "DBD" + offset;
}
//Char
else if (type == typeof(Char))
{//char要转换成int类型,写给PLC
address = "DB" + dbNumber + "." + "DBB" + offset;
}
else
{
address = "DB" + dbNumber + "." + "DBW" + offset;
}
return address;
}
public override void Close()
{
if (plcS7 != null)
plcS7.Close();
}
#region 私有方法
///
/// 验证结果(读取)
///
///
private void ValidateReusltForRead(Object objReuslt, string address)
{
if (objReuslt.GetType() == typeof(ErrorCode))
{
ErrorCode errCode = (ErrorCode)objReuslt;
if (errCode != ErrorCode.NoError)
{
throw new Exception(String.Format("读取" + address + "出现异常:" + errCode.ToString()));
}
}
}
///
/// 验证结果(写入)
///
///
private void ValidateReusltForWrite(Object objReuslt, string address)
{
if (objReuslt.GetType() == typeof(ErrorCode))
{
ErrorCode errCode = (ErrorCode)objReuslt;
if (errCode != ErrorCode.NoError)
{
throw new Exception(String.Format("写入" + address + "出现异常:" + errCode.ToString()));
}
}
}
#endregion
}
}