|
|
|
@ -62,7 +62,16 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 断开连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool DisConnect()
|
|
|
|
|
{
|
|
|
|
|
return s7.ConnectClose().IsSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过地址和长度读取PLC数据
|
|
|
|
|
/// </summary>
|
|
|
|
@ -72,7 +81,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public byte[] readValueByAddress(int len, string address)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo("开始通过PLC地址和长度读取PLC数据");
|
|
|
|
|
PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<byte[]> read = s7.Read(address, (ushort)(len));
|
|
|
|
@ -84,7 +93,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过地址和长度读取PLC数据失败!!!");
|
|
|
|
|
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
@ -100,182 +109,86 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址写入int类型数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public bool writeValueByAddress(int value, string address)
|
|
|
|
|
public bool writeValueByAddress(string address,int value)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult operateResult = s7.Write(address, Convert.ToInt32(value));
|
|
|
|
|
if (operateResult.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过PLC地址写入int类型数据", ex);
|
|
|
|
|
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址清零数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <param name="len"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public bool resetByAddress(string address, int len)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开发通过PLC地址{0}清零数据", address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
byte[] write = new byte[len * 2];
|
|
|
|
|
for (int i = 0; i < len * 2; i++)
|
|
|
|
|
{
|
|
|
|
|
write[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
OperateResult operateResult = s7.Write(address, write);
|
|
|
|
|
|
|
|
|
|
if (operateResult.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}清零数据成功", address));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}清零数据失败!!!", address));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}清零数据异常", address), ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址读取EA值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public string readEaByAddress(string address)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("通过PLC地址{0}读取EA值", address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<Byte[]> read = s7.Read(address, (ushort)(8));
|
|
|
|
|
|
|
|
|
|
if (read.IsSuccess && read.Content != null)
|
|
|
|
|
{
|
|
|
|
|
string result = Convert.ToString(read.Content);
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取EA值失败!!!", address));
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过PLC地址读取EA值异常", ex);
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址读取交互信号
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int readInteractiveSignal(string address)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}读取交互信号", address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<short> read = s7.ReadInt16(address);
|
|
|
|
|
if (read.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content));
|
|
|
|
|
return read.Content;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号失败!!!", address));
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过PLC地址读取交互信号异常", ex);
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址读取int32类型数据
|
|
|
|
|
/// 通过PLC地址读取int16类型数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int readInt32ByAddress(string address)
|
|
|
|
|
public int readInt16ByAddress(string address)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<short> read = s7.ReadInt16(address);
|
|
|
|
|
if (read.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功:{1}", address, read.Content));
|
|
|
|
|
return read.Content;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据失败!!!", address));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败!!!", address));
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
|
|
|
|
|
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过PLC地址写入int32类型数据
|
|
|
|
|
/// 通过PLC地址写入Short类型数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public bool writeInt32ByAddress(string address, int value)
|
|
|
|
|
public bool writeShortByAddress(string address, int value)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult write = s7.Write(address, short.Parse(Convert.ToString(value)));
|
|
|
|
|
if (write.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}成功", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex);
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
|
|
|
|
|
this.IsConnected = false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -290,7 +203,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public bool writeStringByAddress(string address, string value)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult operateResult = s7.Write(address, value);
|
|
|
|
@ -319,7 +232,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public string readStringByAddress(string address, ushort length)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<String> read = s7.ReadString(address, length);
|
|
|
|
@ -334,7 +247,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
|
|
|
|
|
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -347,7 +260,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public bool readBoolByAddress(string address)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult<bool> read = s7.ReadBool(address);
|
|
|
|
@ -376,7 +289,7 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool writeBoolByAddress(string address, bool value)
|
|
|
|
|
{
|
|
|
|
|
//PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult write = s7.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
|
|
|
|
@ -397,14 +310,32 @@ namespace SlnMesnac.Plc.Impl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DisConnect()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 写入Double类型
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool writeDoubleByAddress(string address, int value)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OperateResult write = s7.Write(address, Convert.ToDouble(value));
|
|
|
|
|
|
|
|
|
|
if (write.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PrintLogInfo(string message, Exception ex = null)
|
|
|
|
|