C# 三菱PLC FX5U 通信

需要引用这三个库:

实例:

 //添加PLC控件
                axActUtlType = new AxActUtlType();
                Controls.Add(this.axActUtlType);
                //打开PLC
                string s = MitsubishiHelper.Open(ref axActUtlType, 1);

 

/// <summary>
    /// 三菱
    /// </summary>
    public class MitsubishiHelper
    {
        
        public static AxActUtlType _axActUtlType = null;
       
       
        
        #region 开启PLC
        /// <summary>
        /// 开启PLC
        /// </summary>
        /// <param name="axActUtlType"> AxActUtlType</param>
        /// <param name="_StationNo"></param>
        /// <param name="_Password">密码</param>
        /// <returns></returns>
        public static string  Open(ref AxActUtlType axActUtlType, int _StationNo = 0, string _Password = "")
        {
            try
            {
                //((Form)form).Controls.Add(axActUtlType);
                _axActUtlType = axActUtlType;
                //int iStation = Convert.ToInt32(_StationNo);
                axActUtlType.ActLogicalStationNumber = _StationNo;
                _axActUtlType.ActPassword = _Password;
                int rtn = _axActUtlType.Open();
                if (rtn == 0)
                {
                    return "PLC连接成功";
                }
                else
                {
                    return "PLC连接失败";
                }
            }
            catch (Exception )
            {
                return "PLC连接失败";
            }
        }
        #endregion
        #region 读取数据
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="szDevice"></param>
        /// <param name="lSize"></param>
        /// <returns></returns>
        public static string ReadData(string szDevice, string lSize="2")
        {
            try
            {
                int num = Convert.ToInt32(lSize);
                short[] arr = new short[num];
                int rtn = _axActUtlType.ReadDeviceBlock2(szDevice, num, out arr[0]);
                string s = "";
                if (rtn == 0)
                {
                    for (int i = arr.Length - 1; i >= 0; i--)
                    {
                        s += string.Format("{0:X4}", arr[i]);                       
                    }
                    return (Convert.ToInt32(s, 16).ToString());
                }
                else
                {
                    return null;
                }
            }
            catch (Exception )
            {
                return null;
            }
        }
        #endregion
        #region PLC高低位
        /// <summary>
        /// PLC高低位
        /// </summary>
        /// <param name="twoWord"></param>
        /// <param name="highWord"></param>
        /// <param name="lowWord"></param>
        public static void hi_lo(string twoWord, out string highWord, out string lowWord)
        {
            if (twoWord.Contains("-"))
            {

                int low = int.Parse(twoWord) % 65536;

                int high = -1;
                lowWord = low.ToString();
                highWord = high.ToString();
            }
            else
            {
                int low = int.Parse(twoWord) % 65536;

                int high = int.Parse(twoWord) / 65536;
                lowWord = low.ToString();
                highWord = high.ToString();
            }

        }
        #endregion
        #region 写入 
        /// <summary>
        /// 将数据写入PLC
        /// </summary>
        /// <param name="szDevice">地区</param>
        /// <param name="strData">数据</param>
        /// <returns></returns>
        public static string WriteData(string szDevice, string[] strData) {
            try
            {
                int num = strData.Length;
                int[] arr = new int[num];
                for (int i = 0; i < num; i++)
                {
                    arr[i] = Convert.ToInt32(strData[i]);
                }
               // int rtn = _axActUtlType.WriteDeviceBlock2(szDevice, num, ref arr[0]);
                int rtn = _axActUtlType.WriteDeviceRandom(szDevice, num, ref arr[0]);
                if (rtn == 0)
                {
                    //this.txtNum.Text = num.ToString();
                    return (num.ToString()) + "成功写入";
                }
                else
                {
                    return "写入数据失败";
                }

            }
            catch (Exception )
            {
                return "写入数据失败";
            }
        }
        #endregion
        #region 关闭
        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns></returns>
        public static bool Close()
        {
            return _axActUtlType.Close() == 0;
        }
        #endregion 
        #region 读bit
        /// <summary>
        /// 读bit
        /// </summary>
        /// <param name="szDevice">地区</param>
        /// <returns></returns>
        public static int GetBit(string szDevice)
        {
            int lpsData;
           
            try
            {
                _axActUtlType.GetDevice(szDevice, out lpsData);
            }
            catch (Exception)
            {

                return 0;
            }
            return lpsData;
        }
        #endregion 
        #region 写bit
        /// <summary>
        /// 写bit
        /// </summary>
        /// <param name="szDevice">地区</param>
        /// <param name="sData">数据</param>
        /// <returns></returns>
        public static bool SetBit(string szDevice, short sData)
        {
            try
            {
                _axActUtlType.SetDevice2(szDevice, sData);
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
        #endregion 
    }

 

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值