C# 通过网络和三菱的PLC通信

C# 通过网络和三菱的PLC通信

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;
 
namespace PLCTest2
{
    public partial class Form1 : Form
    {
        private AxActUtlTypeLib.AxActUtlType axActUtlType = null;
        public Form1()
        {
            InitializeComponent();
            this.axActUtlType = new AxActUtlTypeLib.AxActUtlType();
            //((System.ComponentModel.ISupportInitialize)(this.axActUtlType)).BeginInit();
            this.Controls.Add(this.axActUtlType);       //貌似这个控件必须要加到Controls中,不然就会引发异常。
        }
 
        private void btnConn_Click(object sender, EventArgs e)
        {
            try
            {
                int iStation = Convert.ToInt32(this.txtStationNo.Text.Trim());
                this.axActUtlType.ActLogicalStationNumber = iStation;
                this.axActUtlType.ActPassword = this.txtPassword.Text.Trim();
                int rtn = this.axActUtlType.Open();
                if (rtn == 0)
                {
                    ShowMsg("连接成功!");
                    this.txtStationNo.Enabled = false;
                    this.txtPassword.Enabled = false;
                    this.btnConn.Enabled = false;
                }
                else
                {
                    ShowMsg("连接失败");
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }
 
 
 
        private void ShowMsg(string msg)
        {
            string str = string.Format(DateTime.Now.ToString("HH:mm:ss")   "_"   msg);
            this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
            this.richTextBox1.SelectedText  = (str   Environment.NewLine);
            this.richTextBox1.ScrollToCaret();
        }
 
        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                string strAddr = this.txtAddr.Text.Trim();
                int num = Convert.ToInt32(this.txtNum.Text.Trim());
                short[] arr = new short[num];
 
                int rtn = this.axActUtlType.ReadDeviceBlock2(strAddr, num, out arr[0]);
                if (rtn == 0)
                {
                    ShowMsg("读取数据成功!");
                    for (int i = 0; i < arr.Length; i  )
                    {
                        ShowMsg(string.Format("{0:X4}", arr[i]));
                    }
                }
                else
                {
                    ShowMsg("读取数据失败");
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }
 
        private void btnWrite_Click(object sender, EventArgs e)
        {
            try
            {
                string strAddr = this.txtAddr.Text.Trim();
                string[] strData = this.txtData.Lines;
                int num = strData.Length;
                short[] arr = new short[num];
                for (int i = 0; i < num; i  )
                {
                    arr[i] = Convert.ToInt16(strData[i]);
                }
 
                int rtn = this.axActUtlType.WriteDeviceBlock2(strAddr, num, ref arr[0]);
                if (rtn == 0)
                {
                    ShowMsg("写入数据成功!");
                    this.txtNum.Text = num.ToString();
                }
                else
                {
                    ShowMsg("写入数据失败");
                }
 
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }
    }
}

 

标签: C# 网络 c 通信 

 

 

  • 1
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值