Getting started at 1-wire with vb.net

Here are a few examples of methods to start a 1-wire project. I use the Netadapter because I’m connecting through Tini and it is running NetaAdapterHost.


'==================================================
'This is the easiest method, if we know which adapter is connected to the computer
'====================================================

Try

' get the default adapter
tini = com.dalsemi.onewire.OneWireAccessProvider.getAdapter("NetAdapter", "192.168.50")
' initialize adapter to proper settings

' print that we got an adapter
ResultsTextBox.AppendText(vbCrLf & "Adapter: " & tini.getAdapterName & " Port: " & tini.getPortName & vbCrLf & vbCrLf)

Catch ex As Exception
ResultsTextBox.AppendText(vbCrLf & vbCrLf & "Error: " & ex.ToString)
End Try


'=============================================
'Use this method to change the default adapter
'Then, connect to usinggetDefaultAdapter. I don't know why, but the property is not saved to system.
'=============================================

ResultsTextBox.AppendText(vbCrLf & "Adapter: " & com.dalsemi.onewire.OneWireAccessProvider.getProperty("onewire.adapter.default"))
java.lang.System.getProperties().put("onewire.adapter.default", "NetAdapter")
java.lang.System.getProperties().put("onewire.port.default", "192.168.0.50")
ResultsTextBox.AppendText(vbCrLf & "Adapter: " & com.dalsemi.onewire.OneWireAccessProvider.getProperty("onewire.adapter.default"))

Try
adapter = com.dalsemi.onewire.OneWireAccessProvider.getDefaultAdapter

ResultsTextBox.AppendText(vbCrLf & "Adapter: " & adapter.getAdapterName & " Port: " & adapter.getPortName & vbCrLf & vbCrLf)

Catch ex As Exception
ResultsTextBox.AppendText(vbCrLf & vbCrLf & "Error: " & ex.ToString)
End Try


'========================================================
'This lists all the possible values of adapter and port
'This way, it's easy to try every possibility and connect without knowing which hardware is plugged where
'========================================================

Dim adapter_enum As java.util.Enumeration
Dim port_enum As java.util.Enumeration
Dim port As String
adapter_enum = com.dalsemi.onewire.OneWireAccessProvider.enumerateAllAdapters()
While adapter_enum.hasMoreElements()
adapter = adapter_enum.nextElement()

ResultsTextBox.AppendText(vbCrLf & "Adapter: " & adapter.getAdapterName & " Port: ")

port_enum = adapter.getPortNames()

While port_enum.hasMoreElements()
port = port_enum.nextElement()
ResultsTextBox.AppendText(port & vbCrLf & vbCrLf)
End While

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *