Sending And Receiving SMS From GSM modem - vb.net

I am trying to send message from GSM modem. I can submit AT commands the response is OK without any ERRORS. But the problem is I can't send message or read message.
I have implemented 3 functions:
Connect to port
Read SMS
Send SMS
Handles
1. Connect To Port:
Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
If SerialPort1.IsOpen Then
SerialPort1.Close()
BtnConnect.Text = "Connect"
Else
Try
With SerialPort1
.PortName = Trim(Mid(ComboBox1.Text, 1, 5))
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = Ports.StopBits.One
.Handshake = Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
.Open()
.WriteLine("AT+CNMI=1,2,0,0,0" & vbCrLf) 'send whatever data that it receives to serial port
End With
BtnConnect.Text = "Disconnect"
Catch ex As Exception
BtnConnect.Text = "Connect"
MsgBox(ex.Message)
End Try
End If
End Sub
2. Read SMS
Private Sub btn_read_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_read.Click
Try
SerialPort1.WriteLine("AT" & vbCrLf) 'is modem okay?
Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGF=1" & vbCrLf) 'To format SMS as a TEXT message
Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CPMS=""SM""" & vbCrLf) ' Select SIM storage
Threading.Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGL=""REC UNREAD""" & vbCrLf) 'read unread messages
Threading.Thread.Sleep(1000)
SerialPort1.WriteLine("AT+CMGL=""ALL""" & vbCrLf) 'print all message
Threading.Thread.Sleep(1000)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
3. Send SMS
Private Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_send.Click
Try
With SerialPort1
.WriteLine("AT" & vbCrLf)
Threading.Thread.Sleep(1000)
.WriteLine("AT+CMGF=1" & vbCrLf) 'Instruct the GSM / GPRS modem to operate in SMS text mode
Threading.Thread.Sleep(1000)
.WriteLine("AT+CMGS=""9802100355""" & vbCr) 'sender ko no. rakhne ho tyo txtnumber ma
Threading.Thread.Sleep(1000) 'thapeko
.WriteLine("This is test message" & vbCrLf & Chr(26)) 'txtmessage automatic huna parchha haina?
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
4. Handles for Data received in Serial Port
Private Sub serialport1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Pause while all data is read
System.Threading.Thread.Sleep(300)
rcvdata = SerialPort1.ReadExisting()
MsgBox(rcvdata, , "Response From AT")
rcvdata = ""
End Sub
Where did I miss anything? While sending SMS I get CMS 500 error. With the software from the modem I am able to read and send sms. But I need to implement my own in my software.

There may several cause of this error. First check your network. Second set Message service center number using AT commands and save this setting. Hope this will help you

In your second function, you can try declaring a string variable to receive the data, like this:
With serialport1
rcvdata=""
.Write(All AT commands)
Threading.Thread.Sleep(1000)
Msgbox(rcvdata.Tostring)
End With
You can add a handler to datareceived to read all the bytes:
Dim entrada As String = " "
Dim numeros As Integer = SerialPort1.BytesToRead
For i As Integer = 1 To numeros
entrada&= Chr(SerialPort1.ReadChar)
Next
chama(entrada)
Private Sub chama(ByVal dados As String)
rcvdata &= dados
End Sub

AT+CMGS=""9802100355"
Your Phone number is wrong thats why you getting error 500, you need to enter full phone number including 0 at the front.

Related

sending sms message with vb and modem 3g

Hello guys I'm created a windows form that has a basic function of sending sms message to a phone by using my 3g modem. My modem 3g has load that will be use for texting. I check my device manager and I saw my modem 3g is already connected but Unfortunately, it's not sending sms message to my phone's number. here's my code so far. By the way my portname of device is com4
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim ports As String() = SerialPort.GetPortNames
Dim port As String
For Each port In ports
ComboBox1.Items.Add(port)
Next port
ComboBox1.SelectedIndex = 0
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = 9600
.Parity = Parity.None
.StopBits = StopBits.One
.DataBits = 8
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
.Open()
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
If SerialPort1.IsOpen Then
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
.Write(RichTextBox1.Text & Chr(26))
MsgBox("Succesfully Send!")
End With
Else
MsgBox("Port not open!")
End If
Catch ex As Exception
End Try
End Sub
End Class

Send data to Honeywell Xenon 1902 barcode reader via virtual com port

I am trying to send a query to the Honeywell Xenon 1902 barcode scanner. The scanner is connected via virtual com port.
Setting up the communication works fine:
With SerialPort1
If Not .IsOpen Then
Try
.PortName = "Com9"
.BaudRate = 115200
.DataBits = 8
.Parity = Parity.None
.StopBits = StopBits.One
.Handshake = Handshake.None
.DtrEnable = False
.RtsEnable = False
.Open()
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Fehler beim Öffnen des COM Ports", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End With
When I press manually the button for scanning I receive the data of reading from the scanner:
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
Dim sp As SerialPort = CType(sender, SerialPort)
PufferString = sp.ReadExisting
MsgBox(PufferString)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Fehler beim Empfangen", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Now I would like to send the query command "cbr?." from the Honeywell Documentation to the scanner and receive the answer. If I do this on the Honeywell WebInterface it all works fine:
Screenshot from the Honeywell Web Interface Terminal
So my problem is that I am unable to send commands to the scanner neither via Tera Term or any other terminal nor via my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim BefehlsString As String = "cbr?."
Dim enc As System.Text.Encoding = New System.Text.ASCIIEncoding()
Try
Dim ByteArray() As Byte ' Oder String in ...
ByteArray = enc.GetBytes(BefehlsString & vbCr) ' ... Einzelbytes umwandeln
SerialPort1.BaseStream.Write(ByteArray, 0, ByteArray.Length) ' Einzelbytes senden
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Fehler beim Senden", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Due to kunif tip I read the Honeywell Documentation again and I solved my problem:
The command need the prefix "SYN M CR" (ASCII 22,77,13) --> "SYNMCRcbr?." has to be send to the scanner via serial connection.
This is the code I send to the scanner:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim BefehlsString As String = Chr(&H16) & "M" & Chr(&HD) & "cbr?."
serialport.WriteLine(BefehlsString)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace, "Fehler beim Senden", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Then I get an answer as defined in the documentation.
Perhaps, removing the "vbCr" at the end of the command may work.
There is a CR code in the prefix of "Menu Command Syntax" on page 11-1 of Area-Imaging Scanner User's Guide, but there is no CR code in "cbr?." of "Examples of Query Commands" on page 11-3.
Alternatively, you can investigate what kind of communication is occurring using software/hardware called SerialPort/USB protocol monitor/sniffer.

tcp connection with machine via ethernet using vb.net

I want to make a simple formapp using vb.net to send ASCII strings to a machine, that responds after every command. i.e., if i send "AA" it responds "123".
I can already open the connection cause if i check the connection with hyperterminal it says that the port is already being used. Yet, when i send commands i have no answer back.
I kinda adapted the template codes for server/client chat with tcp:
Imports System.Net.Sockets
Imports System.Threading
Dim Listener As New TcpListener(65535)
Dim Client As New TcpClient
Dim Message As String = "
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
RichTextBox1.ForeColor = Color.Black
RichTextBox1.Text += Message + vbCrLf
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSend.Click
If txtName.Text = "" Or cmbAddress.Text = "" Then
MessageBox.Show("All Fields must be Filled", _
"Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Try
Client = New TcpClient(cmbAddress.Text, 65535)
Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(txtName.Text & " Says: " & txtmessage.Text)
Writer.Flush()
RichTextBox1.Text += (txtName.Text & " Says: " & txtmessage.Text) + vbCrLf
txtmessage.Text = ""
Catch ex As Exception
Console.WriteLine(ex)
Dim Errorresult As String = ex.Message
MessageBox.Show(Errorresult & vbCrLf & vbCrLf & _
"Please Review Client Address", _
"Error Sending Message", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
is there a muuuuuuuch better way to do this? i read about .net.sockets but i coulndt get out of this.
and the manuals say nothing about how the machine operates. if it's a client, server, host.. can someone help me here? thank you !
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.UTF7Encoding
Public Class Form1
Dim open As TcpListener
Dim server As TcpListener
Dim client, host As TcpClient
Dim dati, hdat As NetworkStream
Dim ip As String = Nothing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ip = TextBox1.Text
client = New TcpClient
client.Connect(ip, 2101)
If client.Connected = True Then
dati = client.GetStream
MsgBox("Connected", MsgBoxStyle.Information)
Button1.Enabled = False
Timer1.Start()
Else
MsgBox("Error", MsgBoxStyle.Critical)
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If client.Available > 0 Then
Dim t(client.Available - 1) As Byte
dati.Read(t, 0, t.Length)
Dim testo As String = UTF7.GetString(t)
RichTextBox1.Text += vbCrLf & "server : " & testo & vbCrLf
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim t() As Byte = UTF7.GetBytes(TextBox2.Text & vbCrLf)
dati.Write(t, 0, t.Length)
RichTextBox1.Text += vbCrLf & TextBox2.Text & vbCrLf
End Sub
End Class

SMS Sending Application in VB.net

I'm trying to develop an application with vb.net 2008 to send sms to phone numbers from my pc. I've connected my nokia phone to my pc using USB cable (connected with COM3 port). Given bellow code which I have written should work but message is not being sent and I'm getting my app as deadlock condition:
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim ports As String() = SerialPort.GetPortNames
Dim port As String
For Each port In ports
ComboBox1.Items.Add(port)
Next port
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = 9600
.Parity = Parity.None
.StopBits = StopBits.One
.DataBits = 8
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
.Open()
MsgBox("Connected !", MsgBoxStyle.Information)
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Try
If SerialPort1.IsOpen Then
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
.Write(RichTextBox1.Text & Chr(26))
.Close()
MsgBox("Message Sent!", MsgBoxStyle.Information)
End With
Else
MsgBox("Error on port")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Public Function sendMsg(ByVal port As SerialPort, ByVal PhoneNo As String, ByVal Message As String) As Boolean
Dim isSend As Boolean = False
Try
Dim recievedData As String = ExecCommand(port,"AT", 300, "No phone connected")
recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.")
Dim command As String = "AT+CMGS=""" & PhoneNo & """"
recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo")
command = Message & Char.ConvertFromUtf32(26) & vbCr
recievedData = ExecCommand(port,command, 3000, "Failed to send message") '3 seconds
If recievedData.EndsWith(vbCrLf & "OK" & vbCrLf) Then
isSend = True
ElseIf recievedData.Contains("ERROR") Then
isSend = False
End If
Return isSend
Catch ex As Exception
Throw ex
End Try
End Function
ExecCommand
Public Function ExecCommand(ByVal port As SerialPort, ByVal command As String, ByVal responseTimeout As Integer, ByVal 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(vbCrLf & "> "))) AndAlso ((Not input.EndsWith(vbCrLf & "OK" & vbCrLf)))) Then
Throw New ApplicationException("No success message was received.")
End If
Return input
Catch ex As Exception
Throw ex
End Try
End Function
You can also try this coding..
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("COM17")
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
Application.DoEvents()
If SMSPort.IsOpen = True Then
'sending AT commands
SMSPort.WriteLine("AT")
Thread.Sleep(500)
SMSPort.WriteLine("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)
Thread.Sleep(500)
' SMSPort.WriteLine("AT+CSCA=""+919822078000""" & vbCrLf) 'set service center address (which varies for service providers (idea, airtel))
SMSPort.WriteLine("AT+CMGS=" & """" & Form1.TextBox1.Text & """" & vbCrLf) ' enter the mobile number whom you want to send the SMS
_ContSMS = False
Thread.Sleep(500)
SMSPort.WriteLine(Form1.TextBox2.Text & Chr(26)) 'SMS sending(specify the labl and the text box)
MessageBox.Show(":send")
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
you have to connect your phone with PC Suite or Bluetooth or using USB cable.
After that you have to check the port number and change in coding part according to your port number.
Dim SMSEngine As New SMSCOMMS("COM17")
It is working for me..

Row Scanning and Delay for Automatic Sms Sending in VB2012

I have a MS SQL Linked-Server (from Mysql) - generated Table TBL containing
*Index
*StdNo
*MobileNo
And I have a VB2012 code for SMS sending:
Private Sub cmdsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsend.Click
Try
If SerialPort1.IsOpen Then
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
.Write(RichTextBox1.Text & Chr(26))
End With
Else
MsgBox("Error on the port selected")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Where TextBox1.Text is the textbox where number is actually manually inputted
and RichTextBox1.Text is the message that will be sent.
What I wanted is to create a code that will automatically select each row of the table TBL for the MobileNo and then sends SMS. Since the VB code is not really for Bulk messaging, my idea is to create a loop that will scan each row, get each MobileNo, sends the message, then delay for a few second before the next row scan. I don't even know if it is possible. But I think it is, I just don't know how to come up with the loop and I don't even know how to create the connection between the database. Does anybody have any idea how to accomplish this?
may be you can use this code:
Private Sub cmdsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsend.Click
Try
If SerialPort1.IsOpen Then
With SerialPort1
.Write("AT" & vbCrLf)
.Write("AT+CMGF=1" & vbCrLf)
.Write("AT+CMGS=" & Chr(13)
.Write(TextBox1.Text)
.Write(Chr(26))
End With
Else
MsgBox("Error on the port selected")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub