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.

336 lines
12 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 HslCommunication;
using HslCommunication.Profinet.Omron;
using SlnMesnac.Common;
namespace SlnMesnac.Plc.Factory
{
public class OmronNJFactory:PlcAbsractFactory
{
private StringChange _stringChange;
private OmronFinsNet omronFinsNet = null;
public OmronNJFactory(StringChange stringChange)
{
_stringChange = stringChange;
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public override bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="iP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool Connect(string iP, int port)
{
try
{
omronFinsNet.IpAddress = iP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
OperateResult connect = omronFinsNet.ConnectServer();
this.IsConnected = connect.IsSuccess;
if (!connect.IsSuccess)
{
return false;
}
return connect.IsSuccess;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool DisConnect()
{
try
{
OperateResult disConnect = omronFinsNet.ConnectClose();
this.IsConnected = false;
if (!disConnect.IsSuccess)
{
throw new InvalidOperationException($"欧姆龙PLC断开连接失败:{disConnect.Message}");
}
return disConnect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"欧姆龙PLC断开连接异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override byte[] readValueByAddress(string address, int len)
{
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据失败:{read.Message}");
}
return _stringChange.ConvertFloatToINt(read.Content);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据异常:{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int16数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt16ByAddress(string address)
{
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int32数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt32ByAddress(string address)
{
try
{
OperateResult<int> read = omronFinsNet.ReadInt32(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int32数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int32数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int16数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt16ByAddress(string address, int value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt16(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int32数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt32ByAddress(string address, int value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int32数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int32数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeStringByAddress(string address, string value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool readBoolByAddress(string address)
{
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取心跳结果
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public override bool readHeartByAddress(string address)
{
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (!read.IsSuccess)
{
return false;
}
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeBoolByAddress(string address, bool value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToDouble(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据异常{ex.Message}");
}
}
}
}