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.

361 lines
13 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 HslCommunication.Profinet.Omron;
using HslCommunication;
using SlnMesnac.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.Plc.Impl
{
/// <summary>
/// 欧姆龙NJ系列PLC
/// </summary>
public class OmronNJPlc : IPlc
{
private ILogger<OmronNJPlc> _logger;
private StringChange _stringChange;
private OmronFinsNet omronFinsNet = null;
public OmronNJPlc(ILogger<OmronNJPlc> logger,StringChange stringChange)
{
_logger = logger;
_stringChange = stringChange;
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
PrintLogInfo("欧姆龙NJ系列PLC连接开始");
omronFinsNet.IpAddress = IP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
try
{
OperateResult connect = omronFinsNet.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
PrintLogInfo("欧姆龙NJ系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
PrintLogInfo($"欧姆龙NJ系列PLC建立连接失败:{connect.Message}");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
PrintLogInfo("欧姆龙NJ系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return omronFinsNet.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
PrintLogInfo($"开始通过地址:{address},读取长度:{len}的PLC数据");
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = _stringChange.ConvertFloatToINt(read.Content);
PrintLogInfo(String.Format("通过地址和长度读取PLC数据成功{0}", _stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
PrintLogInfo("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(string address,int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt16ByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入Short类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeShortByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.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)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}