91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 0.07M
    文件類型: .rar
    金幣: 1
    下載: 0 次
    發(fā)布日期: 2024-06-19
  • 語(yǔ)言: C#
  • 標(biāo)簽: 西門子??CPU??S7??通訊??C#??

資源簡(jiǎn)介

C#和西門子CPU進(jìn)行S7通訊

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.Threading;
using System.Windows.Forms;
using S7.Net;


namespace S7_Test
{

    public partial class Form1 : Form
    {
        public Plc s7_plc;
        byte[] data = new byte[2];


        public Form1()
        {
            InitializeComponent();
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            s7_plc = new Plc(CpuType.S71500, txt_PLC_IP.Text, 0, 1);

            //開辟一個(gè)后臺(tái)線程
            Task task = Task.Run(() =>
            {
                Communicaton();
            });
        }
        private void Communicaton()
        {
            listBox1.Invoke(new Action(() =>
             listBox1.Items.Add("正在連接到PLC……")
            ));

            s7_plc.Open();
            bool temp1 = true;

            while (true)
            {
                try
                {
                    if (s7_plc.IsConnected & temp1)
                    {
                        temp1 = false;
                        listBox1.Invoke(new Action(() =>
                        listBox1.Items.Add("PLC連接成功!")
                         ));
                    }
                    else
                    {
                        if (!s7_plc.IsConnected)
                        {
                            temp1 = true;
                            listBox1.Invoke(new Action(() =>
                            listBox1.Items.Add("PLC連接中斷!")
                             ));
                        }
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }
                Thread.Sleep(1000);

            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            if (s7_plc.IsConnected == false)
            {
                MessageBox.Show("未連接PLC!", "連接提示", MessageBoxButtons.OK);//檢查PLC是否連接; 
            }
            else
            {
                try
                {
                    string[] arr = (txt_read_addr.Text.ToUpper()).Split('.');//將txt_read_addr文本框中的數(shù)據(jù)轉(zhuǎn)為大寫字母,并用“.”拆分后存放到arr數(shù)組中
                    string valuetype = arr[1].Substring(0, 3);//取數(shù)組中的第二個(gè)元素的前三位,用以確認(rèn)讀取的PLC數(shù)據(jù)類型
                                                              //西門子PLC數(shù)據(jù)類型:DBX(位,bool)DBB(字節(jié),byte)DBW(字,word)DBD(雙字,dword)
                                                              //以下是按不同的數(shù)據(jù)類型,對(duì)PLC數(shù)據(jù)進(jìn)行讀取
                    if (valuetype == "DBX")
                    {
                        bool test1 = (bool)s7_plc.Read(txt_read_addr.Text.ToUpper());
                        ShowMsg(txt_read_addr.Text ":" test1.ToString());
                    }
                    else if (valuetype == "DBB")
                    {
                        //short test2 = ((ushort)s7_plc.Read(txt_read_addr.Text.ToUpper())).ConvertToShort();
                        byte test2 = Convert.ToByte(s7_plc.Read(txt_read_addr.Text.ToUpper()));
                        ShowMsg(txt_read_addr.Text ":" test2.ToString());
                    }

                    else if (valuetype == "DBW")
                    {
                        short test3 = ((ushort)s7_plc.Read(txt_read_addr.Text.ToUpper())).ConvertToShort();
                        ShowMsg(txt_read_addr.Text ":" test3.ToString());
                    }

                    else if (valuetype == "DBD")
                    {
                        //double test5 = ((uint)s7_plc.Read(txt_read_addr.Text.ToUpper())).ConvertToFloat();
                        double test5 = ((uint)s7_plc.Read(txt_read_addr.Text.ToUpper())).ConvertToFloat();
                        ShowMsg(txt_read_addr.Text ":" test5.ToString());
                    }

                    else
                    {
                        MessageBox.Show("請(qǐng)檢查地址是否輸入錯(cuò)誤!", "輸入提示", MessageBoxButtons.OK);
                    }

                    //listBox1.Items.Add("讀取結(jié)果B0:" data[0].ToString());
                    //listBox1.Items.Add("讀取結(jié)果B1:" data[1].ToString());


                }
                catch (Exception ex)
                {
                    MessageBox.Show("請(qǐng)檢查地址是否輸入錯(cuò)誤!", "輸入提示", MessageBoxButtons.OK);
                }
            }
        }

        private void btn_Write_Click(object sender, EventArgs e)
        {
            if (s7_plc.IsConnected == false)
            {
                MessageBox.Show("未連接PLC!", "連接提示", MessageBoxButtons.OK);
            }
            else
            {
                try
                {
                    string[] arr = (txt_write_addr.Text.ToUpper()).Split('.');
                    string valuetype = arr[1].Substring(0, 3);

                    if (valuetype == "DBX")
                    {
                        s7_plc.Write(txt_write_addr.Text.ToUpper(), Convert.ToBoolean(txt_value.Text));
                    }

                    else if (valuetype == "DBB")
                    {
                        var value = byte.Parse(txt_value.Text);
                        s7_plc.Write(txt_write_addr.Text.ToUpper(), value);
                    }

                    else if (valuetype == "DBW")
                    {
                        var value = short.Parse(txt_value.Text);
                        s7_plc.Write(txt_write_addr.Text.ToUpper(), value);
                    }

                    else if (valuetype == "DBD")
                    {
                        //double value = double.Parse(txt_value.Text);
                        Double value = Double.Parse(txt_value.Text);
                        s7_plc.Write(txt_write_addr.Text.ToUpper(), value);
                    }

                    else
                    {
                        MessageBox.Show("請(qǐng)檢查地址是否輸入錯(cuò)誤!", "輸入提示", MessageBoxButtons.OK);
                    }

                }
                catch (Exception Ex)
                {
                    MessageBox.Show("請(qǐng)檢查輸入的“地址”或“值”是否錯(cuò)誤!", "輸入提示", MessageBoxButtons.OK);
                }
            }
        }
        private void btn_Disconnect_Click(object sender, EventArgs e)
        {
            s7_plc.Close();
        }
        private void ShowMsg(string v)
        {
            //txt_result.AppendText(v "\r\n");//將讀取的PLC數(shù)據(jù)追加到“結(jié)果”文本框中
            listBox1.Items.Add("讀取結(jié)果:" v);
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        private void Mouse_down(object sender, EventArgs e)
        {

        }
    }
}

資源截圖

代碼片段和文件信息

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.Threading;
using?System.Windows.Forms;
using?S7.Net;


namespace?S7_Test
{

????public?partial?class?Form1?:?Form
????{
????????public?Plc?s7_plc;
????????byte[]?data?=?new?byte[2];


????????public?Form1()
????????{
????????????InitializeComponent();
????????}

????????private?void?btn_Connect_Click(object?sender?EventArgs?e)
????????{
????????????s7_plc?=?new?Plc(CpuType.S71500?txt_PLC_IP.Text?0?1);

????????????//開辟一個(gè)后臺(tái)線程
????????????Task?task?=?Task.Run(()?=>
????????????{
????????????????Communicaton();
????????????});
????????}
????????private?void?Communicaton()
????????{
????????????listBox1.Invoke(new?Action(()?=>
?????????????listBox1.Items.Add(“正在連接到PLC……“)
????????????));

????????????s7_plc.Open();
????????????bool?temp1?=?true;

????????????while?(true)
????????????{
????????????????try
????????????????{
????????????????????if?(s7_plc.IsConnected?&?temp1)
????????????????????{
????????????????????????temp1?=?false;
????????????????????????listBox1.Invoke(new?Action(()?=>
????????????????????????listBox1.Items.Add(“PLC連接成功!“)
?????????????????????????));
????????????????????}
????????????????????else
????????????????????{
????????????????????????if?(!s7_plc.IsConnected)
????????????????????????{
????????????????????????????temp1?=?true;
????????????????????????????listBox1.Invoke(new?Action(()?=>
????????????????????????????listBox1.Items.Add(“PLC連接中斷!“)
?????????????????????????????));
????????????????????????}
????????????????????}
????????????????}
????????????????catch?(Exception?ex)
????????????????{

????????????????????MessageBox.Show(ex.ToString());
????????????????}
????????????????Thread.Sleep(1000);

????????????}

????????}

????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{

????????}

????????private?void?btn_Read_Click(object?sender?EventArgs?e)
????????{
????????????if?(s7_plc.IsConnected?==?false)
????????????{
????????????????MessageBox.Show(“未連接PLC!“?“連接提示“?MessageBoxButtons.OK);//檢查PLC是否連接;?
????????????}
????????????else
????????????{
????????????????try
????????????????{
????????????????????string[]?arr?=?(txt_read_addr.Text.ToUpper()).Split(‘.‘);//將txt_read_addr文本框中的數(shù)據(jù)轉(zhuǎn)為大寫字母,并用“.”拆分后存放到arr數(shù)組中
????????????????????string?valuetype?=?arr[1].Substring(0?3);//取數(shù)組中的第二個(gè)元素的前三位,用以確認(rèn)讀取的PLC數(shù)據(jù)類型
??????????????????????????????????????????????????????????????//西門子PLC數(shù)據(jù)類型:DBX(位,bool)DBB(字節(jié)byte)DBW(字word)DBD(雙字dword)
??????????????????????????????????????????????????????????????//以下是按不同的數(shù)據(jù)類型,對(duì)PLC數(shù)據(jù)進(jìn)行讀取
????????????????????if?(valuetype?==?“DBX“)
????????????????????{
????????????????????????bool?test1?=?(bool)s7_plc.Read(txt_read_addr.Text.ToUpper());
????????????????????????ShowMsg(txt_read_addr.Text?+

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----

?????文件????????189??2020-11-14?15:38??S7_Test\App.config

?????文件??????80384??2020-09-04?18:55??S7_Test\bin\Debug\S7.Net.dll

?????文件??????14848??2021-03-12?11:32??S7_Test\bin\Debug\S7_Test.exe

?????文件????????189??2020-11-14?15:38??S7_Test\bin\Debug\S7_Test.exe.config

?????文件??????40448??2021-03-12?11:32??S7_Test\bin\Debug\S7_Test.pdb

?????文件???????7380??2021-03-12?11:32??S7_Test\Form1.cs

?????文件??????12981??2020-11-14?22:59??S7_Test\Form1.Designer.cs

?????文件???????5817??2020-11-14?22:58??S7_Test\Form1.resx

?????文件????????214??2020-11-14?15:39??S7_Test\obj\Debug\.NETframeworkVersion=v4.7.2.AssemblyAttributes.cs

?????文件???????1435??2020-11-14?21:45??S7_Test\obj\Debug\DesignTimeResolveAssemblyReferences.cache

?????文件???????7373??2020-11-14?15:50??S7_Test\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

?????文件??????????0??2021-03-12?11:32??S7_Test\obj\Debug\S7_Test.csproj.CopyComplete

?????文件?????????42??2020-12-19?14:40??S7_Test\obj\Debug\S7_Test.csproj.CoreCompileInputs.cache

?????文件???????2164??2020-12-19?15:00??S7_Test\obj\Debug\S7_Test.csproj.FileListAbsolute.txt

?????文件???????1012??2020-11-14?23:00??S7_Test\obj\Debug\S7_Test.csproj.GenerateResource.cache

?????文件???????4560??2021-03-12?11:32??S7_Test\obj\Debug\S7_Test.csprojAssemblyReference.cache

?????文件??????14848??2021-03-12?11:32??S7_Test\obj\Debug\S7_Test.exe

?????文件????????180??2020-11-14?23:00??S7_Test\obj\Debug\S7_Test.Form1.resources

?????文件??????40448??2021-03-12?11:32??S7_Test\obj\Debug\S7_Test.pdb

?????文件????????180??2020-11-14?15:51??S7_Test\obj\Debug\S7_Test.Properties.Resources.resources

?????文件????????137??2020-11-14?15:50??S7_Test\packages.config

?????文件????????519??2020-11-14?15:39??S7_Test\Program.cs

?????文件???????1306??2020-11-14?15:39??S7_Test\Properties\AssemblyInfo.cs

?????文件???????2846??2020-11-14?15:39??S7_Test\Properties\Resources.Designer.cs

?????文件???????5612??2020-11-14?15:38??S7_Test\Properties\Resources.resx

?????文件???????1092??2020-11-14?15:39??S7_Test\Properties\Settings.Designer.cs

?????文件????????249??2020-11-14?15:38??S7_Test\Properties\Settings.settings

?????文件???????3867??2020-11-14?15:50??S7_Test\S7_Test.csproj

?????目錄??????????0??2020-11-14?15:39??S7_Test\obj\Debug\TempPE

?????目錄??????????0??2020-12-19?14:39??S7_Test\bin\Debug

............此處省略8個(gè)文件信息

評(píng)論

共有 條評(píng)論