锦通 发表于 2024-5-19 06:04:43

C#利用MX Component实现三菱PLC软元件数据收罗的完整步骤(仿真)

媒介
本文介绍了怎样利用三菱提供的MX Component插件实现对三菱PLC软元件数据的读写,记录了利用计算机仿真,模拟PLC,直至完成测试的详细流程,并重点介绍了在这个过程中的易错点,供参考。
 
用到的软件:
1. PLC开发编程环境GX Works2,GX Works2下载链接 https://www.mitsubishielectric-fa.cn/site/file-software-detail?id=18
2.实现计算机与可编程控制器通信的软件工具MX Component,MX Component下载链接 https://www.mitsubishielectric-fa.cn/site/file-software-detail?id=27
 设置流程:1.GX Works2的设置流程:(1)新建工程,选择PLC型号;https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506110639044-646362872.png
 (2)修改PLC参数,PLC文件设置=>利用一下文件=>命名,设置合适的容量大小,扩容为了后期测试时利用(该操作可选);
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506110809661-1596450387.png
  (3)修改软元件设置,利用上一步骤中的扩容操作,三菱PLC设计上位机数据操作区一样平常选择D区,R区和W区亦可(该操作可选);
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506111012230-728016993.png
   (4)调试中选择=>模拟开始,出现Simulator窗口,Mode和RUN绿灯长亮即表示仿真正常;
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506111346119-794753530.png
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506111502436-540694348.png
 
2.MX Component设置流程:
(1)找到MX Component安装的对应软件,选择“Communication Setup Utility”,以管理员身份运行;
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506111854265-2068421749.png
(2)添加Logical Station Number(通道号);(这里以99为例)
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506112524884-1867416757.png
(3)由于本案例采取的是GX Works2仿真方案,选择GX Simulator2,选择CPU型号,下一步即可;
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506112631558-643779713.png
(4)选择描述,,以Test为例;
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506112924970-1508717431.png
(5)设置完成后,举行测试,显示successful表示连接乐成;连接乐成后可以关掉该软件,不影响正常通讯;
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506113016913-1112662846.png
 
3.C#上位机的程序测试:
using ActUtlTypeLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MELSEC.MX.Test
{
    public partial class Form1 : Form
    {
      private ActUtlType m_plc;
      public Form1()
      {
            InitializeComponent();
            m_plc = new ActUtlType();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
            cmb_LogicalStationNumber.Items.Clear();
            for (int i = 1;i<=256;i++)
            {
                cmb_LogicalStationNumber.Items.Add(i.ToString());
            }
      }
      
      private void btn_Comm_Click(object sender, EventArgs e)
      {
            try
            {
                int logNUM = Convert.ToInt16(cmb_LogicalStationNumber.Text);
                m_plc.ActLogicalStationNumber=logNUM;
                m_plc.ActPassword = "";
                if (m_plc.Open() != 0)
                {
                  btn_Comm.BackColor = Color.Gray;
                  btn_Comm.Text = "未建立链接";
                  m_plc.Close();
                  m_plc = null;
                }
                else
                {
                  btn_Comm.BackColor = Color.Green;
                  btn_Comm.Text = "已连接";
                }
            }
            catch (Exception )
            {
                throw;
            }
      }

      private void btn_Read_Click(object sender, EventArgs e)
      {
            try
            {
                Thread thread = new Thread(() =>
                {
                  while (true)
                  {
                        txb_D0.Invoke(new Action(() =>
                        {
                            txb_D0.Text = ReadDeviceValue("D0", 2).ToString();
                            txb_D1.Text = ReadDeviceValue("D0", 2).ToString();
                            txb_R100.Text = ReadDeviceValue("R100", 1).ToString();
                            txb_W500.Text = ReadDeviceValue("W500", 1).ToString();
                        }));
                        Thread.Sleep(300);
                  }
                });
                thread.IsBackground = true;
                thread.Start();
            }
            catch (Exception)
            {

                throw;
            }
      }

      private string[] ReadDeviceValue(string DeviceName,int NumberOfData)
      {
            int iReturnCode;
            short[] arrDeviceValue;
            string szDeviceName;
            string[] arrData= { };
            int iNumberOfData;
            try
            {
                szDeviceName = string.Join("\n", DeviceName);
                iNumberOfData = Convert.ToInt32(NumberOfData);
                arrDeviceValue = new short;
                iReturnCode = m_plc.ReadDeviceBlock2(szDeviceName, iNumberOfData, out arrDeviceValue);
                if (iReturnCode == 0)
                {
                  arrData = new string;
                  for (int i = 0; i < iNumberOfData; i++)
                  {
                        arrData = arrDeviceValue.ToString();
                  }
                  return arrData;
                }
                return arrData;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return arrData;
            }
      }
    }
}<br> Demo演示
https://img2024.cnblogs.com/blog/2586965/202405/2586965-20240506114847321-1501143997.png
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C#利用MX Component实现三菱PLC软元件数据收罗的完整步骤(仿真)