资源简介

西门子PLC使用C#读写PLC数据,采用S7 dll库连接下位,在上位显示相关数据。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Snap7;
using System.Threading;

namespace Snap7_read
{
    public partial class Form1 : Form
    {
        private S7Client client1=new S7Client();
        private S7Client client2 = new S7Client();

        private byte[] DB_A = new byte[256];
        private byte[] DB_B = new byte[256];
        

        delegate void setTextCallback(string text);


        private void SetText(string text)
        {
            if (this.textBox1.InvokeRequired)
            {
                while (!this.textBox1.IsHandleCreated)
                {
                    if (this.textBox1.Disposing || this.textBox1.IsDisposed)
                    {
                        return;
                    }
                }
                setTextCallback d = new setTextCallback(SetText);
                this.textBox1.Invoke(d new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
            }  
        }

        private void SetText1(string text)
        {
            if (this.textBox2.InvokeRequired)
            {
                while (!this.textBox2.IsHandleCreated)
                {
                    if (this.textBox2.Disposing || this.textBox2.IsDisposed)
                    {
                        return;
                    }
                }
                setTextCallback d = new setTextCallback(SetText1);
                this.textBox2.Invoke(d new object[] { text });
            }
            else
            {
                this.textBox2.Text = text;
            }
        }
        private void ShowResult(int Result)
        {
            // This function returns a textual explaination of the error code
           // TextError.Text = Client.ErrorText(Result);
        }

        private string Dump(TextBox Box byte[] Buffer int Size)
        {
            string str = ““;
            // Declaration separated from the code for readability
            int y;
            //Box.Text = ““;
            y = 0;
            for (int c = 0; c < Size; c++)
            {
                String S = Convert.ToString(Buffer[c] 16);
                if (S.Length == 1) S = “0“ + S;
                str = str+ “0x“ + S + “ “;
                y++;
                if (y == 8)
                {
                    y = 0;
                   str = str + (char)13 + (char)10; 
                }
            }
            return str;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {

        }

        private void button2_Click(object sender EventArgs e)
        {
            Thread thread1 = new Thread(() =>
            {

                int result1 = client1.ConnectTo(“192.168.1.110“ 0 1

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    .......      2518  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\.gitattributes

    .......      4305  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\.gitignore

    .......      2518  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\NewRepo\.gitattributes

    .......      4305  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\NewRepo\.gitignore

    .......       184  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\App.config

    .......      5074  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Form1.cs

    .......      4382  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Form1.Designer.cs

    .......      5889  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Form1.resx

    .......       500  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Program.cs

    .......      1282  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties\AssemblyInfo.cs

    .......      2762  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties\Resources.Designer.cs

    .......      5496  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties\Resources.resx

    .......      1067  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties\Settings.Designer.cs

    .......       242  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties\Settings.settings

    .......      3565  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Snap7 read.csproj

    .......    108239  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\snap7.net.cs

    .......      1104  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read.sln

     目录          0  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read\Properties

     目录          0  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\NewRepo

     目录          0  2018-10-29 14:55  Snap7-read-master\Snap7-read-master\Snap7 read

     目录          0  2018-10-29 14:55  Snap7-read-master\Snap7-read-master

     目录          0  2019-06-24 14:52  Snap7-read-master

----------- ---------  ---------- -----  ----

               153432                    22


评论

共有 条评论