daiiniel
Здравствуйте, я использую эту библиотеку, сделанную мной, я надеюсь, что это может помочь вам, в любом случае, если у вас есть какие-либо проблемы или сомнения по поводу кода, Вы можете сообщить мне,
Итак, это код:
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Imports System.Threading
Public Class smsclass
Private WithEvents Serie As New SerialPort
Private ReadThread As Thread
#Region "PROPIEDADES"
Private CONTSMS As Boolean '// Receives ">" that means that you have to write the body of the SMS
Private ENDSMS As Boolean '// Receive "+CMGS" that means that the SMS has been succesfully sent
Private ERRORSMS As Boolean '// It becomes true when an error occurred
#End Region
#Region "EVENTS"
Public Event DataReceived(ByVal Message As String) 'Data received from the Serial port
Public Event SMSSended(ByVal NumTelefono As String, ByVal Content As String, ByVal Id As String) 'SMS has been succesfully sent
Public Event SMSError(ByVal Message As String) 'There was an error sending the SMS
#End Region
#Region "PROPERTIES"
'Check the Serial Port Status
Public ReadOnly Property IsOpen() As Boolean
Get
If Serie.IsOpen = True Then
IsOpen = True
Else
IsOpen = False
End If
End Get
End Property
#End Region
#Region "PUBLIC METHODS"
'Public Method for Open the serial Port
Public Sub Open(ByVal PortName As String, ByVal Speed As String)
If IsOpen = False Then
'Serial Port configuration
Serie.PortName = PortName
Serie.BaudRate = Speed
Serie.DataBits = 8
Serie.Parity = IO.Ports.Parity.None
Serie.Handshake = IO.Ports.Handshake.RequestToSend
Serie.RtsEnable = True
Serie.DtrEnable = True
ReadThread = New Thread(AddressOf Read)
'Open the Serial Port and Start thread to read the port serial incoming data
Try
Serie.Open()
ReadThread.Start()
Catch ex As Exception
End Try
Else
End If
End Sub
'Method to close serial port
Public Sub Close()
If IsOpen = True Then
Serie.Close()
End If
ReadThread.Abort()
End Sub
'Method to Send a SMS
Public Sub MandarSMS(ByVal ThelephoneNumber As String, ByVal Content As String, ByVal Id As String)
'// States of the SMS progres to the initial value
CONTSMS = False
ENDSMS = False
ERRORSMS = False
If IsOpen = True Then
Serie.WriteLine("AT+CMGS=" & ThelephoneNumber & vbCr) '// Destinatario
While CONTSMS = False
System.Threading.Thread.Sleep(30)
End While
If (CONTSMS = True) Then
Serie.WriteLine(Content & vbCrLf & Chr(26)) '// SMS CONTENT
End If
While (ENDSMS = False)
System.Threading.Thread.Sleep(30)
'THE MESSAGE COULD NOT BE DELIVERED
If ERRORSMS = True Then
RaiseEvent SMSError(ThelephoneNumber) 'SMS ERROR
Exit While
End If
End While
If ENDSMS = True Then
RaiseEvent SMSSended(ThelephoneNumber, Content, Id) 'SMS WAS SENT
End If
' Restart SMS status vars
ENDSMS = False
CONTSMS = False
ERRORSMS = False
End If
System.Threading.Thread.Sleep(1000)
End Sub
#End Region
#Region "PRIVATE METHODS"
'Leer Puerto Serie
Private Sub Read()
Dim Incoming As String = Nothing
While Serie.IsOpen = True
If (Serie.BytesToRead <> 0) And (IsOpen = True) Then
While Serie.BytesToRead <> 0
System.Threading.Thread.Sleep(30)
Incoming = Serie.ReadExisting()
If Incoming.Contains(">") Then
CONTSMS = True
End If
If Incoming.Contains("+CMGS:") Then
ENDSMS = True
End If
If Incoming.Contains("ERROR") Then
ERRORSMS = True
End If
RaiseEvent DataReceived(Incoming)
End While
End If
System.Threading.Thread.Sleep(300)
End While
End Sub
#End Region
End Class