1

如何使用 vb.net 找到 gsm 调制解调器的 com 端口?我写这段代码:

Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.Ports

Public Class Form1
'connect your mobile/GSM modem to PC,
'then go in device manager and check under ports which COM port has been slected
'if say com1 is there then put com2 in following statement
Dim SMSEngine As New SMSCOMMS("COM5")
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    SMSEngine.Open() 'open the port
    SMSEngine.SendSMS() 'send the SMS

End Sub
End Class
Public Class SMSCOMMS
Private WithEvents SMSPort As SerialPort
Private SMSThread As Thread
Private ReadThread As Thread
Shared _Continue As Boolean = False
Shared _ContSMS As Boolean = False
Private _Wait As Boolean = False
Shared _ReadPort As Boolean = False
Public Event Sending(ByVal Done As Boolean)
Public Event DataReceived(ByVal Message As String)

Public Sub New(ByRef COMMPORT As String)
    'initialize all values
    SMSPort = New SerialPort
    With SMSPort
        .PortName = COMMPORT
        .BaudRate = 19200
        .Parity = Parity.None
        .DataBits = 8
        .StopBits = StopBits.One
        .Handshake = Handshake.RequestToSend
        .DtrEnable = True
        .RtsEnable = True
        .NewLine = vbCrLf
    End With
End Sub
Public Function SendSMS() As Boolean
    If SMSPort.IsOpen = True Then
        'sending AT commands
        SMSPort.WriteLine("AT")
        SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
        SMSPort.WriteLine("AT+CMGS=""+628988397877""" & vbCrLf) ' enter the mobile number whom you want to send the SMS
        _ContSMS = False
        SMSPort.WriteLine("SUCCESS" & vbCrLf & Chr(26)) 'SMS sending
        SMSPort.Close()
    End If
End Function

Public Sub Open()
    If Not (SMSPort.IsOpen = True) Then
        SMSPort.Open()
    End If
End Sub

Public Sub Close()
    If SMSPort.IsOpen = True Then
        SMSPort.Close()
    End If
End Sub
End Class

但在这段代码中:

Dim SMSEngine As New SMSCOMMS("COM5")

我需要知道我的调制解调器 gsm 连接了哪个 com 端口,所以我需要可以自动找到 com 端口的代码,有人可以帮我吗?

非常感谢你。
注意:我使用 Visual Basic 2012 (vb.net)

4

3 回答 3

0

无需在程序界面中选择 COM 端口或硬编码端口号而自动执行此操作的唯一方法是尝试打开所有可用的 COM 端口,并向每个端口发送某种状态命令并侦听(短超时时间)用于只有 GSM 调制解调器会返回的回复。当然,这意味着您可能会向连接到计算机的其他串行设备(如果有)发送奇怪的命令,但我猜在您的情况下,它将是唯一连接任何东西的串行端口。

获取当前计算机上的串口名称列表的功能请参见此处。

于 2012-10-04T14:56:59.077 回答
0

就像其他人说的那样,您必须循环所有端口并检查是否可以使用速率/端口连接到 GSM 调制解调器。这是一个库,它是 c#,但您可以下载源代码以查看它是如何工作的。

http://www.scampers.org/steve/sms/libraries.htm

于 2012-10-04T16:34:15.713 回答
0

这是在 vb.net 中查找附加调制解调器及其属性的代码。

Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_POTSModem")
                ComboBox1.Items.Clear()
                For Each queryObj As ManagementObject In searcher.Get()
                    If queryObj("Status") = "OK" Then
                        modems = queryObj("Description")
                        baud = queryObj("MaxBaudRateToSerialPort")
                        Port = queryObj("AttachedTo")
                         da.rows.add(1)
                        da.rows(incount).item(1)=Port 
                        da.rows(incount).item(2)=baud
                        ComboBox1.Items.Add(modems)
                        ComboBox1.SelectedIndex = ComboBox1.FindString("Nokia")
                        If ComboBox1.SelectedItem = "" Then
                            ComboBox1.SelectedIndex = ComboBox1.FindString("Samsung")
                            If ComboBox1.SelectedItem = "" Then
                                ComboBox1.SelectedIndex = 0
                            End If
                        End If
                    End If
                 intcount+=1
                Next

您需要添加对 System.management 程序集的引用并导入命名空间 system.management。

于 2014-12-06T05:30:06.383 回答