Imports System.Management Public Class Form1 Public Function ModemConnected() As String Dim modems As String = "" Try Dim Searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_POTSModem") For Each queryObj As ManagementObject In Searcher.Get() If queryObj("Status") = "OK" Then modems = modems & (queryObj("AttachedTo") & " - " & queryObj("Description") & "***") End If Next Catch ex As Exception MsgBox(ex.Message) Return "" End Try Return modems End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ports() As String ports = Split(ModemConnected(), "***") For i As Integer = 0 To ports.Length - 2 ComboBox1.Items.Add(ports(i)) Next Try ComboBox1.SelectedIndex = 0 Catch ex As Exception End Try End Sub Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click If btnConnect.Text = "Connect" Then Try Dim df() As String = ComboBox1.Text.Split(" - ") With SerialPort1 .PortName = df(0).ToString .BaudRate = 9600 .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One .Handshake = IO.Ports.Handshake.None .RtsEnable = True .ReceivedBytesThreshold = 1 .NewLine = vbCr .ReadTimeout = 1000 .Open() End With Catch ex As Exception MsgBox(ex.Message) Exit Sub End Try If SerialPort1.IsOpen = True Then With Label3 .Text = "Connected" .ForeColor = Color.Green End With btnConnect.Text = "Diconnect" End If Else Try SerialPort1.Close() Catch ex As Exception MsgBox(ex.Message) Exit Sub End Try With Label3 .Text = "Not Connected" .ForeColor = Color.Red End With btnConnect.Text = "Connect" End If End Sub Private Sub Output_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Output.TextChanged Output.SelectionStart = Output.Text.Length - 1 Output.ScrollToCaret() End Sub Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click Try SerialPort1.WriteLine(Input.Text & vbCr) Catch ex As Exception MsgBox(ex.Message) End Try For Each d As String In input.Items If input.Text = d Then Exit Sub Next input.Items.Add(input.Text) input.SelectAll() End Sub Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Invoke(Sub() Output.Text &= vbCr & vbCr & SerialPort1.ReadExisting()) End Sub Private Sub input_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles input.SelectedIndexChanged End Sub Private Sub input_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles input.KeyPress If e.KeyChar = vbCr Then btnSend.PerformClick() End If End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)