|
|
@@ -0,0 +1,1204 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Toolkit.Log4;
|
|
|
+
|
|
|
+namespace Toolkit.PLc
|
|
|
+{
|
|
|
+ public enum SoftElemType
|
|
|
+ {
|
|
|
+ //AM600
|
|
|
+ ELEM_QX = 0, //QX元件
|
|
|
+ ELEM_MW = 1, //MW元件
|
|
|
+ ELEM_X = 2, //X元件(对应QX200~QX300) 输入
|
|
|
+ ELEM_Y = 3, //Y元件(对应QX300~QX400) 输出
|
|
|
+ //H5u
|
|
|
+ REGI_H5U_Y = 0x30, //Y元件的定义
|
|
|
+ REGI_H5U_X = 0x31, //X元件的定义
|
|
|
+ REGI_H5U_S = 0x32, //S元件的定义
|
|
|
+ REGI_H5U_M = 0x33, //M元件的定义
|
|
|
+ REGI_H5U_B = 0x34, //B元件的定义
|
|
|
+ REGI_H5U_D = 0x35, //D字元件的定义
|
|
|
+ REGI_H5U_R = 0x36, //R字元件的定义
|
|
|
+ //H3u
|
|
|
+ REGI_H3U_Y = 0x20, //Y元件的定义
|
|
|
+ REGI_H3U_X = 0x21, //X元件的定义
|
|
|
+ REGI_H3U_S = 0x22, //S元件的定义
|
|
|
+ REGI_H3U_M = 0x23, //M元件的定义
|
|
|
+ REGI_H3U_TB = 0x24, //T位元件的定义
|
|
|
+ REGI_H3U_TW = 0x25, //T字元件的定义
|
|
|
+ REGI_H3U_CB = 0x26, //C位元件的定义
|
|
|
+ REGI_H3U_CW = 0x27, //C字元件的定义
|
|
|
+ REGI_H3U_DW = 0x28, //D字元件的定义
|
|
|
+ REGI_H3U_CW2 = 0x29, //C双字元件的定义
|
|
|
+ REGI_H3U_SM = 0x2a, //SM
|
|
|
+ REGI_H3U_SD = 0x2b, //SD
|
|
|
+ REGI_H3U_R = 0x2c //SD
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public enum DataType
|
|
|
+ {
|
|
|
+ BYTE = 0,
|
|
|
+ Short = 1,
|
|
|
+ Ushort = 2,
|
|
|
+ Float = 3,
|
|
|
+ Long = 4,
|
|
|
+ Ulong = 5,
|
|
|
+ }
|
|
|
+ public enum PLCType
|
|
|
+ {
|
|
|
+ H3u = 0,
|
|
|
+ H5u = 1,
|
|
|
+ Am600 = 2,
|
|
|
+ }
|
|
|
+ public enum SoftType
|
|
|
+ {
|
|
|
+ QX = 0,
|
|
|
+ MW = 1,
|
|
|
+ X = 2,
|
|
|
+ Y = 3,
|
|
|
+ S = 4,
|
|
|
+ M = 5,
|
|
|
+ B = 6,
|
|
|
+ D = 7,
|
|
|
+ R = 8,
|
|
|
+ TB = 9,
|
|
|
+ TW = 10,
|
|
|
+ CB = 11,
|
|
|
+ CW = 12,
|
|
|
+ DW = 13,
|
|
|
+ CW2 = 14,
|
|
|
+ SM = 15,
|
|
|
+ SD = 16,
|
|
|
+ }
|
|
|
+ public class PlcConn
|
|
|
+ {
|
|
|
+ #region //标准库
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Init_ETH_String", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern bool Init_ETH_String(string sIpAddr, int nNetId = 0, int IpPort = 502);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Exit_ETH", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern bool Exit_ETH(int nNetId = 0);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * H5U
|
|
|
+ */
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H5u_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Am600
|
|
|
+ */
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int Am600_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, long[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
|
|
|
+ public static extern int H3u_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, ulong[] pValue, int nNetId = 0);
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 私有字段
|
|
|
+ /// <summary>
|
|
|
+ /// 网络链接编号,用于标记是第几条网络链接,取值范围0~255
|
|
|
+ /// </summary>
|
|
|
+ private int netId;
|
|
|
+ /// <summary>
|
|
|
+ /// 端口,默认502
|
|
|
+ /// </summary>
|
|
|
+ private int ipPort = 502;
|
|
|
+ /// <summary>
|
|
|
+ /// PLC的Ip地址
|
|
|
+ /// </summary>
|
|
|
+ private string ipAddr;
|
|
|
+ /// <summary>
|
|
|
+ /// PLC类型
|
|
|
+ /// </summary>
|
|
|
+ private PLCType pLCType = PLCType.Am600;
|
|
|
+ /// <summary>
|
|
|
+ /// plc心跳地址类型
|
|
|
+ /// </summary>
|
|
|
+ private SoftType heartbeatSoftType;
|
|
|
+ /// <summary>
|
|
|
+ /// plc心跳地址
|
|
|
+ /// </summary>
|
|
|
+ private int heartbeatAddress;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// plc心跳频率
|
|
|
+ /// </summary>
|
|
|
+ private int heartbeatSleep = 1000;
|
|
|
+
|
|
|
+ private int numberOfErrors = 0;
|
|
|
+
|
|
|
+ private Thread heartbeat = null;
|
|
|
+
|
|
|
+ private bool writeseize = false;
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 网络链接编号,用于标记是第几条网络链接,取值范围0~255
|
|
|
+ /// </summary>
|
|
|
+ public int NetId { get { return netId; } set { netId = value; } }
|
|
|
+ /// <summary>
|
|
|
+ /// 端口,默认502
|
|
|
+ /// </summary>
|
|
|
+ public int IpPort { get { return ipPort; } set { ipPort = value; } }
|
|
|
+ /// <summary>
|
|
|
+ /// PLC的Ip地址
|
|
|
+ /// </summary>
|
|
|
+ public string IpAddr { get { return ipAddr; } set { ipAddr = value; } }
|
|
|
+ /// <summary>
|
|
|
+ /// PLC CPU类型
|
|
|
+ /// </summary>
|
|
|
+ private PLCType MyPLCType { get { return pLCType; } set { pLCType = value; } }
|
|
|
+ public bool IsConned { get; set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 读取写入累计错误次数
|
|
|
+ /// </summary>
|
|
|
+ public int NumberOfErrors { get => numberOfErrors; set => numberOfErrors = value; }
|
|
|
+
|
|
|
+ public static readonly object synchronousLock = new object();
|
|
|
+
|
|
|
+ public bool heartbeatState = true;
|
|
|
+
|
|
|
+ #region 内部私有方法 工具
|
|
|
+ /// <summary>
|
|
|
+ /// 大小写字符转SoftElemType
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="a"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static SoftElemType ToSoftET(string a)
|
|
|
+ {
|
|
|
+ if (a.ToUpper().Equals("R"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_R;
|
|
|
+ }
|
|
|
+ else if (a.ToUpper().Equals("B"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_B;
|
|
|
+ }
|
|
|
+ else if (a.ToUpper().Equals("X"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_X;
|
|
|
+ }
|
|
|
+ else if (a.ToUpper().Equals("Y"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_Y;
|
|
|
+ }
|
|
|
+ else if (a.ToUpper().Equals("M"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_M;
|
|
|
+ }
|
|
|
+ else if (a.ToUpper().Equals("S"))
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_S;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_D;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// h5u和h3u地址转换
|
|
|
+ /// </summary>
|
|
|
+ private SoftElemType SoftTypeToSoftElemType(SoftType softType)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.Am600)
|
|
|
+ {
|
|
|
+ if (softType == SoftType.QX)
|
|
|
+ {
|
|
|
+ return SoftElemType.ELEM_QX;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.MW)
|
|
|
+ {
|
|
|
+ return SoftElemType.ELEM_MW;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.X)
|
|
|
+ {
|
|
|
+ return SoftElemType.ELEM_X;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.Y)
|
|
|
+ {
|
|
|
+ return SoftElemType.ELEM_Y;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception("Am600软元件中不包含" + softType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ if (softType == SoftType.X)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_X;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.Y)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_Y;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.S)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_S;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.M)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_M;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.B)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_B;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.D)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_D;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.R)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H5U_R;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception("H5u软元件中不包含" + softType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ if (softType == SoftType.X)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_X;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.Y)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_Y;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.S)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_S;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.M)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_M;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.TB)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_TB;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.TW)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_TW;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.CB)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_CB;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.CW)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_CW;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.DW)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_DW;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.CW2)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_CW2;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.SM)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_SM;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.SD)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_SD;
|
|
|
+ }
|
|
|
+ else if (softType == SoftType.R)
|
|
|
+ {
|
|
|
+ return SoftElemType.REGI_H3U_R;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception("H3u软元件中不包含" + softType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception("未指定PLCType");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Writefirst()
|
|
|
+ {
|
|
|
+ int jishu = 3;
|
|
|
+ while (jishu-- != 0)
|
|
|
+ {
|
|
|
+ if (!writeseize)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Thread.Sleep(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="netId">网络链接编号,用于标记是第几条网络链接,取值范围0~255</param>
|
|
|
+ /// <param name="ipPort">端口,默认502</param>
|
|
|
+ /// <param name="ipAddr">PLC的Ip地址</param>
|
|
|
+ /// <param name="pLCType">PLC CPU类型</param>
|
|
|
+ /// <param name="heartbeatSoftType">PLC心跳类型</param>
|
|
|
+ /// <param name="heartbeatAddress">PLC心跳地址</param>
|
|
|
+ public PlcConn(int netId, int ipPort, string ipAddr, PLCType pLCType, SoftType heartbeatSoftType, int heartbeatAddress, int heartbeatSleep)
|
|
|
+ {
|
|
|
+ this.netId = netId;
|
|
|
+ this.ipPort = ipPort;
|
|
|
+ this.ipAddr = ipAddr;
|
|
|
+ this.pLCType = pLCType;
|
|
|
+ this.heartbeatSoftType = heartbeatSoftType;
|
|
|
+ this.heartbeatAddress = heartbeatAddress;
|
|
|
+ this.heartbeatSleep = heartbeatSleep;
|
|
|
+
|
|
|
+ }
|
|
|
+ public bool Open(int index)
|
|
|
+ {
|
|
|
+ bool i = Init_ETH_String(ipAddr, index, ipPort);
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 连接PLC
|
|
|
+ /// </summary>
|
|
|
+ public bool Open()
|
|
|
+ {
|
|
|
+ bool i = Init_ETH_String(ipAddr, netId, ipPort);
|
|
|
+ if (i)
|
|
|
+ {
|
|
|
+ if (heartbeat != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ heartbeat.Abort();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ heartbeat = new Thread(new ThreadStart(Heartbeat));
|
|
|
+ heartbeat.Name = "plc心跳";
|
|
|
+ heartbeat.IsBackground = true;
|
|
|
+ //inoutThread.IsBackground = true;
|
|
|
+ heartbeat.Start();
|
|
|
+ //isonThread.Start();
|
|
|
+ // inoutThread.Start();
|
|
|
+ IsConned = true;
|
|
|
+ }
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool isRunning = true;
|
|
|
+ byte[] ISON = new byte[1];
|
|
|
+ private void Heartbeat()
|
|
|
+ {
|
|
|
+ int heartbeat = 0;
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!isRunning)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(heartbeatSleep);
|
|
|
+ heartbeat = heartbeat == 0 ? 1 : 0;
|
|
|
+ if (WritePlc(heartbeatSoftType, heartbeatAddress, 1, new long[1] { heartbeat }) == 0)
|
|
|
+ {
|
|
|
+ throw new Exception("PLC心跳写入失败");
|
|
|
+ }
|
|
|
+ heartbeatState = heartbeat == 0 ? true : false;
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.error.Error("", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void isHear()
|
|
|
+ {
|
|
|
+
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!isRunning)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(1000);
|
|
|
+ Read(SoftType.B, 4020, 1, ref ISON);
|
|
|
+ if (ISON[0] == 0)
|
|
|
+ {
|
|
|
+ if (WritePlc(SoftType.B, 4020, 1, new byte[1] { 1 }) == 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ throw new Exception("上位机写入失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (WritePlc(SoftType.B, 4020, 1, new byte[1] { 0 }) == 0)
|
|
|
+ {
|
|
|
+
|
|
|
+ throw new Exception("上位机写入失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.error.Error("", ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ byte[] isrest = new byte[1];
|
|
|
+ byte[] its_T = new byte[2];
|
|
|
+ byte[] its = new byte[2];
|
|
|
+
|
|
|
+ short[] temp = new short[] { 0 };
|
|
|
+
|
|
|
+ bool isout = true;
|
|
|
+ bool isin = true;
|
|
|
+ bool isin_T = true;
|
|
|
+ byte[] sciOut = new byte[3];
|
|
|
+ bool[] sciOk = new bool[] { true, true, true };
|
|
|
+ int numscount = 0;
|
|
|
+ bool isout_T = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void Close(int index)
|
|
|
+ {
|
|
|
+ Exit_ETH(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Close()
|
|
|
+ {
|
|
|
+ if (IsConned)
|
|
|
+ {
|
|
|
+ Exit_ETH(netId);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ heartbeat.Abort();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ IsConned = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, byte[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!IsConned)
|
|
|
+ return 0;
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ numberOfErrors++;
|
|
|
+ else
|
|
|
+ numberOfErrors = 0;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, short[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ {
|
|
|
+ numberOfErrors++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, ushort[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ {
|
|
|
+ numberOfErrors++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, float[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ {
|
|
|
+ numberOfErrors++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, long[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ {
|
|
|
+ numberOfErrors++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int WritePlc(SoftType softType, int nStartAddr, int nCount, ulong[] value)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (nCount < value.Length)
|
|
|
+ {
|
|
|
+ nCount = value.Length;
|
|
|
+ }
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ int result = 0, surplus = 3;
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ while (surplus-- != 0)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Write_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Write_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Write_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ if (result == 1)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ Thread.Sleep(50);
|
|
|
+ }
|
|
|
+ if (result == 0)
|
|
|
+ {
|
|
|
+ numberOfErrors++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref byte[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length < nCount)
|
|
|
+ value = new byte[nCount];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref short[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length < nCount)
|
|
|
+ value = new short[nCount];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem_Int16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref ushort[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length < nCount)
|
|
|
+ value = new ushort[nCount];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem_UInt16(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref long[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length < nCount)
|
|
|
+ value = new long[nCount];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem_Int32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref ulong[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length < nCount)
|
|
|
+ value = new ulong[nCount];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem_UInt32(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// nCount=value.Length*2
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="softType"></param>
|
|
|
+ /// <param name="nStartAddr"></param>
|
|
|
+ /// <param name="nCount"></param>
|
|
|
+ /// <param name="value"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public int Read(SoftType softType, int nStartAddr, int nCount, ref float[] value)
|
|
|
+ {
|
|
|
+ int result = 0;
|
|
|
+ if (nCount <= 0) return result;
|
|
|
+ if (value == null || value.Length * 2 < nCount)
|
|
|
+ value = new float[nCount / 2 + nCount % 2];
|
|
|
+ SoftElemType softElemType = SoftTypeToSoftElemType(softType);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Writefirst();
|
|
|
+ lock (synchronousLock)
|
|
|
+ {
|
|
|
+ if (pLCType == PLCType.H3u)
|
|
|
+ {
|
|
|
+ result = H3u_Read_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else if (pLCType == PLCType.H5u)
|
|
|
+ {
|
|
|
+ result = H5u_Read_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = Am600_Read_Soft_Elem_Float(softElemType, nStartAddr, nCount, value, netId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 写操作数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Operationcategory"></param>
|
|
|
+ /// <param name="Operands"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public int WriteOperands(int Operationcategory = -1, int Operands = -1)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!IsConned)
|
|
|
+ {
|
|
|
+ throw new Exception("请先连接PLC");
|
|
|
+ }
|
|
|
+ int i;
|
|
|
+ if (Operationcategory != -1 && Operands != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[4] { (short)Operationcategory, 0, (short)Operands, 0 };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 10, 4, value);
|
|
|
+
|
|
|
+ }
|
|
|
+ else if (Operationcategory != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[1] { (short)Operationcategory };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 10, 1, value);
|
|
|
+ }
|
|
|
+ else if (Operands != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[1] { (short)Operands };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 12, 1, value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ i = 0;
|
|
|
+ }
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.error.Error("", ex);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Thread.Sleep(100);
|
|
|
+ short[] value = new short[1] { 0 };
|
|
|
+ WritePlc(SoftType.D, 12, 1, value);
|
|
|
+ writeseize = false;
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int WriteOperand(int Operationcategory = -1, int Operands = -1)
|
|
|
+ {
|
|
|
+ int i = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (!IsConned)
|
|
|
+ {
|
|
|
+ throw new Exception("请先连接PLC");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Operationcategory != -1 && Operands != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[4] { (short)Operationcategory, 0, (short)Operands, 0 };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 10, 4, value);
|
|
|
+ }
|
|
|
+ else if (Operationcategory != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[1] { (short)Operationcategory };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 10, 1, value);
|
|
|
+ }
|
|
|
+ else if (Operands != -1)
|
|
|
+ {
|
|
|
+ short[] value = new short[1] { (short)Operands };
|
|
|
+ writeseize = true;
|
|
|
+ i = WritePlc(SoftType.D, 12, 1, value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ i = 0;
|
|
|
+ }
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LogHelper.error.Error("", ex);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ //if (i == 0)
|
|
|
+ // MessageBox.Show("写入操作数" + Operands + "失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|