我是靠谱客的博主 美丽板栗,最近开发中收集的这篇文章主要介绍C# 使用modbus 读取PLC 寄存器地址的方法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

使用的组件Nmodbus

定义参数,全局变量:

//创建modbus实体对象        private static ModbusFactory modbusFactory;
        private static IModbusMaster master;
        //写线圈或写寄存器数组
        bool[] coilsBuffer;
        ushort[] registerBuffer;
        //功能码
        string functionCode;
        //参数(分别为站号,起始地址,长度)
        byte slaveAddress;
        ushort startAddress;
        ushort numberOfPoints;

        //ip 地址与端口号
        string IPAddress = string.Empty;
        int VisitPort = 502;

初始化参数,创建连接:

private void Connection(object sender, EventArgs e)
         {
          try
           {
                //PLC连接配置信息,PLC ip地址
                IPAddress =127.0.0.1;                //端口,modbus 默认502,根据实际情况填写
                VisitPort =502;

                //初始化modbusmaster
                modbusFactory = new ModbusFactory();
                //在本地测试 所以使用回环地址,modbus协议规定端口号 502
                master = modbusFactory.CreateMaster(new TcpClient(IPAddress, VisitPort));
                //设置读取超时时间
                master.Transport.ReadTimeout = 10000;
                master.Transport.Retries = 2000;  
            }
            catch (Exception ex)
            {
              MessageBox.Show(ex.Message);
            }
        }

初始化读写参数

/// <summary>
        /// 初始化读参数
        /// </summary>
        private void SetReadParameters(int slaveAddress=1,int startAddress=200,int numberOfPoints=1)
        {           //DeviceID
           slaveAddress = byte.Parse(slaveAddress);           //起始地址,
           startAddress = ushort.Parse(startAddress);           //数据长度
           numberOfPoints = ushort.Parse(numberOfPoints);            
        }

运行读取方法

private async void ExecuteFunction()
        {
            try
            {             //Moddbus共有这些功能码               //01 Read Coils               //02 Read DisCrete Inputs               //03 Read Holding Registers               //04 Read Input Registers               //05 Write Single Coil               //06 Write Single Registers               //0F Write Multiple Coils               //10 Write Multiple Registers                 functionCode="01 Read Coils";//根据实际点号类型选择读写功能码
                //重新实例化是为了 modbus slave更换连接时不报错 
                //master = modbusFactory.CreateMaster(new TcpClient(IPAddress, VisitPort));
                if (functionCode != null)
                {
                    switch (functionCode)
                    {
                        case "01 Read Coils"://读取单个线圈
                            SetReadParameters();
                            coilsBuffer = master.ReadCoils(slaveAddress, startAddress, numberOfPoints);

                            for (int i = 0; i < coilsBuffer.Length; i++)
                            {
                                SetMsg(coilsBuffer[i] + " ");
                            }
                            SetMsg("rn");
                            break;
                        case "02 Read DisCrete Inputs"://读取输入线圈/离散量线圈
                            SetReadParameters();

                            coilsBuffer = master.ReadInputs(slaveAddress, startAddress, numberOfPoints);
                            for (int i = 0; i < coilsBuffer.Length; i++)
                            {
                                SetMsg(coilsBuffer[i] + " ");
                            }
                            SetMsg("rn");
                            break;
                        case "03 Read Holding Registers"://读取保持寄存器
                            SetReadParameters();
                            registerBuffer = master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
                            for (int i = 0; i < registerBuffer.Length; i++)
                            {
                                SetMsg(registerBuffer[i] + " ");
                            }
                            SetMsg("rn");
                            break;
                        case "04 Read Input Registers"://读取输入寄存器
                            SetReadParameters();
                            registerBuffer = master.ReadInputRegisters(slaveAddress, startAddress, numberOfPoints);
                            for (int i = 0; i < registerBuffer.Length; i++)
                            {
                                SetMsg(registerBuffer[i] + " ");
                            }
                            SetMsg("rn");
                            break;
                        case "05 Write Single Coil"://写单个线圈
                            SetWriteParametes();
                            await master.WriteSingleCoilAsync(slaveAddress, startAddress, coilsBuffer[0]);
                            break;
                        case "06 Write Single Registers"://写单个输入线圈/离散量线圈
                            SetWriteParametes();
                            await master.WriteSingleRegisterAsync(slaveAddress, startAddress, registerBuffer[0]);
                            break;
                        case "0F Write Multiple Coils"://写一组线圈
                            SetWriteParametes();
                            await master.WriteMultipleCoilsAsync(slaveAddress, startAddress, coilsBuffer);
                            break;
                        case "10 Write Multiple Registers"://写一组保持寄存器
                            SetWriteParametes();
                            await master.WriteMultipleRegistersAsync(slaveAddress, startAddress, registerBuffer);
                            break;
                        default:
                            break;
                    }

                }
                else
                {
                    MessageBox.Show("请选择功能码!");
                }
                master.Dispose();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

//展示消息

/// <summary>
        /// SetMessage
        /// </summary>
        /// <param name="msg"></param>
        public void SetMsg(string msg)
        {     
            MessageBox.Show(msg);
        }

到此这篇关于C# 使用modbus 读取PLC 寄存器地址的文章就介绍到这了,更多相关C# 读取PLC 寄存器地址内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是美丽板栗为你收集整理的C# 使用modbus 读取PLC 寄存器地址的方法的全部内容,希望文章能够帮你解决C# 使用modbus 读取PLC 寄存器地址的方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(95)

评论列表共有 0 条评论

立即
投稿
返回
顶部