Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- Imports System.Text
- Public Class Form1
- <DllImport("kernel32")> Private Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Long
- End Function
- <DllImport("kernel32")> Private Shared Function GetPrivateProfileString(section As String, key As String, def As String, retVal As StringBuilder, size As Integer, filePath As String) As Integer
- End Function
- Private Const WM_DEVICECHANGE As Integer = &H219
- Private Const DBT_DEVICEARRIVAL As Integer = &H8000
- Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
- Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
- If m.Msg = WM_DEVICECHANGE Then
- If m.WParam.ToInt32 = DBT_DEVICEARRIVAL Then
- GetPorts()
- ElseIf m.WParam.ToInt32 = DBT_DEVICEREMOVECOMPLETE Then
- GetPorts()
- End If
- End If
- MyBase.WndProc(m)
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- GetPorts()
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim devices As String = ""
- devices = iniread("Settings", "AutoConnectUSB") + "," + ListBox1.SelectedItem
- If devices.ToString.EndsWith(",") Then
- devices = devices.ToString.Substring(0, devices.Length - 1)
- End If
- iniwrite("Settings", "AutoConnectUSB", devices)
- End Sub
- Dim isConnected As Boolean
- Private Sub GetPorts()
- Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\SERIALCOMM")
- ListBox1.Items.Clear()
- For Each item In key.GetValueNames
- ListBox1.Items.Add(item.Replace("\Device\", ""))
- Next
- For Each item In key.GetValueNames
- For Each device In iniread("Settings", "AutoConnectUSB").ToString.Split(",")
- If item.Replace("\Device\", "") = device Then
- SerialPort1.PortName = key.GetValue(item)
- If isConnected = False Then
- Try
- SerialPort1.Open()
- isConnected = True
- Me.Text = "Connected"
- Exit For
- Catch ex As Exception
- GetPorts()
- Exit Sub
- End Try
- Else
- Try
- SerialPort1.Close()
- isConnected = False
- Me.Text = "Disconnected"
- GetPorts()
- Exit For
- Catch ex As Exception
- MsgBox("Already Disconnected")
- Exit Sub
- End Try
- End If
- End If
- Next
- Next
- End Sub
- Public path As String = My.Application.Info.DirectoryPath + "\LionTrack IButton Config.ini"
- Public Sub iniwrite(Section As String, Key As String, Value As String)
- WritePrivateProfileString(Section, Key, Value, path)
- End Sub
- Public Function iniread(Section As String, Key As String) As String
- Dim temp As New StringBuilder(255)
- Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, path)
- Return temp.ToString()
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement