|
|
@@ -5,6 +5,7 @@ using System.ComponentModel;
|
|
|
using System.IO.Ports;
|
|
|
using YSAI.Core.data;
|
|
|
using YSAI.Core.subscription;
|
|
|
+using static YSAI.Modbus.client.ModbusClientData;
|
|
|
|
|
|
namespace YSAI.Modbus.client
|
|
|
{
|
|
|
@@ -200,175 +201,12 @@ namespace YSAI.Modbus.client
|
|
|
[Description("UdpParam")]
|
|
|
Udp
|
|
|
}
|
|
|
- /// <summary>
|
|
|
- /// TCP 连接参数
|
|
|
- /// </summary>
|
|
|
- public class TcpParam
|
|
|
- {
|
|
|
- /// <summary>
|
|
|
- /// IP
|
|
|
- /// </summary>
|
|
|
- [Description("IP")]
|
|
|
- public string? Ip { get; set; } = "127.0.0.1";
|
|
|
- /// <summary>
|
|
|
- /// 端口
|
|
|
- /// </summary>
|
|
|
- [Description("端口")]
|
|
|
- public int Port { get; set; } = 502;
|
|
|
- /// <summary>
|
|
|
- /// 连接超时时间
|
|
|
- /// </summary>
|
|
|
- [Description("连接超时时间")]
|
|
|
- public int ConnectTimeOut { get; set; } = 5000;
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 重写基类中的Equals方法
|
|
|
- /// </summary>
|
|
|
- /// <param name="obj"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public override bool Equals(object obj)
|
|
|
- {
|
|
|
- if (obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- TcpParam? Obj = obj as TcpParam;
|
|
|
- if (Obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (Ip == Obj.Ip &&
|
|
|
- Port == Obj.Port &&
|
|
|
- ConnectTimeOut == Obj.ConnectTimeOut)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// UDP 连接参数
|
|
|
- /// </summary>
|
|
|
- public class UdpParam
|
|
|
- {
|
|
|
- /// <summary>
|
|
|
- /// IP
|
|
|
- /// </summary>
|
|
|
- [Description("IP")]
|
|
|
- public string? Ip { get; set; } = "127.0.0.1";
|
|
|
- /// <summary>
|
|
|
- /// 端口
|
|
|
- /// </summary>
|
|
|
- [Description("端口")]
|
|
|
- public int Port { get; set; } = 502;
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 重写基类中的Equals方法
|
|
|
- /// </summary>
|
|
|
- /// <param name="obj"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public override bool Equals(object obj)
|
|
|
- {
|
|
|
- if (obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- UdpParam? Obj = obj as UdpParam;
|
|
|
- if (Obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (Ip == Obj.Ip &&
|
|
|
- Port == Obj.Port)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// RTU 连接参数
|
|
|
- /// </summary>
|
|
|
- public class SerialPortParam
|
|
|
- {
|
|
|
- /// <summary>
|
|
|
- /// 串口号
|
|
|
- /// </summary>
|
|
|
- [Description("串口号")]
|
|
|
- public string? PortName { get; set; }
|
|
|
- /// <summary>
|
|
|
- /// 波特率
|
|
|
- /// </summary>
|
|
|
- [Description("波特率")]
|
|
|
- public int BaudRate { get; set; } = 19200;
|
|
|
- /// <summary>
|
|
|
- /// 校验位
|
|
|
- /// </summary>
|
|
|
- [Description("校验位")]
|
|
|
- [JsonConverter(typeof(StringEnumConverter))]
|
|
|
- public Parity ParityBit { get; set; } = Parity.Even;
|
|
|
- /// <summary>
|
|
|
- /// 数据位
|
|
|
- /// </summary>
|
|
|
- [Description("数据位")]
|
|
|
- public int DataBit { get; set; } = 8;
|
|
|
- /// <summary>
|
|
|
- /// 停止位
|
|
|
- /// </summary>
|
|
|
- [Description("停止位")]
|
|
|
- [JsonConverter(typeof(StringEnumConverter))]
|
|
|
- public StopBits StopBit { get; set; } = StopBits.One;
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 重写基类中的Equals方法
|
|
|
- /// </summary>
|
|
|
- /// <param name="obj"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public override bool Equals(object obj)
|
|
|
- {
|
|
|
- if (obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- SerialPortParam? Obj = obj as SerialPortParam;
|
|
|
- if (Obj == null)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (PortName == Obj.PortName &&
|
|
|
- BaudRate == Obj.BaudRate &&
|
|
|
- ParityBit == Obj.ParityBit &&
|
|
|
- DataBit == Obj.DataBit &&
|
|
|
- StopBit == Obj.StopBit)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 基础数据
|
|
|
/// </summary>
|
|
|
- public class Basics: SubscribeData.SCData
|
|
|
+ public class Basics : SubscribeData.SCData
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 唯一标识符
|
|
|
@@ -412,23 +250,52 @@ namespace YSAI.Modbus.client
|
|
|
[Description("写入超时时间")]
|
|
|
public int WriteTimeOut { get; set; } = 2000;
|
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// TCP参数
|
|
|
+ /// IP
|
|
|
/// </summary>
|
|
|
- [Description("TCP参数")]
|
|
|
- public TcpParam? TcpParam { get; set; }
|
|
|
-
|
|
|
+ [Description("IP")]
|
|
|
+ public string? Ip { get; set; } = "127.0.0.1";
|
|
|
/// <summary>
|
|
|
- /// UDP参数
|
|
|
+ /// 端口
|
|
|
+ /// </summary>
|
|
|
+ [Description("端口")]
|
|
|
+ public int Port { get; set; } = 502;
|
|
|
+ /// <summary>
|
|
|
+ /// 连接超时时间
|
|
|
/// </summary>
|
|
|
- [Description("UDP参数")]
|
|
|
- public UdpParam? UdpParam { get; set; }
|
|
|
+ [Description("连接超时时间")]
|
|
|
+ public int ConnectTimeOut { get; set; } = 5000;
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 串口参数
|
|
|
+ /// 串口号
|
|
|
+ /// </summary>
|
|
|
+ [Description("串口号")]
|
|
|
+ public string? PortName { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 波特率
|
|
|
/// </summary>
|
|
|
- [Description("串口参数")]
|
|
|
- public SerialPortParam? SerialPortParam { get; set; }
|
|
|
+ [Description("波特率")]
|
|
|
+ public int BaudRate { get; set; } = 19200;
|
|
|
+ /// <summary>
|
|
|
+ /// 校验位
|
|
|
+ /// </summary>
|
|
|
+ [Description("校验位")]
|
|
|
+ [JsonConverter(typeof(StringEnumConverter))]
|
|
|
+ public Parity ParityBit { get; set; } = Parity.Even;
|
|
|
+ /// <summary>
|
|
|
+ /// 数据位
|
|
|
+ /// </summary>
|
|
|
+ [Description("数据位")]
|
|
|
+ public int DataBit { get; set; } = 8;
|
|
|
+ /// <summary>
|
|
|
+ /// 停止位
|
|
|
+ /// </summary>
|
|
|
+ [Description("停止位")]
|
|
|
+ [JsonConverter(typeof(StringEnumConverter))]
|
|
|
+ public StopBits StopBit { get; set; } = StopBits.One;
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
/// 重写基类中的Equals方法
|
|
|
@@ -448,28 +315,22 @@ namespace YSAI.Modbus.client
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (SleepTime == Obj.SleepTime &&
|
|
|
- SameDataOut == Obj.SameDataOut &&
|
|
|
- DataChangeOut == Obj.DataChangeOut &&
|
|
|
- ProtocolType == Obj.ProtocolType &&
|
|
|
- ReadTimeOut == Obj.ReadTimeOut &&
|
|
|
- WriteTimeOut == Obj.WriteTimeOut)
|
|
|
+ if (PortName == Obj.PortName &&
|
|
|
+ BaudRate == Obj.BaudRate &&
|
|
|
+ ParityBit == Obj.ParityBit &&
|
|
|
+ DataBit == Obj.DataBit &&
|
|
|
+ StopBit == Obj.StopBit &&
|
|
|
+ Ip == Obj.Ip &&
|
|
|
+ Port == Obj.Port &&
|
|
|
+ ConnectTimeOut == Obj.ConnectTimeOut &&
|
|
|
+ SleepTime == Obj.SleepTime &&
|
|
|
+ SameDataOut == Obj.SameDataOut &&
|
|
|
+ DataChangeOut == Obj.DataChangeOut &&
|
|
|
+ ProtocolType == Obj.ProtocolType &&
|
|
|
+ ReadTimeOut == Obj.ReadTimeOut &&
|
|
|
+ WriteTimeOut == Obj.WriteTimeOut)
|
|
|
{
|
|
|
- bool State = false;
|
|
|
- switch (ProtocolType)
|
|
|
- {
|
|
|
- case ProtocolType.Ascii:
|
|
|
- case ProtocolType.Rtu:
|
|
|
- State = SerialPortParam.Equals(Obj.SerialPortParam);
|
|
|
- break;
|
|
|
- case ProtocolType.Tcp:
|
|
|
- State = TcpParam.Equals(Obj.TcpParam);
|
|
|
- break;
|
|
|
- case ProtocolType.Udp:
|
|
|
- State = UdpParam.Equals(Obj.UdpParam);
|
|
|
- break;
|
|
|
- }
|
|
|
- return State;
|
|
|
+ return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -477,7 +338,6 @@ namespace YSAI.Modbus.client
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/// <summary>
|