苏州实操派科技有限公司官网-实操派教育科技,汇川技术PLC培训,实操派PLC培训,苏州PLC培训机构

汇川 modbus tcp modbus通讯类

[复制链接]
4 139

附件下载,需登录可以查看贴内更多信息

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/// <summary>
        /// 和PLC通信异常时循环次数的最大值
        /// </summary>
        const int CycleMax = 4;
        private enum SoftElemType
        {
            //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,        //
            REGI_H3U_R = 0x2c        //
        }
        #region //标准库
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;Init_ETH_String&quot;, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Init_ETH_String(string sIpAddr, int nNetId = 0, int IpPort = 502);

        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;Exit_ETH&quot;, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Exit_ETH(int nNetId = 0);

        /******************************************************************************
         1.功能描述 : 写H3u软元件
         2.返 回 值 :1 成功  0 失败
         3.参    数 : nNetId:网络链接编号
                      eType:软元件类型  
                          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
                      nStartAddr:软元件起始地址
                      nCount:软元件个数
                      pValue:数据缓存区
         4.注意事项 : 1.x和y元件地址需为8进制; 2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
        ******************************************************************************/
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem_Int16&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem_Int32&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem_UInt16&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem_UInt32&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Write_Soft_Elem_Float&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);

        /******************************************************************************
        1.功能描述 : 读H3u软元件
        2.返 回 值 :1 成功  0 失败
        3.参    数 : nNetId:网络链接编号
                     eType:软元件类型  
                         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
                     nStartAddr:软元件起始地址
                     nCount:软元件个数
                     pValue:返回数据缓存区
        4.注意事项 : 1.x和y元件地址需为8进制;
                     2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
                     3.如果是读位元件,每个位元件的值存储在一个字节中,pValue数据缓存区字节数必须是8的整数倍
       ******************************************************************************/



        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem_Int16&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem_Int32&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem_UInt16&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem_UInt32&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
        [DllImport(&quot;StandardModbusApi.dll&quot;, EntryPoint = &quot;H3u_Read_Soft_Elem_Float&quot;, CallingConvention = CallingConvention.Cdecl)]
        private static extern int H3u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
        #endregion


        /// <summary>
        /// 读取32位D
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;></param>
        /// <param name=&quot;Dx&quot;></param>
        public static bool readDoubleD(int nStratAddr,out int Dx)
        {
            int[] pValue = new int[1];
            pValue[0] = 0;
            int read_d = 0;
            int nRet = 0;
            do
            {
                nRet = H3u_Read_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, 2, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                Dx = pValue[0];
                return true;
            }
            else
            {
                Dx = 0;
                return false;
            }

        }


        /// <summary>
        /// 读出多个依32位格式的D寄存器
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>地址</param>
        /// <param name=&quot;Dx&quot;>返回的数据</param>
        /// <param name=&quot;num&quot;>读取的个数</param>
        /// <returns></returns>
        public static bool readDoubleD(int nStratAddr, out int[] Dx, int num = 1)
        {
            int read_d = 0;
            int nRet = 0;
            Dx = new int[num];
            int[] pValue = new int[num];
            for (int i = 0; i < Dx.Length; i++)
            {
                Dx = 0;
                pValue = 0;
            }
            do
            {
                nRet = H3u_Read_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, num*2, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Dx.Length; i++)
                {
                    Dx = pValue;
                }
                return true;
            }
            else
            {
                for (int i = 0; i < Dx.Length; i++)
                {
                    Dx = 0;
                }
                return false;
            }
        }







        /// <summary>
        /// 读出多个依16位格式的D寄存器
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>地址</param>
        /// <param name=&quot;Dx&quot;>返回的数据</param>
        /// <param name=&quot;num&quot;>读取的个数</param>
        /// <returns></returns>
        public static bool ReadD(int nStratAddr, out int[] Dx, int num = 1)
        {            
            int read_d = 0;
            int nRet = 0;
            Dx = new int[num];
            short[] pValue = new short[num];
            for (int i = 0; i < Dx.Length; i++)
            {
                Dx= 0;
                pValue = 0;
            }
            do
            {
                nRet = H3u_Read_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Dx.Length; i++)
                {
                    Dx = pValue;                    
                }               
                return true;
            }
            else
            {
                for (int i = 0; i < Dx.Length; i++)
                {
                    Dx = 0;
                }
                return false;
            }
        }



        /// <summary>
        /// 依16位的格式读取D寄存器
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>地址</param>
        /// <param name=&quot;Dx&quot;>返回的数据</param>
        /// <returns></returns>
        public static bool ReadD(int nStratAddr,out int Dx)
        {
            short[] pValue = new short[1];
            pValue[0] = 0;
            int read_d = 0;
            int nRet=0;           
            do
            {
                nRet = H3u_Read_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet==0 && read_d<=CycleMax);
            if (read_d<=CycleMax)
            {
                Dx = pValue[0];
                return true;
            }
            else
            {
                Dx = 0;
                return false;
            }            
        }

        /// <summary>
        /// 读取1个或多个X点
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始位</param>
        /// <param name=&quot;Xm&quot;>X点</param>
        /// <param name=&quot;num&quot;>数量</param>
        /// <returns> 返回值 =1时正常</returns>
        public static bool readX(int nStratAddr, out byte[] Xm,int num=1)//读取X点
        {
            int read_d = 0;
            int nRet = 0;
            Xm = new byte[num];
            byte[] pValue = new byte[num];
            for (int i = 0; i < Xm.Length; i++)
            {
                Xm = 0;
                pValue = 0;
            }
            do
            {               
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Xm.Length; i++)
                {
                    Xm = pValue;
                }
                return true;
            }
            else
            {
                for (int i = 0; i < Xm.Length; i++)
                {
                    Xm = 0;
                }
                return false;
            }

        }

        /// <summary>
        /// 读取单个X点
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;></param>
        /// <param name=&quot;Xm&quot;></param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readX(int nStratAddr,out int Xm)//读取X点
        {
            byte[] pValue = new byte[1];
            pValue[0] = 0;
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                Xm = pValue[0];
                return true;
            }
            else
            {
                Xm = 0;
                return false;
            }
        }



        /// <summary>
        /// 读取1个或多个M点
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;Mx&quot;>返回的变量</param>
        /// <param name=&quot;num&quot;>读取的个数默认为1</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readM(int nStratAddr, out byte[] Mx,int num=1)//读取M点
        {
            int read_d = 0;
            int nRet = 0;
            Mx = new byte[num];
            byte[] pValue = new byte[num];
            for (int i = 0; i < Mx.Length; i++)
            {
                Mx = 0;
                pValue = 0;
            }
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Mx.Length; i++)
                {
                    Mx = pValue;
                }
                return true;
            }
            else
            {
                for (int i = 0; i < Mx.Length; i++)
                {
                    Mx = 0;
                }
                return false;
            }
        }



        /// <summary>
        /// 读取单个X点
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;></param>
        /// <param name=&quot;Mx&quot;></param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readM(int nStratAddr, out int Mx)//读取M点
        {
            byte[] pValue = new byte[1];
            pValue[0] = 0;
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                Mx = pValue[0];
                return true;
            }
            else
            {
                Mx = 0;
                return false;
            }
        }


        /// <summary>
        /// 读1个或多个S
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;Sx&quot;>返回的数据</param>
        /// <param name=&quot;num&quot;>数量</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readS(int nStratAddr, out byte[] Sx,int num=1)//读取S点
        {
            int nRet = 0;
            int read_d = 0;
            Sx = new byte[num];
            byte[] pValue = new byte[num];
            for (int i = 0; i < Sx.Length; i++)
            {
                Sx = 0;
                pValue = 0;
            }
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_S, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Sx.Length; i++)
                {
                    Sx = pValue;
                }
                return true;
            }
            else
            {
                for (int i = 0; i < Sx.Length; i++)
                {
                    Sx = 0;
                }
                return false;
            }
        }

        /// <summary>
        /// 读取单个S点
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;Sx&quot;>返回的数据</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readS(int nStratAddr, out int Sx)//读取S点
        {
            byte[] pValue = new byte[1];
            pValue[0] = 0;
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_S, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                Sx = pValue[0];
                return true;
            }
            else
            {
                Sx = 0;
                return false;
            }
        }

        /// <summary>
        /// 读取1个或多个Y
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;Yx&quot;>返回的数据</param>
        /// <param name=&quot;num&quot;>数量</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readY(int nStratAddr, out byte[] Yx,int num=1)//读取Y点
        {
            int nRet = 0;
            int read_d = 0;
            Yx = new byte[num];
            byte[] pValue = new byte[num];
            for (int i = 0; i < Yx.Length; i++)
            {
                Yx = 0;
                pValue = 0;
            }
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                for (int i = 0; i < Yx.Length; i++)
                {
                    Yx = pValue;
                }
                return true;
            }
            else
            {
                for (int i = 0; i < Yx.Length; i++)
                {
                    Yx = 0;
                }
                return false;
            }
        }


        /// <summary>
        /// 读取单个Y
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;Yx&quot;>返回的数据</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool readY(int nStratAddr, out int Yx)//读取Y点
        {
            byte[] pValue = new byte[1];
            pValue[0] = 0;
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax)
            {
                Yx = pValue[0];
                return true;
            }
            else
            {
                Yx = 0;
                return false;
            }
        }


        //public static void readFloat(int nStratAddr, out float Dx)//读取16位D
        //{
        //    float[] pValue = new float[1];
        //    pValue[0] = 0;
        //    int nRet = 0;
        //    nRet= H3u_Read_Soft_Elem_Float(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);         
        //    if (nRet == 1)
        //    {
        //        Dx = pValue[0];
        //    }
        //    else
        //    {
        //        Dx = pValue[0];
        //        MessageBox.Show(&quot;与PLC的通信出错,出错代码005&quot;);
        //    }
        //}


        public static bool writeDoubleD(int nStratAddr,int Dx)//写入32位D
        {
            int[] pValue = new int[1];
            pValue[0] = Dx;
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, 2, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet==1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public static bool writeD(int nStratAddr, short Dx)//写入16位D
        {
            short[] pValue = new short[1];
            pValue[0] = Dx;
            int nRet = 0;            
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 将值写入到M
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;On_Off&quot;>ON或OFF</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool writeM(int nStratAddr, bool On_Off)//写入M
        {
            byte[] pValue = new byte[1];
            if (On_Off ==true)
            {
                pValue[0] = 1;
            }
            else
            {
                pValue[0] = 0;
            }
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 写入1个多个M
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;pValue&quot;>需要写入的数组</param>
        /// <param name=&quot;num&quot;>需要写入的数量</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool writeM(int nStratAddr, byte[] pValue, int num = 1)
        {
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }




        /// <summary>
        /// 将值写入到X
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;On_Off&quot;>ON或OFF</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool writeX(int nStratAddr, bool On_Off)//写入X
        {
            byte[] pValue = new byte[1];
            if (On_Off == true)
            {
                pValue[0] = 1;
            }
            else
            {
                pValue[0] = 0;
            }
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }




        /// <summary>
        /// 将值写入到Y
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;On_Off&quot;>ON或OFF</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool writeY(int nStratAddr, bool On_Off)//写入Y
        {
            byte[] pValue = new byte[1];
            if (On_Off == true)
            {
                pValue[0] = 1;
            }
            else
            {
                pValue[0] = 0;
            }
            int nRet = 0;               
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, 1, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 写入1个多个Y
        /// </summary>
        /// <param name=&quot;nStratAddr&quot;>起始地址</param>
        /// <param name=&quot;pValue&quot;>需要写入的数组</param>
        /// <param name=&quot;num&quot;>需要写入的数量</param>
        /// <returns>返回值 =1时正常</returns>
        public static bool writeY(int nStratAddr, byte[] pValue, int num=1)//写入Y
        {
            int nRet = 0;
            short read_d = 0;
            do
            {
                nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, num, pValue);
                read_d++;
            } while (nRet == 0 && read_d <= CycleMax);
            if (read_d <= CycleMax || nRet == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



汇川Plc.rar (2.91 KB, 下载次数: 0)
免责声明:内容为网友自行发布或者来自互联网资源或者参考文献,如果侵犯了您的权益,请联系站长 1304546267@qq.com,我们会及时删除侵权内容,感谢您的理解!
回复

使用道具 举报

罗明之星 2022-5-17 09:34:12 | 显示全部楼层
感谢分享
回复

使用道具 举报

ChatGPT机器人83 2022-5-17 09:56:59 | 显示全部楼层
感谢分享
回复

使用道具 举报

历史的遗忘cc 2022-6-13 06:16:50 | 显示全部楼层
感谢分享
回复

使用道具 举报

关键词:汇川,modbus tcp,modbus通讯类,循环次数,异常,PLC,软元件,网络链接编号,软元件类型,起始地址,个数,数据缓存区

汇川是一种PLC(可编程逻辑控制器)品牌,在工业自动化领域广泛应用。而Modbus TCP是一种常用的通信协议,用于在PLC和其他设备之间进行数据传输。在汇川PLC中,有一个专门用于Modbus通讯的类。

在使用汇川PLC进行通信时,有时会出现异常情况。为了解决这些异常情况,可以设置一个循环次数的最大值。当通信出现异常时,程序会循环尝试进行通信,直到达到设定的次数。

在汇川PLC中,有不同类型的软元件,如Y元件、X元件、S元件、M元件等等。每种软元件都有一个对应的定义。通过设置软元件的类型、起始地址、个数和数据缓存区,可以对软元件进行读写操作。

在代码中,有一些关于Modbus通讯的函数,如Init_ETH_String函数用于初始化网络链接,Exit_ETH函数用于关闭网络链接。另外还有一个写H3u软元件的函数,通过指定网络链接编号、软元件类型、起始地址、个数和数据缓存区,可以写入数据到对应的软元件中。

需要注意的是,对于X和Y元件的地址,必须使用8进制表示。同时,在处理C元件双字寄存器时,每个寄存器需要占用4个字节的数据。

总之,通过这些函数和方法,可以实现汇川PLC和其他设备之间的Modbus通讯,并进行数据的读写操作。通过设置循环次数的最大值,可以处理通信出现的异常情况。同时,不同类型的软元件可以通过指定对应的类型、起始地址、个数和数据缓存区,进行读写操作。
                                                                                                                                                                                                                                                 以上内容来自  AI机器人,如需继续对话,则在此回答下“点击回复” 或者 在提问时  “@机器人”!禁止 "黄赌毒及政治敏感",违者封号,严重者 提交 网警公安部门。本站不对内容真实性负责,AI机器人有时候会乱说一通……
回复

使用道具 举报

data/attachment/temp/202212/30/133743h8cd9z14sjs84kaz.png_thumb.jpg 共30章节544课时

【全科班】汇川中型PLC从0到1就业班(Codesys系统化全科班课程-600节+)-汇川PLC教程

朱子文

¥ 4699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/VMdEZiqp3BMkbBIlMRRD.png 共9章节44课时

【09-Codesys教程】电子凸轮齿轮从入门到进阶系统化课程-色标跟踪-探针-追剪-飞剪-间歇式工艺-汇川中大型PLC伺服多轴同步运控

朱子文

¥ 699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240217/v39r666Z663eBBGKErRm.png 共31章节529课时

【全科班】汇川技术H5U从0到1就业班(系统化全科班-600节+)

张汉清

¥ 4199.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240207/HlL5SWHry9lPVdqC8i59.png 共5章节63课时

【01-Codesys教程】ST文本从0到1,从入门到进阶-汇川中大型PLC教程

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/Fu5pu34pPcTpu0La443P.png 共6章节52课时

【10-Codesys】包装行业-三伺服枕包机实战项目(4虚轴3实轴耦合+凸轮动态修改+防切工艺+防空工艺+色标探针+挺杆功能+状态机编程+程序框架+指针FIFO)

朱子文

¥ 899.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240227/AMo32PcbJeIMJBT3m8Pu.png 共4章节36课时

03 中大型ST文本结合仿真软件小项目 31节-气缸功能块封装-状态机编程思路-小设备框架

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/Su1ibr3I77yl78DLBR7D.png 共4章节29课时

【12-Codesys小课】中大型PLC-PID控制器算法从入门到进阶之液位控制系统

朱子文

¥ 699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20230603/Cz3hqDmDD8qHfT3MDm4m.png 共3章节30课时

【05-Codesys教程】汇川中大型PLC单轴运动控制教程-汇川PLC教程

朱子文

¥ 399.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20230603/gXpEpJ8Qb78eD5lDqlnQ.png 共3章节25课时

【06-Codesys教程】汇川中大型PLC运动控制进阶篇(多轴PTP)-编程框架与编程模板实操

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/e39s63ppg54Q2s5H33H9.png 共3章节24课时

【08-codesys教程】汇川中大型codesys教程-多轴运控扩展篇经验分享小知识分享

朱子文

¥ 299.00
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

data/attachment/temp/202212/30/133743h8cd9z14sjs84kaz.png_thumb.jpg 共30章节544课时

【全科班】汇川中型PLC从0到1就业班(Codesys系统化全科班课程-600节+)-汇川PLC教程

朱子文

¥ 4699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/VMdEZiqp3BMkbBIlMRRD.png 共9章节44课时

【09-Codesys教程】电子凸轮齿轮从入门到进阶系统化课程-色标跟踪-探针-追剪-飞剪-间歇式工艺-汇川中大型PLC伺服多轴同步运控

朱子文

¥ 699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240217/v39r666Z663eBBGKErRm.png 共31章节529课时

【全科班】汇川技术H5U从0到1就业班(系统化全科班-600节+)

张汉清

¥ 4199.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240207/HlL5SWHry9lPVdqC8i59.png 共5章节63课时

【01-Codesys教程】ST文本从0到1,从入门到进阶-汇川中大型PLC教程

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/Fu5pu34pPcTpu0La443P.png 共6章节52课时

【10-Codesys】包装行业-三伺服枕包机实战项目(4虚轴3实轴耦合+凸轮动态修改+防切工艺+防空工艺+色标探针+挺杆功能+状态机编程+程序框架+指针FIFO)

朱子文

¥ 899.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240227/AMo32PcbJeIMJBT3m8Pu.png 共4章节36课时

03 中大型ST文本结合仿真软件小项目 31节-气缸功能块封装-状态机编程思路-小设备框架

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/Su1ibr3I77yl78DLBR7D.png 共4章节29课时

【12-Codesys小课】中大型PLC-PID控制器算法从入门到进阶之液位控制系统

朱子文

¥ 699.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20230603/Cz3hqDmDD8qHfT3MDm4m.png 共3章节30课时

【05-Codesys教程】汇川中大型PLC单轴运动控制教程-汇川PLC教程

朱子文

¥ 399.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20230603/gXpEpJ8Qb78eD5lDqlnQ.png 共3章节25课时

【06-Codesys教程】汇川中大型PLC运动控制进阶篇(多轴PTP)-编程框架与编程模板实操

朱子文

¥ 499.00
https://shicaopai-video-files.oss-cn-hangzhou.aliyuncs.com/keke_video_base/image/20240206/e39s63ppg54Q2s5H33H9.png 共3章节24课时

【08-codesys教程】汇川中大型codesys教程-多轴运控扩展篇经验分享小知识分享

朱子文

¥ 299.00

到"π³ 实操派",学PLC就是快!汇川PLC及Codesys培训

项目实战 手把手教学 微信&电话 15850116008
超600节系统化课程,汇川优秀自动化讲师亲授!学一门Codesys,玩800家PLC仅剩少量名额
点击免费试听

到实操派,学PLC就是快!

扫码领取工控文库VIP

钻石VIP先到先得

服务电话 & 微信:9:00-22:00

15850116008

苏州实操派科技有限公司

公司地址:江苏省苏州市吴中区越溪街道天鹅荡路5号31B13

Copyright © 苏州实操派科技有限公司 2022 到实操派,学PLC就是快!工业自动化业务培训适用人群:成人 ( 苏ICP备2022021576号-3 )

公司地址:江苏省苏州市吴中区越溪街道天鹅荡路5号31B13

返回顶部