HslCommunication模拟西门子读写数据

导入HslCommunication

HslCommunication模拟西门子读写数据_第1张图片

C#端代码(上位机)

这里要注意的是上位机IP用的当前电脑的IP。

using HslCommunication;
using HslCommunication.Profinet.Siemens;
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;

namespace hslcommonicationDemo
{
    public partial class Form1 : Form
    {
        //创建PLC:西门子S1500
        SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1500,"192.168.56.1");
        public Form1()
        {
            InitializeComponent();
            //机架号默认0 ,槽号默认1
            plc.Rack = 0;
            plc.Slot = 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        OperateResult operateResult;
        private void button1_Click(object sender, EventArgs e)
        {
             operateResult = plc.ConnectServer();
            if (operateResult.IsSuccess)
            {
                MessageBox.Show("连接成功!");
                panel1.Enabled= true;   
            }
            else {
                MessageBox.Show("连接失败!"); 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
             operateResult = plc.ConnectClose();
            if (operateResult.IsSuccess)
            {
                MessageBox.Show("PLC已断开!");
                panel1.Enabled = false;
            }
            else
            {
                MessageBox.Show("断开失败!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OperateResult result = plc.ReadBool(textBox1.Text);
            textBox2.Text = result.Content.ToString();
            
            OperateResult result2 = plc.ReadInt16(textBox3.Text);
            textBox4.Text = result2.Content.ToString();

            OperateResult result3 = plc.ReadFloat(textBox5.Text);
            textBox6.Text = result3.Content.ToString();

            MessageBox.Show("读取成功!");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            plc.Write(textBox1.Text, bool.Parse(textBox2.Text));
            plc.Write(textBox3.Text, short.Parse(textBox4.Text));
            plc.Write(textBox5.Text, float.Parse(textBox6.Text));
            MessageBox.Show("写入成功!");
        }
    }
}

源码

https://download.csdn.net/download/liuming690452074/88527936

效果

HslCommunication模拟西门子读写数据_第2张图片

参考
https://www.bilibili.com/video/BV1Zz4y1A7gU/?spm_id_from=333.880.my_history.page.click&vd_source=297c866c71fa77b161812ad631ea2c25

https://www.bilibili.com/video/BV1JM411g7tk/?spm_id_from=333.337.search-card.all.click&vd_source=297c866c71fa77b161812ad631ea2c25

HSL的源码

nullicon-default.png?t=N7T8https://gitee.com/taohuaguo/HslCommunication

HslCommunication模拟西门子读写数据_第3张图片

你可能感兴趣的:(#,C#,c#)