.NET与C#和PLC交互的例子

要读取PLC中指定数据块(DB)内连续的多个字节,可以使用HSLCommunication库的Read方法。这个方法允许你一次性读取多个字节,然后再根据需要解析这些字节。

下面是修改后的代码,示范如何从指定数据块的起始地址读取30个字节的数据:

csharp 复制代码
using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.Profinet.Siemens;

namespace PLCdemo
{
    public partial class Form1 : Form
    {
        private SiemensS7Net plc;

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

        public void InitHslCommunication()
        {
            plc = new SiemensS7Net(SiemensPLCS.S1200, "10.32.58.65");

            // 设置连接超时时间(可选)
            plc.ConnectTimeOut = 3000; // 3秒

            // 启动连接和读写操作
            PlcOperation();
        }

        private void PlcOperation()
        {
            try
            {
                // 连接到PLC
                OperateResult connect = plc.ConnectServer();
                if (connect.IsSuccess)
                {
                    UpdateLabel("连接成功!");

                    // 读取DB50数据块中的连续30个字节
                    OperateResult<byte[]> readResult = plc.Read("DB50.0", 30);
                    if (readResult.IsSuccess)
                    {
                        byte[] data = readResult.Content;
                        string hex = BitConverter.ToString(data);
                        UpdateLabel("读取成功,数据为:" + hex);

                        // 解析特定地址的数据,例如读取DWord
                        uint taskNum = BitConverter.ToUInt32(data, 0);
                        UpdateLabel("任务号为:" + taskNum);

                        // 等待一段时间,便于观察读取结果
                        Thread.Sleep(2000);
                    }
                    else
                    {
                        UpdateLabel("读取失败,原因:" + readResult.Message);
                    }
                }
                else
                {
                    UpdateLabel("连接失败,原因:" + connect.Message);
                }
            }
            catch (Exception ex)
            {
                UpdateLabel("发生异常:" + ex.Message);
            }
            finally
            {
                // 确保连接关闭
                plc.ConnectClose();
                UpdateLabel("已断开与PLC的连接");
            }
        }

        private void UpdateLabel(string message)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string>(UpdateLabel), message);
            }
            else
            {
                label1.Text = message;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

说明:

  1. 读取数据 :

    使用 plc.Read("DB50.0", 30) 从 DB50 数据块的起始地址读取30个字节的数据。注意这里使用的是"DB50.0"表示数据块DB50的起始地址。

  2. 解析数据 :

    读取到的数据是一个字节数组,可以根据需要进行解析。例如,BitConverter.ToUInt32(data, 0) 用于将前4个字节解析为一个DWord(任务号)。

  3. 显示数据 :

    使用 BitConverter.ToString(data) 将字节数组转换为十六进制字符串,便于显示读取到的原始数据。同时,解析特定地址的数据并显示任务号。

  4. 异常处理和资源释放 :

    在finally块中关闭与PLC的连接,并更新界面标签,以确保无论操作成功还是失败,都能正确关闭连接。

这样,你就可以从PLC中读取指定的数据块并解析其中的内容了。

相关推荐
用户7884773163413 小时前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
时间的情敌13 小时前
Cesium 流动管线、管道动画、管线流向仿真,管网、油气、水利孪生高频需求
前端·交互·cesium
寂寞的船17 小时前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
Jeff-Tsai18 小时前
Day 19:定制层的范围与叠加粒度
.net·架构设计·erp·definition-driven
用户3721574261351 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船1 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
Jeff-Tsai2 天前
Day 18:错误契约与用户可见消息
.net·架构设计·erp·definition-driven
IT规划师2 天前
HybridCache-缓存架构决策框架落地
架构·.net
Jeff-Tsai3 天前
Day 17:Connector 与调用端的职责收敛
.net·架构设计·erp·definition-driven
huaqianzkh3 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#