资讯详情

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

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

  • 背景
  • 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下载

标签: allenbradley继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台