📄 atcommandbase.vb
字号:
Dim regexCMS As Regex = New Regex( _
"\+CMS\s*ERROR:\s*(?<CODE>\d+)", _
RegexOptions.IgnoreCase _
Or RegexOptions.Compiled _
Or RegexOptions.RightToLeft _
)
Dim m As Match = regexCMS.Match(buffer)
mLastError = m.Groups("CODE").Value
Case StringToNotifyEnum.CDS
Stop
Case StringToNotifyEnum.CDSI
Dim regexCMS As Regex = New Regex( _
"\+CDSI:\s*""(?<STO>\w*)"",(?<INDEX>\d+)", _
RegexOptions.IgnoreCase _
Or RegexOptions.Compiled _
Or RegexOptions.RightToLeft _
)
Dim m As Match = regexCMS.Match(buffer)
If m.Success = True Then
Dim index As Integer = CInt(m.Groups("INDEX").ToString)
RaiseEvent NewMessageArrived(index, String.Empty)
End If
Case StringToNotifyEnum.CMT
Stop
Case StringToNotifyEnum.CMTI
Dim regexCMS As Regex = New Regex( _
"\+CMTI:\s*""(?<STO>\w*)"",(?<INDEX>\d+)", _
RegexOptions.IgnoreCase _
Or RegexOptions.Compiled _
Or RegexOptions.RightToLeft _
)
Dim m As Match = regexCMS.Match(buffer)
If m.Success = True Then
Dim index As Integer = CInt(m.Groups("INDEX").ToString)
RaiseEvent NewMessageArrived(index, String.Empty)
End If
End Select
ClearInputBuffer()
End Sub
#End Region
#Region "AT command base"
''' <summary>
''' Send a AT command
''' </summary>
''' <param name="command">Command to Send. Will be automatically add VBCRLF after the command</param>
''' <returns>Response of this AT Command</returns>
''' <remarks></remarks>
Public Overloads Function SendCommand(ByVal command As String) As String
mCancel = False
DisableMonitor()
myPhone.WaitOne()
'Clear input buffer
ClearInputBuffer()
mSerialStream.Write(command & vbCrLf)
Dim StartTime As Date = Now
Dim buffer As String
Do
Thread.Sleep(10)
mInputBuffer.Append(mSerialStream.ReadExisting)
buffer = mInputBuffer.ToString
If buffer.IndexOf("OK") >= 0 Then Exit Do
If buffer.IndexOf("ERROR") >= 0 Then Exit Do
If mCancel = True Then
buffer = mCancelResponse
Exit Do
End If
'Simulate time out
If Now.Subtract(StartTime).TotalMilliseconds > mTimeOut Then
Throw New TimeoutException("Send AT command time out")
End If
Loop
myPhone.ReleaseMutex()
EnableMonitor()
buffer = buffer.Replace(command, "")
Return buffer
End Function
Public Overloads Function SendCommand(ByVal command As String, ByVal toWait As String, ByVal commandAfterWait As String) As String
mCancel = False
DisableMonitor()
myPhone.WaitOne()
command = command & vbCrLf
'Clear input buffer
ClearInputBuffer()
'Send byte data
mSerialStream.Write(command)
Dim StartTime As Date = Now
'Simulate TimeOut
Do
Thread.Sleep(1)
mInputBuffer.Append(mSerialStream.ReadExisting)
If mInputBuffer.ToString.IndexOf(toWait) >= 0 Then Exit Do
If mCancel = True Then
Return mCancelResponse
End If
If Now.Subtract(StartTime).TotalMilliseconds > mTimeOut Then
Throw New IOException("Wait for certain string timeout!")
Return String.Empty
End If
Loop
mSerialStream.Write(commandAfterWait)
StartTime = Now
Dim Buffer As String
Do
Thread.Sleep(10)
mInputBuffer.Append(mSerialStream.ReadExisting)
Buffer = mInputBuffer.ToString
If Buffer.IndexOf("OK") >= 0 Then Exit Do
If Buffer.IndexOf("ERROR") >= 0 Then Exit Do
If mCancel = True Then
Buffer = mCancelResponse
Exit Do
End If
If Now.Subtract(StartTime).TotalMilliseconds = mTimeOut Then
Throw New IOException("Wait for term char timeout!")
Return String.Empty
End If
Loop
myPhone.ReleaseMutex()
EnableMonitor()
Return mInputBuffer.ToString
End Function
Public Overloads Function CancelCurrentAction() As Boolean
mCancel = True
DisableMonitor()
EnableMonitor()
Return True
End Function
#End Region
''' <summary>
''' Check if error occur
''' </summary>
''' <param name="inputBuffernputBuffer"></param>
''' <returns>True if no error; False if error</returns>
''' <remarks></remarks>
Public Shared Function CheckResponse(ByVal inputBuffer As String) As Boolean
'##################################
'Function should be extend to hand +CME ERROR
'##################################
If inputBuffer Is Nothing Then Return False
If inputBuffer Is String.Empty Then Return False
If inputBuffer.IndexOf("ERROR") > 0 Then Return False
'When no error return true
Return True
End Function
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
If mSerialStream Is Nothing = False Then
mSerialStream.Close()
End If
If myPhone Is Nothing = False Then
myPhone.Close()
End If
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
''' <summary>
''' Get current input buffer
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>Added at 10/2/2005</remarks>
Public ReadOnly Property InputBuffer() As String
Get
Return mInputBuffer.ToString()
End Get
End Property
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -