技术论坛

 C#用S7.NET与SMART通信测试

返回主题列表
作者 主题
sangshunyang
侠圣

经验值:4092
发帖数:565
精华帖:4
楼主    2018-11-28 18:47:02
主题:C#用S7.NET与SMART通信测试 精华帖  精编帖 

S7.NET开源库的质料说明支持的PLC是:S7-200,S7-1200,S7-1500,S7-300    ,S7-400

好像不支持SMART ,

经测试C#使用S7.NET可以与SMART通信。将图片及源码分享出来供大家参考,初学C#代码有点乱。

几点注意事项:

1,与SMART通信时选择PLC类型 选的是S7-1200,机架号是 0,插槽号是 1

2,读写 V 区数据使用的是DB1






源程序代码:

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using S7.Net;


namespace WindowsFormsApplication4

{

    public partial class Form1 : Form


    {

        bool status=false;

        public Form1()

        {

            InitializeComponent();

        }

        Plc plc1 = new Plc(CpuType.S71200, "192.168.1.10", 0, 1);


        private void Form1_Load(object sender, EventArgs e)

        {


        }


        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                plc1.OpenAsync();

                status = true;

                toolStripStatusLabel2.Text ="正在连接" + plc1.IP ;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }


        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            foreach (var readdata in ReadMultipleBytes(13, 1, 0))

            {

                try

                {

                    textBox1.Text = textBox1.Text + readdata+" ";

                }

                catch

                {

                    ;

                }

            }  ;


         

        }


        private void timer1_Tick(object sender, EventArgs e)

        {

            if (status)

            {

                if (plc1.IsConnected)

                {

                    toolStripStatusLabel2.ForeColor = Color.Black;

                    toolStripStatusLabel2.Text = plc1.IP + " 已成功连接 ";

                }

                else

                {

                    toolStripStatusLabel2.ForeColor = Color.Red;

                    toolStripStatusLabel2.Text = plc1.IP + " 连接连接断开";

                }

            }

        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)

        {



        }

        private List<byte> ReadMultipleBytes(int numBytes, int db, int startByteAdr = 5)

        {

            List<byte> resultBytes = new List<byte>();

            int index = startByteAdr;

            while (numBytes > 0)

            {

                var maxToRead = (int)Math.Min(numBytes, 200);

                byte[] bytes = plc1.ReadBytes(DataType.DataBlock, db, index, (int)maxToRead);

                if (bytes == null)

                    return new List<byte>();

                resultBytes.AddRange(bytes);

                numBytes -= maxToRead;

                index += maxToRead;

            }

            return resultBytes;

        }


        private void timer2_Tick(object sender, EventArgs e)

        {

             try

            {

                byte[] bytes = plc1.ReadBytes(DataType.DataBlock, 1, 0, 1);

                if (bytes == null)

                {

                    status = false;

                    plc1.OpenAsync();

                    toolStripStatusLabel2.Text = "正在连接" + plc1.IP;

                        

                }

                else

                    status = true;

            }

            catch (Exception ex)

            {

               MessageBox.Show(ex.Message);

            }

        }


        private void button3_Click(object sender, EventArgs e)

        {

            plc1.Close();

        }

    }

}

附件:

WindowsFormsApplication4.rar

s7netplus-develop.zip


sangshunyang
侠圣

经验值:4092
发帖数:565
精华帖:4
8楼    2018-11-29 18:45:04
精编帖  主题:回复:C#用S7.NET与SMART通信测试

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using S7.Net;                      //与西门子PLC建立连接的开源代码库,引用该命名空间才能与西门子PLC通信,该库只能使用以太网进行通信。开源社区中可以找到该库,VS中联网就可以搜索到


namespace WindowsFormsApplication4

{

    public partial class Form1 : Form


    {

        bool status=false;

        Plc plc1 = new Plc(CpuType.S71200, "192.168.1.10", 0, 1);     //定义一个连接,每个参数的具体意义参考手册,这里要说明的是 SMART PLC连接时选用的是( CpuType.S71200,“网络地址”  0 , 1)网络地址用字符串表示


        public Form1()

        {

            InitializeComponent();                                      //初始化窗体控件,VS系统自动生成的代码,用户无需关心。

        }

        //连接到PLC

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                plc1.OpenAsync();                   //异步连接到PLC ,这里也可以用plc1.Open()方法,但连接状态无法及时显示,"正在连接"

                status = true;                       //一旦执行了连接命令就开始判断连接的状态

                toolStripStatusLabel2.Text ="正在连接" + plc1.IP ;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);      //连接出错用对话框显示出错的信息

            }

        }


        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";                                      //清空文本框的内容

            foreach (var readdata in ReadMultipleBytes(13, 1, 0))    

            {

                    textBox1.Text = textBox1.Text + readdata+" ";    //把读到的数据用循环的方法依次从缓冲区中读出,并显示在文本框内

            }  ;   

        }

        //连接建立后实时更新连接的状态,即改变状态栏的文字信息, plc1.IP 是连接PLC的网络地址

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (status)

            {

                if (plc1.IsConnected)

                {

                    toolStripStatusLabel2.ForeColor = Color.Black;                      //已经建立连接改变显示状态的文字颜色为黑色

                    toolStripStatusLabel2.Text = plc1.IP + " 已成功连接 ";

                }

                else

                {

                    toolStripStatusLabel2.ForeColor = Color.Red;                       //连接断开改变显示状态的文字颜色为红色

                    toolStripStatusLabel2.Text = plc1.IP + " 连接连接断开";

                }

            }

        }

        //读取数据的方法

        private List<byte> ReadMultipleBytes(int numBytes, int db, int startByteAdr = 5)

        {

            List<byte> resultBytes = new List<byte>(); //建立一个LIst对象用于存储读取的数据

            int index = startByteAdr;

            while (numBytes > 0)                       //如果设定读取的数据大于0个字节,虽然这里是用的循环,实际上这里和IF语句相同

            {

                var maxToRead = (int)Math.Min(numBytes, 200);    //如果设定读取的数据大于200个字节,取最大200个字节,小于200时取设定值

                byte[] bytes = plc1.ReadBytes(DataType.DataBlock, db, index, (int)maxToRead);   //从PLC中读取数据

                if (bytes == null)                     //如果没有读到数据,就返回一个空的List的对象,即一个空的集合

                    return new List<byte>();

                resultBytes.AddRange(bytes);           //设置记录的大小

                numBytes -= maxToRead;                 //让numBytes归零

                index += maxToRead;

            }

            return resultBytes;                          //读到数据返回读到数据的一个集合

        }

        //以下方法是实时检测是否与PLC通信是否保持,该方法测试时出现问题,实际上都没有使用,把timer2 的Enable属性设置成false了

        private void timer2_Tick(object sender, EventArgs e)

        {

             try

            {

                byte[] bytes = plc1.ReadBytes(DataType.DataBlock, 1, 0, 1);//读取vb0的数据。当然也可读取其他数据区的数据,改变DataType即可。具体还得看手册

                if (bytes == null)                                         //如果没有读到数据,认为连接已经断开

                {

                    status = false;

                    plc1.OpenAsync();                                       //异步建立与PLC的连接

                    toolStripStatusLabel2.Text = "正在连接" + plc1.IP;                     

                }

                else

                    status = true;                                         //读到数据让一个状态置 1,说明连接正常

            }

            catch (Exception ex)

            {

               MessageBox.Show(ex.Message);                                 //建立通信连接出现异常反馈异常信息

            }

        }


        //断开PLC的连接

        private void button3_Click(object sender, EventArgs e)

        {

            plc1.Close();                                                 //断开连接

        }

    }

}

找S7.NET的方法:

第一步

第二步

    余下步骤用户自己就很容易做了不在赘述。


S7.NET的使用文档:

S7.NET.pdf


您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。