前言
本文介绍了如何使用三菱提供的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
(2)修改PLC参数,PLC文件设置=>使用一下文件=>命名,设置合适的容量大小,扩容为了后期测试时使用(该操作可选);
(3)修改软元件设置,使用上一步骤中的扩容操作,三菱PLC设计上位机数据操作区一般选择D区,R区和W区亦可(该操作可选);
(4)调试中选择=>模拟开始,出现Simulator窗口,Mode和RUN绿灯长亮即表示仿真正常;
2.MX Component配置流程:
(1)找到MX Component安装的对应软件,选择“Communication Setup Utility”,以管理员身份运行;
(2)添加Logical Station Number(通道号);(这里以99为例)
(3)由于本案例采用的是GX Works2仿真方案,选择GX Simulator2,选择CPU型号,下一步即可;
(4)选择描述,,以Test为例;
(5)配置完成后,进行测试,显示successful表示连接成功;连接成功后可以关掉该软件,不影响正常通讯;
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)[0].ToString(); txb_D1.Text = ReadDeviceValue("D0", 2)[1].ToString(); txb_R100.Text = ReadDeviceValue("R100", 1)[0].ToString(); txb_W500.Text = ReadDeviceValue("W500", 1)[0].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[iNumberOfData]; iReturnCode = m_plc.ReadDeviceBlock2(szDeviceName, iNumberOfData, out arrDeviceValue[0]); if (iReturnCode == 0) { arrData = new string[iNumberOfData]; for (int i = 0; i < iNumberOfData; i++) { arrData[i] = arrDeviceValue[i].ToString(); } return arrData; } return arrData; } catch (Exception ex) { MessageBox.Show(ex.Message, Name, MessageBoxButtons.OK, MessageBoxIcon.Error); return arrData; } } } }
Demo演示