PLC通讯实现-C#实现AB-CIP以太网通讯

本文档介绍了如何使用C#语言实现与Allen Bradley PLC的CIP以太网通讯,通过Wongoing.Plc.Communication.dll库进行读写操作,包括连接、断开、读取和写入不同数据类型标签的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PLC通讯实现-C#实现AB-CIP以太网通讯

背景

本人近十年的工作都与工业软件相关、其中工控系统开发过程中有一个必要环节就是跟各大厂商的PLC进行通讯,而对于从互联网行业跨入工业互联网行业的从业人员来说要实现各型号PLC通讯还是需要一个过程的,本人在此对主流型号PLC通讯实现进行总结以便大家参考。

AB-CIP以太网通讯实现

1、开发语言
开发语言为C#
2、通讯库封装
所有通讯的关键代码封装到Wongoing.Plc.Communication.dll中
3、在项目引用Wongoing.Plc.Communication.dll
4、实现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;
using Wongoing.Plc.Communication;

namespace WinTestApp
{
    public partial class Form1 : Form
    {
        private Wongoing.Plc.Communication.Profinet.AllenBradley.AllenBradleyNet equip = new Wongoing.Plc.Communication.Profinet.AllenBradley.AllenBradleyNet("192.168.4.200");
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OperateResult result = this.equip.ConnectServer();
            if (result.IsSuccess)
            {
                Console.WriteLine("连接成功!");
            }
            else
            {
                Console.WriteLine("连接失败!");
                Console.WriteLine(result.ToMessageShowString());
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            #region uint16

            //string tagName = "UpCmd_M[0]";
            //OperateResult<UInt16[]> result = this.equip.ReadUInt16(tagName, 1);
            //if (result.IsSuccess)
            //{
            //    foreach (UInt16 value in result.Content)
            //    {
            //        Console.WriteLine(value);
            //    }
            //    Console.WriteLine();
            //}
            //else
            //{
            //    Console.WriteLine("读取失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region int16

            string tagName = "UpCmd_M[0]";

            OperateResult<Int16[]> result = this.equip.ReadInt16(tagName, 5);
            if (result.IsSuccess)
            {
                foreach (Int16 value in result.Content)
                {
                    Console.Write(value + "\t");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("读取失败!");
                Console.WriteLine(result.ToMessageShowString());
            }

            #endregion

            #region UInt32

            //string tagName = "EM_Back_Stitcher_SFC_A[0]";
            //OperateResult<UInt32[]> result = this.equip.ReadUInt32(tagName, 1);
            //if (result.IsSuccess)
            //{
            //    foreach(UInt32 value in result.Content)
            //    {
            //        Console.WriteLine(value);
            //    }
            //    Console.WriteLine();
            //}
            //else
            //{
            //    Console.WriteLine("读取失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region int32

            //string tagName = "Operating_Status_HMI_S";
            //OperateResult<Int32[]> result = this.equip.ReadInt32(tagName, 4);
            //if (result.IsSuccess)
            //{
            //    foreach(Int32 value in result.Content)
            //    {
            //        Console.WriteLine(value + "\t");
            //    }
            //    Console.WriteLine();
            //}
            //else
            //{
            //    Console.WriteLine("读取失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region bool

            //string tagName = "EM_Back_Stitcher_SFC_A[0]";

            //OperateResult<bool[]> result = this.equip.ReadBoolArray(tagName);
            //if (result.IsSuccess)
            //{
            //    foreach (bool value in result.Content)
            //    {
            //        Console.WriteLine(value);
            //    }
            //    //Console.WriteLine(result.Content);
            //}
            //else
            //{
            //    Console.WriteLine("读取失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region string

            //string tagName = "Recipe_D"; 

            //OperateResult<string> result = this.equip.ReadString(tagName);
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine(result.Content);
            //}
            //else
            //{
            //    Console.WriteLine("读取失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion


            #region byte

            //for(int i = 0; i < 50; i++)
            //{
            //    string tagName = String.Format("NL_Stitching_Auto_Action[{0}]", i);
            //    //OperateResult<SFC_ACTION> result = this.equip.ReadCustomer<SFC_ACTION>(tagName);
            //    //if (result.IsSuccess)
            //    //{
            //    //    Console.WriteLine(result.Content); 
            //    //}
            //    //else
            //    //{
            //    //    Console.WriteLine("读取失败!");
            //    //    Console.WriteLine(result.ToMessageShowString());
            //    //}


            //    OperateResult<byte[]> result = this.equip.Read(tagName, 1);
            //    if (result.IsSuccess)
            //    {
            //        //Console.WriteLine(result.Content);

            //        foreach (byte value in result.Content)
            //        {
            //            Console.Write(value + "\t");
            //        }
            //        Console.WriteLine();
            //        Console.WriteLine("-----------------------------------------------------");
            //        Console.WriteLine( i + " Length = " + result.Content.Length);
            //    }
            //    else
            //    {
            //        Console.WriteLine("读取失败!");
            //        Console.WriteLine(result.ToMessageShowString());
            //    }
            //}
            

            #endregion
        }

        private void button3_Click(object sender, EventArgs e)
        {
            #region uint16

            //string tagName = "UpCmd_M[0]";
            //OperateResult result = this.equip.Write(tagName, new UInt16[] { 1 });
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region int16

            string tagName = "UpCmd_M[0]";
            OperateResult result = this.equip.Write(tagName, new Int16[] { 6, 7, 8, 9, 10 });
            if (result.IsSuccess)
            {
                Console.WriteLine("写入成功!");
            }
            else
            {
                Console.WriteLine("写入失败!");
                Console.WriteLine(result.ToMessageShowString());
            }

            #endregion

            #region Int32

            //string tagName = "EM_Back_Stitcher_SFC_A[0]";
            //OperateResult result = this.equip.Write(tagName, new int[] { 1 });
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region UInt32

            //string tagName = "EM_Back_Stitcher_SFC_A";
            //OperateResult result = this.equip.Write(tagName, new Int32[] { 1, 192 });
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region bool

            //string tagName = "DB200";
            //OperateResult result = this.equip.Write(tagName, false);
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            //string tagName = "EM_Back_Stitcher_SFC_A[0].0";
            //OperateResult result = this.equip.Write(tagName, true);
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion

            #region string

            //string tagName = "Recipe_D";

            //OperateResult result = this.equip.Write(tagName, "67001259P");
            //if (result.IsSuccess)
            //{
            //    Console.WriteLine("写入成功!");
            //}
            //else
            //{
            //    Console.WriteLine("写入失败!");
            //    Console.WriteLine(result.ToMessageShowString());
            //}

            #endregion
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OperateResult result = this.equip.ConnectClose();
            if (result.IsSuccess)
            {
                Console.WriteLine("关闭成功");
            }
            else
            {
                Console.WriteLine("关闭失败");
                Console.WriteLine(result.ToMessageShowString());
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            byte[] content = { 0, 0, 32, 0 };
            Int32 value = BitConverter.ToInt32(content, 0);
            Console.WriteLine(value);
        }
    }
}

引用库下载

Wongoing.Plc.Communication.dll下载

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值