|
|
|
@ -26,10 +26,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
{
|
|
|
|
|
private SiemensS7Net siemensS7Net = null;
|
|
|
|
|
|
|
|
|
|
public event EventHandler<string> ErrorMessage = null;
|
|
|
|
|
|
|
|
|
|
public event EventHandler<string> InfoMessage = null;
|
|
|
|
|
|
|
|
|
|
public PLCCommunicationService()
|
|
|
|
|
{
|
|
|
|
|
siemensS7Net = new SiemensS7Net(SiemensPLCS.S200Smart);
|
|
|
|
@ -42,22 +38,26 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <param name="port"></param>
|
|
|
|
|
public void Connection(string address, int port = 0)
|
|
|
|
|
public PLCResult Connection(string address, int port = 0)
|
|
|
|
|
{
|
|
|
|
|
siemensS7Net.IpAddress = address;
|
|
|
|
|
if (port > 0)
|
|
|
|
|
{
|
|
|
|
|
siemensS7Net.Port = port;
|
|
|
|
|
}
|
|
|
|
|
siemensS7Net.ConnectServer();
|
|
|
|
|
var operateResult = siemensS7Net.ConnectServer();
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭连接PLC(长连接)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ColseConnection()
|
|
|
|
|
public PLCResult ColseConnection()
|
|
|
|
|
{
|
|
|
|
|
siemensS7Net.ConnectClose();
|
|
|
|
|
var operateResult = siemensS7Net.ConnectClose();
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -65,11 +65,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool ReadBool(string address)
|
|
|
|
|
public PLCResult<bool> ReadBool(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadBool(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadBool(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -77,11 +77,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public byte ReadByte(string address)
|
|
|
|
|
public PLCResult<byte> ReadByte(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadByte(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadByte(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -89,11 +89,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int ReadInt16(string address)
|
|
|
|
|
public PLCResult<short> ReadInt16(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadInt16(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadInt16(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -101,11 +101,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int ReadInt32(string address)
|
|
|
|
|
public PLCResult<int> ReadInt32(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadInt32(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadInt32(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -113,33 +113,33 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public long ReadLong(string address)
|
|
|
|
|
public PLCResult<long> ReadLong(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadInt64(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadInt64(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取指定地址的Float值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public float ReadFloat(string address)
|
|
|
|
|
public PLCResult<float> ReadFloat(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadFloat(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadFloat(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取指定地址的double值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public double ReadDouble(string address)
|
|
|
|
|
public PLCResult<double> ReadDouble(string address)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.ReadDouble(address);
|
|
|
|
|
VerifyResult(result, address);
|
|
|
|
|
return result.Content;
|
|
|
|
|
var operateResult = siemensS7Net.ReadDouble(address);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -147,10 +147,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, bool value)
|
|
|
|
|
public PLCResult Write(string address, bool value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -158,10 +159,12 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, byte value)
|
|
|
|
|
public PLCResult Write(string address, byte value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -169,10 +172,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, Int16 value)
|
|
|
|
|
public PLCResult Write(string address, Int16 value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -180,10 +184,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, Int32 value)
|
|
|
|
|
public PLCResult Write(string address, Int32 value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -191,10 +196,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, float value)
|
|
|
|
|
public PLCResult Write(string address, float value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -202,10 +208,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, double value)
|
|
|
|
|
public PLCResult Write(string address, double value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -213,10 +220,11 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">写入地址</param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
public void Write(string address, long value)
|
|
|
|
|
public PLCResult Write(string address, long value)
|
|
|
|
|
{
|
|
|
|
|
var result = siemensS7Net.Write(address, value);
|
|
|
|
|
VerifyResult(result, address, value);
|
|
|
|
|
var operateResult = siemensS7Net.Write(address, value);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
@ -257,7 +265,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<bool>> ReadBoolAsync(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadBoolAsync(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -270,7 +277,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<byte>> ReadByteAsync(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadByteAsync(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -283,7 +289,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<short>> ReadInt16Async(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadInt16Async(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -296,7 +301,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<int>> ReadInt32Async(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadInt32Async(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -309,7 +313,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<long>> ReadLongAsync(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadInt64Async(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -321,7 +324,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<float>> ReadFloatAsync(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadFloatAsync(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -333,7 +335,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult<double>> ReadDoubleAsync(string address)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.ReadDoubleAsync(address);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -346,7 +347,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, bool value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -360,7 +360,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -373,7 +372,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, Int16 value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -386,7 +384,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, Int32 value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -399,7 +396,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, float value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -412,7 +408,6 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, double value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
@ -425,61 +420,13 @@ namespace GuideScreen.Common.Services.Impl
|
|
|
|
|
public async Task<PLCResult> WriteAsync(string address, long value)
|
|
|
|
|
{
|
|
|
|
|
var operateResult = await siemensS7Net.WriteAsync(address, value);
|
|
|
|
|
VerifyResult(operateResult);
|
|
|
|
|
var result = ConvertResult(operateResult);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 验证结果 如果启用了 ErrorMessage 就不抛出异常信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="result"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="Exception"></exception>
|
|
|
|
|
private bool VerifyResult(OperateResult result, string address = "", object value = null)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var message = $"address:{address}";
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
message += $",value:{value}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object content = null;
|
|
|
|
|
|
|
|
|
|
if (result.GetProperties().Where(f => f.Name == "Content").Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
content = result.GetProperty<object>("Content");
|
|
|
|
|
}
|
|
|
|
|
if (content != null)
|
|
|
|
|
{
|
|
|
|
|
message += $",content:{content}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message += "," + result.Message;
|
|
|
|
|
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
InfoMessage.Invoke(this, "PLC信息:" + message);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ErrorMessage == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(result.Message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage.Invoke(this, "PLC异常:" + message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 转换 result (将框架结果结构转换成当前程序结果结构)
|
|
|
|
|
/// </summary>
|
|
|
|
|