📄 modem.vb
字号:
#Region "Private Functions"
''' <summary>
''' Get device manufacturer
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetManufacturer() As String
Try
Dim response() As String = ParseATResponse(serialDriver.SendCmd(ATHandler.MANUFACTURER_COMMAND))
Return response(0)
Catch ex As System.Exception
Throw New InvalidCommandException(ex.Message, ex)
End Try
End Function
''' <summary>
''' Get device revision
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetRevision() As String
Try
Dim response As String = serialDriver.SendCmd(ATHandler.REVISION_COMMAND, ATHandler.RESPONSE_OK)
Return response.Replace(ATHandler.RESPONSE_OK, "").Trim
Catch ex As System.Exception
Throw New InvalidCommandException(ex.Message, ex)
End Try
End Function
#End Region
#Region "Protected Functions"
''' <summary>
''' Parse response from serial port into a 2 dimensional string array
''' </summary>
''' <param name="response"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function ParseSplitATResponse(ByVal response As String) As String(,)
Dim rows() As String
Dim cols() As String
Dim results(1, 1) As String
Dim i, j As Integer
Dim rowCount, colCount As Integer
rowCount = 0
colCount = 0
rows = response.Split(ControlChars.CrLf)
For i = 0 To rows.Length - 1
If (rows(i).Trim = String.Empty) Then Continue For
cols = rows(i).Split(",")
If (i = 0) Then
ReDim results(rows.Length, cols.Length)
End If
colCount = 0
For j = 0 To cols.Length - 1
If (cols(j).Trim() <> String.Empty) Then
results(rowCount, colCount) = cols(j)
colCount = colCount + 1
End If
Next
rowCount = rowCount + 1
Next
ParseSplitATResponse = results
End Function
''' <summary>
''' Parse response from serial port into a string array
''' </summary>
''' <param name="response"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Function ParseATResponse(ByVal response As String) As String()
Dim rows() As String
Dim results(1) As String
Dim i As Integer
Dim rowCount As Integer
rowCount = 0
rows = response.Split(ControlChars.CrLf)
ReDim results(rows.Length)
For i = 0 To rows.Length - 1
If (rows(i).Trim = String.Empty) Then Continue For
results(rowCount) = rows(i).Trim()
rowCount = rowCount + 1
Next
' TODO, extra checking here
If rowCount = 0 Then
ReDim Preserve results(0)
ElseIf rowCount > 0 Then
ReDim Preserve results(rowCount - 1)
End If
Return results
End Function
#End Region
#Region "Public Functions"
Public Function ClearLog() As Boolean
ClearLog = True
End Function
Public Function ClearLog(ByVal keepLinesInLog As Integer) As Boolean
ClearLog = True
End Function
''' <summary>
''' Auto detect GSM modem
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function AutoDetect() As Boolean
Try
Dim modems() As Modem = GetAvailableModems()
Dim bluetooth, gprs, infrared, serial As Modem
bluetooth = Nothing
gprs = Nothing
infrared = Nothing
serial = Nothing
For Each modem As Modem In modems
If (modem.Name.ToLower.Contains("bluetooth modem")) Then
bluetooth = modem
End If
If (modem.Name.ToLower.Contains("gprs")) Then
gprs = modem
End If
If (modem.Name.ToLower.Contains("infrared")) Then
infrared = modem
End If
If (modem.Name.ToLower.Contains("serial")) Then
serial = modem
End If
Next
If Not bluetooth Is Nothing Then
Me.strName = bluetooth.Name
Me.Port = bluetooth.Port
Me.BaudRate = bluetooth.BaudRate
Me.strModel = bluetooth.Model
Return True
End If
If Not gprs Is Nothing Then
Me.strName = gprs.Name
Me.Port = gprs.Port
Me.BaudRate = gprs.BaudRate
Me.strModel = gprs.Model
Return True
End If
If Not infrared Is Nothing Then
Me.strName = infrared.Name
Me.Port = infrared.Port
Me.BaudRate = infrared.BaudRate
Me.strModel = infrared.Model
Return True
End If
If Not serial Is Nothing Then
Me.strName = serial.Name
Me.Port = serial.Port
Me.BaudRate = serial.BaudRate
Me.strModel = serial.Model
Return True
End If
Return False
Catch ex As System.Exception
Throw New GeneralException(ex.Message, ex)
End Try
End Function
''' <summary>
''' Auto detect bluetooth modem.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function AutoDetectBluetooth() As Boolean
Try
Dim modems() As Modem = GetAvailableModems()
For Each modem As Modem In modems
If (modem.Name.ToLower.Contains("bluetooth modem")) Then
Me.strName = modem.Name
Me.Port = modem.Port
Me.BaudRate = modem.BaudRate
Me.strModel = modem.Model
AutoDetectBluetooth = True
Exit Function
End If
Next
AutoDetectBluetooth = False
Catch ex As System.Exception
Throw New GeneralException(ex.Message, ex)
End Try
End Function
''' <summary>
''' Auto detect infrared modem
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function AutoDetectInfrared() As Boolean
Try
Dim modems() As Modem = GetAvailableModems()
For Each modem As Modem In modems
If (modem.Name.ToLower.Contains("infrared")) Then
Me.strName = modem.Name
Me.Port = modem.Port
Me.BaudRate = modem.BaudRate
Me.strModel = modem.Model
AutoDetectInfrared = True
Exit Function
End If
Next
AutoDetectInfrared = False
Catch ex As System.Exception
Throw New GeneralException(ex.Message, ex)
End Try
End Function
''' <summary>
''' Get available modems
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetAvailableModems() As Modem()
Try
Dim query As New SelectQuery("CIM_PotsModem")
' ManagementObjectSearcher retrieves a collection of WMI objects based on
' the query.
Dim search As New ManagementObjectSearcher(query)
' Display each entry for Win32_processor
Dim info As ManagementObject
Dim modems() As Modem
ReDim modems(search.Get().Count - 1)
Dim counter As Integer
counter = 0
For Each info In search.Get()
Dim propertyDataCollection As PropertyDataCollection = info.Properties()
Dim modem As New Modem
modem.strName = info("Name").ToString()
modem.Port = info("AttachedTo").ToString()
modem.BaudRate = CInt(info("MaxBaudRateToSerialPort"))
modem.strModel = info("Model").ToString()
modems(counter) = modem
counter = counter + 1
Next
GetAvailableModems = modems
Catch ex As System.Exception
Throw New GeneralException(ex.Message, ex)
End Try
End Function
''' <summary>
''' Send heart beat to keep connection alive
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function KeepAlive() As Boolean
Try
If serialDriver.IsOpen Then
serialDriver.SendCmd(ATHandler.KEEP_ALIVE_COMMAND)
End If
Return True
Catch ex As System.Exception
Return False
End Try
End Function
#End Region
#Region "Public Procedures"
''' <summary>
''' Connect to the serial port
''' </summary>
''' <remarks></remarks>
Public Sub Connect()
Try
If (serialDriver.IsOpen) Then
serialDriver.Close()
End If
serialDriver.Open()
' Check if there is a need to enter a PIN
If Me.PIN <> String.Empty Then
Dim responses() As String = ParseATResponse(serialDriver.SendCmd(ATHandler.CPIN_COMMAND & "?"))
If Not responses Is Nothing And responses.Length > 0 Then
If Not responses(0) Is Nothing Then
If responses(0).IndexOf(ATHandler.RESPONSE_PIN_REQUIRED) >= 0 Then
' PIN is needed
responses = ParseATResponse(serialDriver.SendCmd(ATHandler.CPIN_COMMAND & "=" & Me.PIN))
If Not responses Is Nothing And responses.Length > 0 Then
If Not responses(0) Is Nothing Then
If Not responses(0).IndexOf(ATHandler.RESPONSE_OK) Then
Throw New InvalidOpException("Unable to login using the PIN provided.")
End If
End If
End If
End If
End If
End If
End If
Me.Echo = False
If (Not String.IsNullOrEmpty(strModemInitString)) Then
Dim response As String = serialDriver.SendCmd(strModemInitString)
Dim results() As String = ParseATResponse(response)
End If
Catch ex As InvalidOperationException
Throw New ConnectionException(ex.Message, ex)
Catch ex As ArgumentOutOfRangeException
Throw New ConnectionException(ex.Message, ex)
Catch ex As ArgumentException
Throw New ConnectionException(ex.Message, ex)
Catch ex As IO.IOException
Throw New ConnectionException(ex.Message, ex)
Catch ex As UnauthorizedAccessException
Throw New ConnectionException(ex.Message, ex)
Catch ex As Exception
Throw New ConnectionException(ex.Message, ex)
End Try
End Sub
''' <summary>
''' Disconnect serial port
''' </summary>
''' <remarks></remarks>
Public Sub Disconnect()
If serialDriver.IsOpen Then
serialDriver.Close()
'serialPort = Nothing
End If
End Sub
#End Region
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -