首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蛇尖网不透明浮子

蛇尖网不透明浮子
EN

Stack Overflow用户
提问于 2016-02-08 18:21:38
回答 1查看 1.4K关注 0票数 2

我通过c#中的snmpsharpnet查询一个语法NAS,并为UPS电池获得以下值:

OID: 1.3.6.1.1.1.6574.3.1.1.0类型:不透明数据: 9F 78 04 42 C8 00

但是,它应该是一个浮点值=> 100.00。

电压相同:

OID: 1.3.6.1.1.6574.4.4.1.1.0类型:不透明数据: 9F 78 04 43 65 00

浮点值=> 230.00

我怎样才能得到价值呢?

我的守则:

代码语言:javascript
复制
        // SNMP community name
        OctetString communityo = new OctetString(community);
        // Define agent parameters class
        AgentParameters param = new AgentParameters(communityo);
        // Set SNMP version to 1 (or 2)
        param.Version = SnmpVersion.Ver1;
        // Construct the agent address object
        // IpAddress class is easy to use here because
        //  it will try to resolve constructor parameter if it doesn't
        //  parse to an IP address
        IpAddress agent = new IpAddress(host);

        // Construct target
        UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

        // Pdu class used for all requests
        Pdu pdu = new Pdu(PduType.Get);
        pdu.VbList.Add(batteryoid); //
        pdu.VbList.Add(voltageoid); //
        pdu.VbList.Add(statusoid); //

        // Make SNMP request
        SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

        // If result is null then agent didn't reply or we couldn't parse the reply.
        if (result != null)
        {
            // ErrorStatus other then 0 is an error returned by 
            // the Agent - see SnmpConstants for error definitions
            if (result.Pdu.ErrorStatus != 0)
            {
                // agent reported an error with the request
                Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                    result.Pdu.ErrorStatus,
                    result.Pdu.ErrorIndex);
            }
            else
            {
                // Reply variables are returned in the same order as they were added
                //  to the VbList
                Console.WriteLine("battery ({0}) ({1}): {2}",
                    result.Pdu.VbList[0].Oid.ToString(),
                    SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
                    result.Pdu.VbList[0].Value.ToString());
                Console.WriteLine("voltage ({0}) ({1}): {2}",
                    result.Pdu.VbList[1].Oid.ToString(),
                    SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
                    result.Pdu.VbList[1].Value.ToString());
                Console.WriteLine("status ({0}) ({1}): {2}",
                    result.Pdu.VbList[2].Oid.ToString(),
                    SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
                    result.Pdu.VbList[2].Value.ToString());
            }
        }
        else
        {
            Console.WriteLine("No response received from SNMP agent.");
        }
        target.Close();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-08 19:17:18

SNMP中的不透明数据意味着某个字段的值本身就是BER编码的ASN.1数据--它是用SNMP查询本身编码的同一种语言编码的。

对于某些快速背景,ASN.1是建模数据的标准-它定义了如何以标准方式构造数据,以便其他程序能够理解结构。ASN.1不是一个以比特和字节编码该结构的系统--这是BER或DER编码标准的工作。

您拥有的数据-- 9F 78 04 42 C8 00 00 --是误码率编码的数据。BER数据分为三部分:typelengthvalue

如果我们使用该模板反汇编字节,我们得到的是9f78类型;04是长度,42c80000是值。

根据一些NetSnmp文档9f78的'type‘代码在BER中的意思是"FLOATTYPE“。看上去很有希望。

下一个字节是长度字节,在本例中是04。这意味着值中有一个4字节的浮点数。

最后,我们得到了值代码42c80000。根据RFC 6340,这个值将被解释为一个标准的IEEE 754 32位浮点数.IEEE 754是用于将浮点值编码为位的最广泛使用的标准--这是大多数计算机和编程语言使用的标准。C#的float类型定义为IEEE 754 32位单精度浮点值,而double是IEEE 754 64位双精度浮点值。

如果我们使用汉迪在线转换器将原始字节转换为浮点,我们会发现作为IEEE 754 32位浮点数处理的42c80000是'100.0‘值。瞧,我们有你的价值。

既然我们已经理解了您的数据,那么让我们来了解如何获得处理它的代码。

您从这些设备获得的值应该始终是相同的格式,因此我们希望它们总是以9f78 04开头。因此,您应该检查得到的值是否包含这些字节,然后将它们分割掉,剩下的四个字节存储浮点值。您只需将这些字节转换为浮点数即可。

谢天谢地,.Net有一个类可以帮您完成这个任务-- BitConverter

代码语言:javascript
复制
    private static void BytesToSingle()
    {
        // Remember that  0x42c80000 has the 0x42 at the highest byte, 00 at the lowest byte.
        byte[] snmpData = new byte[] { 0x00, 0x00, 0xC8, 0x42 };

        single floatVal = BitConverter.ToSingle( snmpData, 0 );

        // Prints "100.0".
        Console.WriteLine( floatVal );
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35276556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档