| | |
| | | using HslCommunication.Profinet.Omron; |
| | | using HslCommunication.Profinet.Siemens; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | |
| | | namespace WIDESEAWCS_Communicator |
| | | { |
| | | /// <summary> |
| | | /// 西门子S7通讯类 |
| | | /// 欧姆龙EtherNet/IP(CIP) |
| | | /// </summary> |
| | | [Description("欧姆龙EtherNet/IP(CIP)")] |
| | | public class OmronEtherNetCommunicator : BaseCommunicator |
| | |
| | | #endregion Private Member |
| | | |
| | | #region Public Member |
| | | /// <summary> |
| | | /// 日志记录实例对象 |
| | | /// </summary> |
| | | public override ILogNet LogNet => _logNet; |
| | | |
| | | /// <summary> |
| | | /// 设备名称 |
| | | /// </summary> |
| | | public override string Name => _name; |
| | | |
| | | /// <summary> |
| | | /// 获取当前通讯器是否已连接到PLC。 |
| | | /// </summary> |
| | | public override bool IsConnected => _connected; |
| | | |
| | | #endregion Public Member |
| | |
| | | case TypeCode.Char: |
| | | return plc.Write(address, Convert.ToChar(value)); |
| | | default: |
| | | if (value is int[]) |
| | | { |
| | | return plc.Write(address, value as int[]); |
| | | } |
| | | else if (value is uint[]) |
| | | { |
| | | return plc.Write(address, value as uint[]); |
| | | } |
| | | else if (value is short[]) |
| | | { |
| | | return plc.Write(address, value as short[]); |
| | | } |
| | | else if (value is ushort[]) |
| | | { |
| | | return plc.Write(address, value as ushort[]); |
| | | } |
| | | else if (value is bool[]) |
| | | { |
| | | return plc.Write(address, value as bool[]); |
| | | } |
| | | else if (value is float[]) |
| | | { |
| | | return plc.Write(address, value as float[]); |
| | | } |
| | | else if (value is double[]) |
| | | { |
| | | return plc.Write(address, value as double[]); |
| | | } |
| | | else if (value is byte[]) |
| | | { |
| | | return plc.Write(address, value as byte[]); |
| | | } |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | private object Read(string address, TypeCode typeCode) |
| | | private object Read(string address, TypeCode typeCode, ushort length = 1) |
| | | { |
| | | try |
| | | { |
| | | switch (typeCode) |
| | | { |
| | | case TypeCode.Int32: |
| | | if (length > 1) |
| | | return (int)GetContent(plc.ReadInt32(address, length), address); |
| | | return (int)GetContent(plc.ReadInt32(address), address); |
| | | case TypeCode.UInt32: |
| | | if (length > 1) |
| | | return (uint)GetContent(plc.ReadUInt32(address, length), address); |
| | | return (uint)GetContent(plc.ReadUInt32(address), address); |
| | | case TypeCode.Int16: |
| | | if (length > 1) |
| | | return (short)GetContent(plc.ReadInt16(address, length), address); |
| | | return (short)GetContent(plc.ReadInt16(address), address); |
| | | case TypeCode.UInt16: |
| | | if (length > 1) |
| | | return (ushort)GetContent(plc.ReadUInt16(address, length), address); |
| | | return (ushort)GetContent(plc.ReadUInt16(address), address); |
| | | case TypeCode.Single: |
| | | if (length > 1) |
| | | return (float)GetContent(plc.ReadFloat(address, length), address); |
| | | return (float)GetContent(plc.ReadFloat(address), address); |
| | | case TypeCode.Boolean: |
| | | if (length > 1) |
| | | return (bool)GetContent(plc.ReadBool(address, length), address); |
| | | return (bool)GetContent(plc.ReadBool(address), address); |
| | | case TypeCode.Byte: |
| | | if (length > 1) |
| | | return (byte)GetContent(plc.Read(address, length), address); |
| | | return (byte)GetContent(plc.ReadByte(address), address); |
| | | case TypeCode.String: |
| | | if (length > 1) |
| | | return (string)GetContent(plc.ReadString(address, length), address); |
| | | return (string)GetContent(plc.ReadString(address), address); |
| | | case TypeCode.Char: |
| | | if (length > 1) |
| | | return (char)GetContent(plc.Read(address, length), address); |
| | | return (char)GetContent(plc.ReadByte(address), address); |
| | | default: |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError); |
| | |
| | | #endregion |
| | | |
| | | #region Public Method |
| | | /// <summary> |
| | | /// 连接到PLC。 |
| | | /// </summary> |
| | | /// <returns>如果连接成功则返回true,否则返回false。</returns> |
| | | public override bool Connect() |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 断开与工业设备的连接。 |
| | | /// </summary> |
| | | /// <returns>如果成功断开连接则返回true,如果已经是断开状态则返回false。</returns> |
| | | public override bool Disconnect() |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 释放对象资源的接口。 |
| | | /// </summary> |
| | | public override void Dispose() |
| | | { |
| | | _isPing = false; |
| | |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 从PLC读取数据。 |
| | | /// </summary> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="length">要读取的数据长度。</param> |
| | | /// <returns>读取到的数据,如果读取失败则可能返回null或空数组。</returns> |
| | | public override byte[] Read(string address, int length) |
| | | { |
| | | return (byte[])GetContent(plc.Read(address, (ushort)length), address); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 从PLC读取数据。 |
| | | /// </summary> |
| | | /// <typeparam name="T">读取数据的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <returns>读取到的数据,如果读取失败则可能返回null或抛出异常。</returns> |
| | | public override T Read<T>(string address) |
| | | { |
| | | Type type = typeof(T); |
| | | return (T)Read(address, Type.GetTypeCode(type)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 从PLC读取数据。 |
| | | /// </summary> |
| | | /// <typeparam name="T">读取数据的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="length">读取的长度。</param> |
| | | /// <returns>读取到的数据,如果读取失败则可能返回null或抛出异常。</returns> |
| | | public override T[] Read<T>(string address, ushort length) |
| | | { |
| | | Type type = typeof(T); |
| | | return (T[])Read(address, Type.GetTypeCode(type), length); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 从PLC读取数据。 |
| | | /// </summary> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="dataType">数据类型。</param> |
| | | /// <returns>读取到的数据,如果读取失败则可能返回null或抛出异常。</returns> |
| | | public override object ReadAsObj(string address, string dataType) |
| | | { |
| | | return Read(address, SiemensDBDataType.GetTypeCode(dataType)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读取自定义的数据类型,需要继承自IDataTransfer接口,返回一个新的类型的实例对象。 |
| | | /// </summary> |
| | | /// <typeparam name="T">自定义的数据类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <returns>成功返回自定义类型数据,失败抛出异常。</returns> |
| | | public override T ReadCustomer<T>(string address) |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 等待指定地址的泛型类型值为指定的值 |
| | | /// </summary> |
| | | /// <typeparam name="T">指定的值的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="readInterval">读取的频率。</param> |
| | | /// <param name="waitTimeout">等待的超时时间,如果超时时间为-1的话,则是无期限等待。</param> |
| | | /// <param name="value">等待检测的值</param> |
| | | /// <returns>是否等待成功的结果对象,一旦通信失败,或是等待超时就返回失败。否则返回成功,并告知调用方等待了多久。</returns> |
| | | public override OperateResult<TimeSpan> Wait<T>(string address, int readInterval, int waitTimeout, T value) |
| | | { |
| | | TypeCode typeCode = Type.GetTypeCode(typeof(T)); |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 向PLC写入数据。 |
| | | /// </summary> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="data">要写入的数据。</param> |
| | | /// <returns>如果写入成功则返回true,否则抛出异常。</returns> |
| | | public override bool Write(string address, byte[] data) |
| | | { |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 向PLC写入数据。 |
| | | /// </summary> |
| | | /// <typeparam name="T">写入数据的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="value">要写入的数据。</param> |
| | | /// <returns>如果写入成功则返回true,否则抛出异常。</returns> |
| | | public override bool Write<T>(string address, T value) |
| | | { |
| | | return GetResult(Write(address, value), address, value); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 向PLC写入数据。 |
| | | /// </summary> |
| | | /// <typeparam name="T">写入数据的类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="values">要写入的数据。</param> |
| | | /// <returns>如果写入成功则返回true,否则抛出异常。</returns> |
| | | public override bool Write<T>(string address, T[] values) |
| | | { |
| | | return GetResult(Write(address, values), address, values); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 写入自定义类型的数据,该类型必须继承自IDataTransfer接口。 |
| | | /// </summary> |
| | | /// <typeparam name="T">自定义的数据类型泛型。</typeparam> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="value">要写入数据。</param> |
| | | /// <returns>如果写入成功则返回true,失败则抛出异常。</returns> |
| | | public override bool WriteCustomer<T>(string address, [NotNull] T value) |
| | | { |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 向PLC写入数据。 |
| | | /// </summary> |
| | | /// <param name="address">源地址,具体格式取决于使用的工业协议。</param> |
| | | /// <param name="dataType">要写入的数据类型。</param> |
| | | /// <param name="value">要写入的数据。</param> |
| | | /// <returns>如果写入成功则返回true,失败则抛出异常。</returns> |
| | | /// <exception cref="CommunicationException"></exception> |
| | | public override bool WriteObj(string address, string dataType, object value) |
| | | { |
| | | bool obj = false; |