资源简介

C#与三菱PLC通讯源码 串口通讯,可读取X、Y、M、D等寄存器,可批量读取

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.Profinet.Melsec;

namespace PLC_TestDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            splitContainer1.Panel2.Enabled = false;
            btnBreakConn.Enabled = false;
        }

        #region 建立通讯连接
        MelsecMcNet melsec = new MelsecMcNet();
        //建立通讯连接
        private void btnConnect_Click(object sender EventArgs e)
        {
            melsec.IpAddress = txtIPAddr.Text.Trim();
            melsec.Port = Convert.ToInt32(txtPort.Text.Trim());
            try
            {
                OperateResult connectStatus = melsec.ConnectServer();
                if (connectStatus.IsSuccess)
                {
                    MessageBox.Show(“连接成功“);
                    splitContainer1.Panel2.Enabled = true;
                    btnBreakConn.Enabled = true;
                    btnConnect.Enabled = false;
                }
                else
                {
                    MessageBox.Show(“连接失败“);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //断开连接
        private void btnBreakConn_Click(object sender EventArgs e)
        {
            try
            {
                melsec.ConnectClose();
                splitContainer1.Panel2.Enabled = false;
                btnBreakConn.Enabled = false;
                btnConnect.Enabled = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion 

        #region 读取三菱PLC数据
        private void btnReadX_Buttonclick(object Sender)
        {
            txtResultView.Text = melsec.ReadBool(txtReadX.Text.Trim() 1).Content[0] .ToString();
        }

        private void btnReadY_Buttonclick(object Sender)
        {
            txtResultView.Text = melsec.ReadBool(txtReadY.Text.Trim() 1).Content[0].ToString();
        }

        private void btnReadM_Buttonclick(object Sender)
        {
            txtResultView.Text = melsec.ReadBool(txtReadM.Text.Trim()).Content.ToString();
        }

        private void btnReadD_Buttonclick(object Sender)
        {
            txtResultView.Text = melsec.ReadInt16(txtReadD.Text.Trim()).Content.ToString();
            
        }
        #endregion

        #region 写入三菱PLC数据
        private void btnWriteY_Buttonclick(object Sender)
        {
            bool writebool = false ;
            if (txtWriteView .Text .Trim ()==“1“|| txtWriteView.Text.Trim() ==“true“)
            {
                writebool = true;
            }
            if (txtWriteView.Text.Trim() == “0

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     55808  2019-12-31 09:24  MitsubishiCommunication\.vs\MitsubishiCommunication\v14\.suo

    ..A..H.     58880  2019-12-19 20:32  MitsubishiCommunication\MitsubishiCommunication\.vs\MitsubishiCommunication\v14\.suo

     文件    1824768  2019-09-09 09:22  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\DSControls.dll

     文件     692736  2019-06-25 21:59  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\HslCommunication.dll

     文件    1194671  2019-06-25 21:59  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\HslCommunication.xml

     文件      76288  2019-12-04 16:15  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.exe

     文件      32256  2019-12-04 16:15  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.pdb

     文件      22688  2019-12-31 09:21  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.vshost.exe

     文件        490  2015-06-04 12:48  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.vshost.exe.manifest

     文件     488360  2019-04-22 01:06  MitsubishiCommunication\MitsubishiCommunication\bin\Debug\Newtonsoft.Json.dll

     文件       5088  2019-12-31 09:24  MitsubishiCommunication\MitsubishiCommunication\FrmMain.cs

     文件      54836  2019-12-04 16:37  MitsubishiCommunication\MitsubishiCommunication\FrmMain.Designer.cs

     文件      81379  2019-12-04 16:37  MitsubishiCommunication\MitsubishiCommunication\FrmMain.resx

     文件    1824768  2019-09-09 09:22  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\DSControls.dll

     文件     692736  2019-06-25 21:59  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\HslCommunication.dll

     文件    1194671  2019-06-25 21:59  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\HslCommunication.xml

     文件      76288  2019-12-04 16:15  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.exe

     文件      32256  2019-12-04 16:15  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.pdb

     文件      22688  2019-12-19 20:28  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.vshost.exe

     文件        490  2015-06-04 12:48  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\MitsubishiCommunication.vshost.exe.manifest

     文件     488360  2019-04-22 01:06  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\bin\Debug\Newtonsoft.Json.dll

     文件       5094  2019-12-19 20:31  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\FrmMain.cs

     文件      54836  2019-12-04 16:37  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\FrmMain.Designer.cs

     文件      81379  2019-12-04 16:37  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\FrmMain.resx

     文件       4132  2019-12-04 10:28  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication.csproj

     文件        228  2019-12-04 09:48  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication.csproj.user

     文件       3699  2019-12-15 11:58  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7265  2019-12-04 10:31  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1362  2019-12-19 20:28  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\obj\Debug\MitsubishiCommunication.csproj.FileListAbsolute.txt

     文件       1014  2019-12-04 16:15  MitsubishiCommunication\MitsubishiCommunication\MitsubishiCommunication\obj\Debug\MitsubishiCommunication.csproj.GenerateResource.Cache

............此处省略62个文件信息

评论

共有 条评论