使用 c#从 PLC 读取/写入 PLC 标签文件夹

问题描述 投票:0回答:1

我正在开发 C# 应用程序,我需要从 PLC 标签文件夹 (TIA Portal) 读取/写入值。 我看过 S7.Net 库和 Libnodave,但它们只读/写数据块,而不是像我的项目那样的标签。

它的建议:

bool val = true;
plc.Write("DB1.DBX0.0", val);

我愿意得到的:

bool val = true;
plc.Write("I0.0", val);

你有什么想法吗? PS:我知道有一个这样的旧帖子但没有解决。

c# plc tia-portal
1个回答
0
投票

我建议使用 Sharp7,它是 Snap7 库的 C# 端口。要read/write输入bytes,您可以使用EBRead/EBWrite函数,或者如果您想read/writebit使用ReadArea/WriteArea函数Sharp7 wiki。还要检查 target compability 是否适合您的 plc 型号。

例如读取plc输入代码可以是这样的:

    static void Main(string[] args)
    {
        var client = new S7Client();
        int result = client.ConnectTo("127.0.0.1", 0, 2);
        if (result != 0)
        {
            Console.WriteLine(client.ErrorText(result));
        }
        else
        {
            var buffer = new byte[1];
            //First parameter is offset, i.e. if you read 1 byte from offset 0 you read whole IB0
            //Second parameter define how many bytes you will read in buffer
            //Third parameter is buffer in which the data is written
            result = client.EBRead(0, buffer.Length, buffer);
            if (result != 0)
            {
                Console.WriteLine(client.ErrorText(result));
            }
            else
            {
                //Here you convert data from buffer to desiered c# type
                //First parameter is buffer with data
                //Second parameter is offset in buffer 
                byte input = S7.GetByteAt(buffer, 0);
                //Third parameter is only to get bit, to specify bit number in byte
                bool I00 = S7.GetBitAt(buffer, 0, 0);
                Console.WriteLine(input.ToString());
            }
        }
        Console.ReadKey();
    }
© www.soinside.com 2019 - 2024. All rights reserved.