2012-10-04 51 views
1

如何使用vb.net找到gsm modem com端口?我寫這個代碼:使用vb.net找到gsm modem 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") 

我需要知道哪個COM口,我的調制解調器GSM進行連接,所以我需要的代碼,可以自動找到COM端口,可以有人幫助我嗎?

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

回答

0

像其他人說的那樣,您必須循環所有端口並檢查您是否可以連接使用速率/端口連接到GSM調制解調器。這是一個庫,它是C#,但你可以下載源代碼,看看它是如何工作的。

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

+0

你能給我一個檢查vb.net中所有端口的代碼嗎? – NPE

+0

檢查我發佈的鏈接,它有源代碼。儘管它在c#中,但直接轉換爲vb.net。但是,它使用不同的庫來檢查連接。庫本身工作正常,你不需要做任何低級編碼。 – urlreader

0

要做到這一點,而無需在程序界面或硬編碼的端口號或者選擇COM端口自動的唯一方法是嘗試打開所有可用的COM端口,併發送每一種狀態命令並監聽(短暫超時),以便只回復GSM調制解調器的回覆。當然,這意味着你可以發送奇怪的命令,其他串行設備,如果有的話,是連接到計算機,但我在你的情況猜測它會隨着附加任何唯一的串行端口。

請參閱here以獲取當前計算機上串行端口名稱列表的功能。

+0

感謝您的回答,但您能更簡單高效嗎?我們可以知道gsm調制解調器使用哪個端口嗎? – NPE

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。