亲宝软件园·资讯

展开

C# 读取PLC 寄存器地址 C# 使用modbus 读取PLC 寄存器地址的方法

peiban1994 人气:4
想了解C# 使用modbus 读取PLC 寄存器地址的方法的相关内容吗,peiban1994在本文为您仔细讲解C# 读取PLC 寄存器地址的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,读取PLC,寄存器地址,C#,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("\r\n");
                            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("\r\n");
                            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("\r\n");
                            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("\r\n");
                            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);
        }

加载全部内容

相关教程
猜你喜欢
用户评论