OneWire DS18B20从指定的传感器获取vb.net中的温度

时间:2013-10-04 10:53:43

标签: vb.net

我是编程的新手,正在重写我的自动啤酒厂的代码,以便从模拟温度传感转移到onewire。

我正在努力获得任何快乐,但是有人可以为我提供这些代码的大脑并建议吗?目的是让HLT_currenttemp读取DS18B20传感器在hltaddress的当前温度。

 Dim owd As com.dalsemi.onewire.container.OneWireContainer
    Dim state As Object
    Dim tc As com.dalsemi.onewire.container.TemperatureContainer
    ' Try
    ' get exclusive use of 1-Wire network
    adapter.beginExclusive(True)
    ' clear any previous search restrictions
    adapter.setSearchAllDevices()
    adapter.targetAllFamilies()
    adapter.setSpeed(com.dalsemi.onewire.adapter.DSPortAdapter.SPEED_REGULAR)
    owd = CreateObject("OWAPI.OneWireContainer28")
    ' retrieve OneWireContainer
    owd.setupContainer(adapter, hltaddress)

    ' cast the OneWireContainer to TemperatureContainer
    tc = DirectCast(owd, com.dalsemi.onewire.container.TemperatureContainer)
    ' read the device
    state = tc.readDevice
    ' extract the temperature from previous read
    tc.doTemperatureConvert(state)
    ' retrieve Temp
    HLT_Currentemp = (Math.Round(tc.getTemperature(state), 2))

    Debug.WriteLine(tc.getTemperature(state))
    ' end exclusive use of 1-Wire net adapter
    adapter.endExclusive()
    'Catch ex As Exception
        Debug.WriteLine(hltaddress)
        ' End Try

此致

史蒂夫

1 个答案:

答案 0 :(得分:0)

终于明白了!

' retrieve OneWireContainer
        owd = adapter.getDeviceContainer(hltaddress)
        ' cast the OneWireContainer to TemperatureContainer
        tc = DirectCast(owd, com.dalsemi.onewire.container.TemperatureContainer)
        ' read the device
        state = tc.readDevice
        ' extract the temperature from previous read
        tc.doTemperatureConvert(state)
        ' retrieve Temp
        HLT_Currentemp = (Math.Round(tc.getTemperature(state), 3))