C#与200Smart 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.Windows.Forms;
using S7.Net;
using System.Threading;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Net;

namespace S71
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void skr()
    {
        while (true)
        {
            byte[] bb = new byte[1024];
            int a = PLC.Sk.Receive(bb);
            //    string str = BitConverter.ToString(bb,0,a);
            string str = System.Text.Encoding.Default.GetString(bb,0,a);
            textBox1.AppendText(str+"\r\n");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        PLC.PLc = new Plc(CpuType.S71200,"192.168.0.10",0,1);
        PLC.PLc.Open();
        textBox1.AppendText("PLC已经成功连接\r\n");


        PLC.Sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
        IPAddress Ip = IPAddress.Parse("192.168.0.10");
        int Port = 2000;
        IPEndPoint pop = new IPEndPoint(Ip,Port);
        PLC.Sk.Connect(pop);
        textBox1.AppendText("socket 连接完成\r\n");

// 线程生成
Thread th = new Thread(skr);
th.IsBackground = true;
th.Start();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.0",true);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.1", true);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.2", true);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.0", false);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.1", false);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        PLC.PLc.Write("M0.2", false);
    }

    private void button7_Click(object sender, EventArgs e)
    {
        ushort A = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW0"));
        textBox2.Text = A.ToString();

        ushort B = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW2"));
        textBox3.Text = B.ToString();

        ushort C = Convert.ToUInt16(PLC.PLc.Read("DB1.DBW4"));
        textBox4.Text =C.ToString();
        textBox5.Text=((uint)PLC.PLc.Read("DB1.DBD6")).ConvertToFloat().ToString();
        textBox1.AppendText("数据读取成功成功\r\n");
    }

    private void button8_Click(object sender, EventArgs e)
    {
        ushort A = Convert.ToUInt16(textBox2.Text.Trim());
        ushort B= Convert.ToUInt16(textBox3.Text.Trim());
        ushort C = Convert.ToUInt16(textBox4.Text.Trim());
        Single D = Convert.ToSingle(textBox5.Text.Trim());

        PLC.PLc.Write("DB1.DBW0",A);
        Thread.Sleep(100);
        PLC.PLc.Write("DB1.DBW2", B);
        Thread.Sleep(100);
        PLC.PLc.Write("DB1.DBW4", C);
        Thread.Sleep(100);
        PLC.PLc.Write("DB1.DBD6", D);


        textBox1.AppendText("数据写入成功\r\n");
    }

    private void button9_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值