使用modbusTCP给西门子PLC传输字符串

modbus的报文是16进制数,PLC使用word(16位)或者int(16位)类型能接收到modbus的16进制数报文。

在PLC中,可以用以下方法把word/int解析为String类型。

在这里插入图片描述
在这里插入图片描述
代码:

    #pos := 0;
    "GunData".dints[0] := Serialize(SRC_VARIABLE := "GunData".ushorts1,
                                    DEST_ARRAY => "GunData".bytes,
                                    POS := #pos);
    
    Chars_TO_Strg(Chars := "GunData".bytes,
                  pChars := "GunData".dints[2],
                  Cnt := "GunData".uints[1],
                  Strg => "GunData".strings[0]);

先用序列化命令Serialize将 “GunData”.ushorts 解析为"GunData".bytes,即word数组解析为byte字节数组。

最后使用命令Chars_TO_Strg 将字节数组解析为字符串。Chars_TO_Strg 参数定义如下:

在这里插入图片描述

附带上我的C# modbus通信库,基于Nmodbus封装。

using Modbus.Device;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZgCsharp.Entities;
using ZgCsharp.SqlSugar.Impl;
using ZgCsharp.SqlSugar.Repository.Impl;

namespace ZgCsharp.Modbus
{
    public class ModbusHelper : IModbusHelper
    {
        bool last = false;
        bool trigIn = false;
        bool trigQ = false;

        private string ip;
        private string port;


        private List<ActualData> actualDatas = new List<ActualData>();

        public List<ActualData> ActualDatas
        {
            get { return actualDatas; }
            set { actualDatas = value; }
        }

        /**
         * modubs从站ID
         */
        private string slaveNo;

        private bool isConnected = false;

        private bool isFirstConn = true;

        private int errorTimes = 0;

        CancellationTokenSource cts = new CancellationTokenSource();

        /**
         * 起始地址
         */

        private string address;

        //变量个数
        private string varNum;

        private TcpClient tcpClient = null;

        private ModbusIpMaster master;

        //配置文件地址
        private string jsonfile = Application.StartupPath + "\\Config\\modbuscfg1.json";

        private List<string> trendList = new List<string>();

        public List<string> TrendList
        {
            get { return trendList; }
            set { trendList = value; }
        }

        private Dictionary<string, string> currentPLCNote = new Dictionary<string, string>();

        public Dictionary<string, string> CurrentPLCNote
        {
            get { return currentPLCNote; }
            set { currentPLCNote = value; }
        }

        private Dictionary<string, string> currentPLCValue = new Dictionary<string, string>();

        public Dictionary<string, string> CurrentPLCValue
        {
            get { return currentPLCValue; }
            set { currentPLCValue = value; }
        }




        public string Jsonfile
        {
            get { return jsonfile; }
        }

        //读取变量
        private string str;
        private CfgJson cfgJson;
        private ModelRepository modelRepository;
        private List<ReportData> reportDatas = new List<ReportData>();

        public ModbusHelper(ModelRepository modelRepository)
        {
            this.modelRepository = modelRepository;
            str = File.ReadAllText(jsonfile);
            cfgJson = JsonConvert.DeserializeObject<CfgJson>(str);

            this.ip = cfgJson.Ip;
            this.port = cfgJson.Port;
            this.slaveNo = cfgJson.SlaveNo;
            this.address = cfgJson.Address;
            this.varNum = cfgJson.VarNum;
            this.actualDatas = cfgJson.ActualDatas;

            foreach (var item in actualDatas)
            {
                trendList.Add(item.Name);
                if (!currentPLCNote.ContainsKey(item.Name))
                {
                    currentPLCNote.Add(item.Name, item.Description);
                }
                else
                {
                    currentPLCNote[item.Name] = item.Description;
                }
            }



            //this.Conn();
            //this.readHoldingRegisters();
        }

        public string Conn()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(ip), int.Parse(port));
                master = ModbusIpMaster.CreateIp(tcpClient);
                isConnected = true;
                master.Transport.ReadTimeout = 1000;//读取数据超时时间为设定值

                master.Transport.WriteTimeout = 1000;//写入数据超时时间为设定值

                master.Transport.Retries = 5;

                master.Transport.WaitToRetryMilliseconds = 2500;
                Console.WriteLine("OK");
                return "OK";

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return ex.Message;
            }
        }

        public string DisConn()
        {
            if (master != null)
            {
                master.Dispose();
                tcpClient.Dispose();
                tcpClient.Close();
                return "OK";
            }
            else
            {
                return "Failed";
            }
        }

        public void readHoldingRegisters()
        {
            Task.Run(async () =>
            {
                while (!cts.IsCancellationRequested)
                {
                    if (isConnected == true)
                    {
                        ushort[] des = null;
                        await Task.Delay(500);
                        try
                        {
                            des = master.ReadHoldingRegisters(byte.Parse(slaveNo), ushort.Parse(address), ushort.Parse(varNum));
                        }
                        catch (Exception ex)
                        {

                        }

                        //读出的数据不为空,则解析数据,存储
                        if (des != null)
                        {
                            errorTimes = 0;
                            for (int i = 0; i < actualDatas.Count; i++)
                            {
                                actualDatas[i].Value = ((float)des[i] * float.Parse(actualDatas[i].Scale)).ToString();
                                actualDatas[i].CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            //foreach (var item in actualDatas)
                            //{
                            //    Console.WriteLine(item.Value);
                            //}

                            foreach (var item in actualDatas)
                            {
                                if (!currentPLCValue.ContainsKey(item.Name))
                                {
                                    currentPLCValue.Add(item.Name, item.Value);
                                }
                                else
                                {
                                    currentPLCValue[item.Name] = item.Value;
                                }
                            }


                            ReportDataGenerate();
                            //按事件插入数据库
                            Insert();
                        }

                        else
                        {
                            errorTimes++;
                            await Task.Delay(500);
                            if (errorTimes >= 3)
                            {
                                isConnected = false;
                            }
                        }
                    }
                    else
                    {
                        if (!isFirstConn)
                        {
                            await Task.Delay(1000);

                            tcpClient?.Close();
                            //已经测试,不行
                            //tcpClient.Dispose();
                            //master?.Transport.Dispose();
                            //master?.Dispose();
                        }

                        try
                        {
                            //首次连接,或多次连接
                            tcpClient = new TcpClient();
                            tcpClient.Connect(IPAddress.Parse(ip), int.Parse(port));
                            //先将master置为空,再断线重连
                            master = null;
                            master = ModbusIpMaster.CreateIp(tcpClient);
                            master.Transport.ReadTimeout = 1000;//读取数据超时时间为设定值

                            master.Transport.WriteTimeout = 1000;//写入数据超时时间为设定值

                            master.Transport.Retries = 5;

                            master.Transport.WaitToRetryMilliseconds = 2500;
                            isConnected = true;
                            isFirstConn = false;

                        }
                        catch (Exception ex)
                        {

                        }


                    }

                }
            }, cts.Token);
        }

        public string writeHoldingRegisters(string varAddress, string value)
        {
            master.WriteSingleRegister(byte.Parse(this.slaveNo), ushort.Parse(varAddress), ushort.Parse(value));
            return "OK";
        }

        public string WriteMultipleRegisters(ushort starAddress, ushort[] data)
        {
            master.WriteMultipleRegisters(byte.Parse(this.slaveNo), starAddress, data);
            return "OK";
        }

        public void Insert()
        {
            trigIn = this.actualDatas[9].Value.Equals("10");
            trigQ = (trigIn & !last);
            last = trigIn;
            if (trigQ)
            {
                try
                {
                    InsertDataBase();
                }
                catch (Exception ex)
                {
                    
                }
            }
        }

        //插入数据工具类
        public int InsertDataBase()
        {
            string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            foreach (var item in this.actualDatas)
            {
                item.InsertTime = now;
            }
            int myCount = modelRepository.InsertReportData(reportDatas);
            int count = modelRepository.InsertActualData(this.actualDatas);

            return count+ myCount;
        }

        //生成数据
        public void ReportDataGenerate()
        {
            if (reportDatas.Count < 500)
            {
                reportDatas.Add(new ReportData()
                {
                    InsertTime = DateTime.Now,
                    Var1 = actualDatas[0].Value,
                    Var2 = actualDatas[1].Value,
                    Var3 = actualDatas[2].Value,
                    Var4 = actualDatas[3].Value,
                    Var5 = actualDatas[4].Value,
                    Var6 = actualDatas[5].Value,
                    Var7 = actualDatas[6].Value,
                    Var8 = actualDatas[7].Value,
                    Var9 = actualDatas[8].Value,
                    Var10 = actualDatas[9].Value
                });
            }
            else
            {
                reportDatas.RemoveAt(0);
            }
        }


    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZgCsharp.Modbus
{
    public interface IModbusHelper
    {
        string Conn();
        string DisConn();
        void readHoldingRegisters();
        string writeHoldingRegisters(string varAddress, string value);
        string WriteMultipleRegisters(ushort starAddress,ushort[] data);
    }
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: LabVIEW是一款功能强大的可视化编程软件,广泛应用于各个领域的自动化控制系统中。Modbus TCP协议是一种基于TCP/IP协议的通讯协议,广泛应用于各种工业自动化设备中。 使用LabVIEW的Modbus TCP函数,可以实现与PLC之间的通讯。首先需要建立一个Modbus TCP连接,通常使用TCP Client来实现。然后需要设置数据格式、寄存器地址等参数,来指定要读写的寄存器及其内容。 接下来,使用Modbus TCP函数来进行数据的读写操作。读操作可以使用Modbus TCP Read函数,指定要读取的寄存器地址及数据类型,然后将读取到的数据保存到一个变量中。写操作可以使用Modbus TCP Write函数,指定要写入的寄存器地址及数据类型,然后将要写入的数据传递给函数即可。 除此之外,还可以使用Modbus TCP Read Multiple和Modbus TCP Write Multiple函数来一次性读写多个寄存器的数据,提高通讯效率。 总之,使用LabVIEW的Modbus TCP函数,可以方便快捷地实现与PLC的通讯,为工业自动化控制系统的开发与维护提供了良好的支持。 ### 回答2: 使用LabVIEW中的Modbus TCP函数,可以方便地与PLC进行通讯。Modbus TCP是一种开放式的通讯协议,其主要作用是实现设备之间的通讯。PLC作为一种广泛应用的工业控制设备,通常具有Modbus TCP通讯功能。 在使用LabVIEW与PLC进行通讯时,首先需要确保PLC与计算机处于同一局域网中,并分配好IP地址。然后,在LabVIEW中添加Modbus TCP函数库,可以在该函数库中找到与PLC通讯所需的函数。 Modbus TCP通讯通常通过TCP/IP协议进行数据传输。因此,在使用LabVIEW中的Modbus TCP函数进行通讯时,需要设置好通讯的IP地址和端口号等参数。具体步骤为:打开LabVIEW软件,选择Tools菜单下的:Instrumentation和控制→Connectivity→Modbus,然后在该模块中进行相关的设置。设置完成后,LabVIEW即可实现与PLC通讯。 通常,PLC与计算机之间的通讯需要传输大量的数据,因此在进行通讯时,需要考虑到数据的传输速度以及数据的正确性。除此之外,在实际应用中,还需要考虑到安全性、稳定性等问题。因此,在使用LabVIEW进行PLC通讯时,除了选择合适的Modbus TCP函数库之外,还需要进行充分的测试和调试,以确保通讯的准确性和稳定性。 ### 回答3: LabVIEW是一款图形化编程语言,广泛应用于工业自动化和实验室测试中。PLC(可编程逻辑控制器)是一种自动化控制系统,被广泛应用于工业控制,特别是在制造业中。 Modbus TCP是一种通讯协议,用于基于TCP/IP网络的设备间通讯。在工业控制领域中,PLC是常用的设备之一。LabVIEW通过使用Modbus TCP函数实现与PLC的通讯。 Modbus TCP函数库提供了一种简单、快速和可靠的方式来连接和获取PLC中的数据。使用该函数库时,用户需要指定PLC的IP地址、连接端口号、Modbus服务器地址和端口等参数。通过这些参数,LabVIEW可以知道如何与目标PLC进行通讯,在此基础上,用户可以构建自己的控制逻辑和数据采集程序。 总之,LabVIEW通过Modbus TCP函数库实现与PLC的通讯,为工业自动化和实验室测试等领域提供了便利和高效性。同时,PLC的广泛应用也加速了制造业的自动化进程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值