使用vb.net查找gsm调制解调器com端口

时间:2012-10-04 13:33:05

标签: vb.net

如何使用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端口的代码,有人可以帮帮我吗?

非常感谢你。
nb:我使用visual basic 2012(vb.net)

3 个答案:

答案 0 :(得分:0)

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

有关获取当前计算机上串行端口名称列表的函数,请参阅here

答案 1 :(得分:0)

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

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

答案 2 :(得分: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。