C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# 读取PLC 寄存器地址

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

作者:peiban1994

今天通过本文给大家介绍C# 使用modbus 读取PLC 寄存器地址的方法,使用的组件Nmodbus,文中通过实例代码给大家介绍的非常详细,需要的朋友参考下吧

使用的组件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);
        }

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

您可能感兴趣的文章:
阅读全文