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ポートを自動的に見つけることができるコードが必要です。誰かが私を助けてくれますか?

どうもありがとうございます。
nb: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 に答える