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);

  

原文地址:https://www.cnblogs.com/HansZimmer/p/13469296.html