2013-10-04 70 views
0

我是一名新手,正在編寫自動啤酒廠中的代碼,並將其重寫爲從模擬溫度感測轉換爲單線。OneWire DS18B20從指定的傳感器獲取vb.net中的溫度

但是,我正在努力獲得任何喜悅,有人可以運行他們的大腦在這段代碼對我和建議請嗎?目標是讓HLT_currenttemp讀取hltaddress處DS18B20傳感器的當前溫度。

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 

問候

史蒂夫

回答

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