电工学习网

 找回密码
 立即注册
查看: 5737|回复: 0

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

[复制链接]
发表于 2018-11-29 12:39:16 | 显示全部楼层 |阅读模式
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
6367902766983719232109136.JPG
6367902768500877514240938.JPG

6367902770552387499238824.JPG




源程序代码:
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 ReadMultipleBytes(int numBytes, int db, int startByteAdr = 5)
        {
            List resultBytes = new List();
            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();
                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();
        }
    }
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

电工学习网 ( )

GMT+8, 2024-3-29 18:44

Powered by © 2011-2022 www.diangon.com 版权所有 免责声明 不良信息举报

技术驱动未来! 电工学习网—专业电工基础知识电工技术学习网站。

栏目导航: 工控家园 | 三菱plc | 西门子plc | 欧姆龙plc | plc视频教程

快速回复 返回顶部 返回列表