zoukankan      html  css  js  c++  java
  • C# 通过 snmp协议 获取上位机接口数据

    一:业务需求

      通过Snmp通讯协议获取上位机里的温湿度状态信息

    二:Snmp定义

      为不同种类的设备、不同厂家生产的设备、不同型号的设备,定义为一个统一的接口和协议,即 网络管理协议

    三:C#获取数据

      开发文档如下图:

    --------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------

      1.在VS的Nuget里下载 SnmpSharpNet 包

      2.方法如下

            public static string getSnmpValByOid(string host, int snmpPort, string community, string oid)
            {
                string val = "";
                OctetString second = new OctetString(community);
                AgentParameters param = new AgentParameters(second);
    
                param.Version = SnmpVersion.Ver2;
    
                IpAddress agent = new IpAddress(host);
    
                UdpTarget target = new UdpTarget((IPAddress)agent, snmpPort, 2000, 1);
    
                Pdu pdu = new Pdu(PduType.Get);
                pdu.VbList.Add(oid);
    
                SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
                if (result != null)
                {  
                    if (result.Pdu.ErrorStatus == 0)
                    {
                        val = result.Pdu.VbList[0].Value.ToString();
                    }
                }
                target.Close();
                return val;
            }

      3.调用如下:

    string result = getSnmpValByOid("192.168.1.12",161,"public", "1.3.6.1.4.1.34651.2.2.1.10.1");
    Console.WriteLine(result);

      

  • 相关阅读:
    c++ 中的substr
    c++ 中将数字字符串转换成int输出的stoi() 和 atoi()
    c++ 四舍五入函数 round
    stddef.h----常用函数
    local.h-----地区函数
    errno.h-----定义出错代码
    signal.h-----信号函数
    stdio.h----标准的输入输出函数
    time.h-------日期与时间函数
    math.h--------数学函数
  • 原文地址:https://www.cnblogs.com/HansZimmer/p/13469296.html
Copyright © 2011-2022 走看看