Code VB 2010 - Send SMS via AT command
-
Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.IO.Ports Imports System.Threading Imports System.Text.RegularExpressions Public Class clsSMS #Region "Open and Close Ports" 'Open Port Public Function OpenPort(p_strPortName As String, p_uBaudRate As Integer, p_uDataBits As Integer, p_uReadTimeout As Integer, p_uWriteTimeout As Integer) As SerialPort receiveNow = New AutoResetEvent(False) Dim port As New SerialPort() Try port.PortName = p_strPortName 'COM1 port.BaudRate = p_uBaudRate '9600 port.DataBits = p_uDataBits '8 port.StopBits = StopBits.One '1 port.Parity = Parity.None 'None port.ReadTimeout = p_uReadTimeout '300 port.WriteTimeout = p_uWriteTimeout '300 port.Encoding = Encoding.GetEncoding("iso-8859-1") AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived) port.Open() port.DtrEnable = True port.RtsEnable = True Catch ex As Exception Throw ex End Try Return port End Function 'Close Port Public Sub ClosePort(port As SerialPort) Try port.Close() RemoveHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived) port = Nothing Catch ex As Exception Throw ex End Try End Sub #End Region 'Execute AT Command Public Function ExecCommand(port As SerialPort, command As String, responseTimeout As Integer, errorMessage As String) As String Try port.DiscardOutBuffer() port.DiscardInBuffer() receiveNow.Reset() port.Write(command & vbCr) Dim input As String = ReadResponse(port, responseTimeout) If (input.Length = 0) OrElse ((Not input.EndsWith(vbCr & vbLf & "> ")) AndAlso (Not input.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf))) Then Throw New ApplicationException("No success message was received.") End If Return input Catch ex As Exception Throw ex End Try End Function 'Receive data from port Public Sub port_DataReceived(sender As Object, e A
-
Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.IO.Ports Imports System.Threading Imports System.Text.RegularExpressions Public Class clsSMS #Region "Open and Close Ports" 'Open Port Public Function OpenPort(p_strPortName As String, p_uBaudRate As Integer, p_uDataBits As Integer, p_uReadTimeout As Integer, p_uWriteTimeout As Integer) As SerialPort receiveNow = New AutoResetEvent(False) Dim port As New SerialPort() Try port.PortName = p_strPortName 'COM1 port.BaudRate = p_uBaudRate '9600 port.DataBits = p_uDataBits '8 port.StopBits = StopBits.One '1 port.Parity = Parity.None 'None port.ReadTimeout = p_uReadTimeout '300 port.WriteTimeout = p_uWriteTimeout '300 port.Encoding = Encoding.GetEncoding("iso-8859-1") AddHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived) port.Open() port.DtrEnable = True port.RtsEnable = True Catch ex As Exception Throw ex End Try Return port End Function 'Close Port Public Sub ClosePort(port As SerialPort) Try port.Close() RemoveHandler port.DataReceived, New SerialDataReceivedEventHandler(AddressOf port_DataReceived) port = Nothing Catch ex As Exception Throw ex End Try End Sub #End Region 'Execute AT Command Public Function ExecCommand(port As SerialPort, command As String, responseTimeout As Integer, errorMessage As String) As String Try port.DiscardOutBuffer() port.DiscardInBuffer() receiveNow.Reset() port.Write(command & vbCr) Dim input As String = ReadResponse(port, responseTimeout) If (input.Length = 0) OrElse ((Not input.EndsWith(vbCr & vbLf & "> ")) AndAlso (Not input.EndsWith(vbCr & vbLf & "OK" & vbCr & vbLf))) Then Throw New ApplicationException("No success message was received.") End If Return input Catch ex As Exception Throw ex End Try End Function 'Receive data from port Public Sub port_DataReceived(sender As Object, e A
? A big code dump. So what? What's the problem?