Advertisement
AsmodHacker

vb.NET Serial Comport USB Multi Scanner and Connector.

Sep 24th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2. Imports System.Text
  3.  
  4. Public Class Form1
  5.  
  6. <DllImport("kernel32")> Private Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Long
  7. End Function
  8. <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
  9. End Function
  10.  
  11. Private Const WM_DEVICECHANGE As Integer = &H219
  12. Private Const DBT_DEVICEARRIVAL As Integer = &H8000
  13. Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
  14.  
  15. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  16. If m.Msg = WM_DEVICECHANGE Then
  17. If m.WParam.ToInt32 = DBT_DEVICEARRIVAL Then
  18. GetPorts()
  19. ElseIf m.WParam.ToInt32 = DBT_DEVICEREMOVECOMPLETE Then
  20. GetPorts()
  21. End If
  22. End If
  23. MyBase.WndProc(m)
  24. End Sub
  25.  
  26. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  27. GetPorts()
  28. End Sub
  29.  
  30. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  31. Dim devices As String = ""
  32. devices = iniread("Settings", "AutoConnectUSB") + "," + ListBox1.SelectedItem
  33. If devices.ToString.EndsWith(",") Then
  34. devices = devices.ToString.Substring(0, devices.Length - 1)
  35. End If
  36. iniwrite("Settings", "AutoConnectUSB", devices)
  37. End Sub
  38.  
  39. Dim isConnected As Boolean
  40.  
  41. Private Sub GetPorts()
  42. Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\DEVICEMAP\SERIALCOMM")
  43. ListBox1.Items.Clear()
  44. For Each item In key.GetValueNames
  45. ListBox1.Items.Add(item.Replace("\Device\", ""))
  46. Next
  47.  
  48. For Each item In key.GetValueNames
  49. For Each device In iniread("Settings", "AutoConnectUSB").ToString.Split(",")
  50. If item.Replace("\Device\", "") = device Then
  51. SerialPort1.PortName = key.GetValue(item)
  52. If isConnected = False Then
  53. Try
  54. SerialPort1.Open()
  55. isConnected = True
  56. Me.Text = "Connected"
  57. Exit For
  58. Catch ex As Exception
  59. GetPorts()
  60. Exit Sub
  61. End Try
  62. Else
  63. Try
  64. SerialPort1.Close()
  65. isConnected = False
  66. Me.Text = "Disconnected"
  67. GetPorts()
  68. Exit For
  69. Catch ex As Exception
  70. MsgBox("Already Disconnected")
  71. Exit Sub
  72. End Try
  73. End If
  74. End If
  75. Next
  76. Next
  77. End Sub
  78.  
  79. Public path As String = My.Application.Info.DirectoryPath + "\LionTrack IButton Config.ini"
  80. Public Sub iniwrite(Section As String, Key As String, Value As String)
  81. WritePrivateProfileString(Section, Key, Value, path)
  82. End Sub
  83. Public Function iniread(Section As String, Key As String) As String
  84. Dim temp As New StringBuilder(255)
  85. Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, path)
  86. Return temp.ToString()
  87. End Function
  88.  
  89. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement