Hi all,

I know a little bit of thread but the problem is that I did not know how to implement it to my code. I have this code that send sms but when the sms is send, it sent successfully but the message that is sent is only half the message that i have type. I am using serial port to send sms. Hope that anyone can help.
Thanks.Here is the code that i am using:


Public Class Form1

Private WithEvents serialPort As New IO.Ports.SerialPort
Dim receivedData As String
Private ReadThread As Thread

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'---------------Display all the ports name which available-----------'
For i As Integer = 0 To _
My.Computer.Ports.SerialPortNames.Count - 1
cbbCOMPorts.Items.Add( _
My.Computer.Ports.SerialPortNames(i))
Next
btnDisconnect.Enabled = False
End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
'---close the serial port if it is open---
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
'---configure the various parameters of the serial port---
With serialPort
.PortName = cbbCOMPorts.Text
.BaudRate = 115200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
'-----set the encoding to Unicode-----
.Encoding = System.Text.Encoding.Unicode

End With

'---open the serial port---
serialPort.Open()
'---update the status of the serial port and
' enable/disable the buttons---
lblShow.Text = cbbCOMPorts.Text & " connected."
btnConnect.Enabled = False
btnDisconnect.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
'---close the serial port---
serialPort.Close()
'---update the status of the serial port and
' enable/disable the buttons---
lblShow.Text = serialPort.PortName & " disconnected."
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

serialPort.Write("AT+CMGS=" & RichTextBox1.Text & vbCrLf & TextBox1.Text & Chr(26)) 'set command message format to text mode(1)
MsgBox("Message has been sent")

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
serialPort.Write("AT" & vbCrLf) 'set command message format to text mode(1)

End Sub

Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
serialPort.Write("AT+CMGF=1" & vbCrLf) 'set command message format to text mode(1)

End Sub
End Class

I haven't ever done SMS sending but noticed one point in your code:

serialPort.Write("AT+CMGS=" & RichTextBox1.Text & vbCrLf & TextBox1.Text & Chr(26)) 'set command message format to text

Are you aware that SMS length is limited to 160 ASCII characters or 70 Unicode characters? Does your text exceed these limits? You could also check if setting the BaudRate a bit lower helps anything.

Back to your original question about threads. You declare Private ReadThread As Thread but never use it?

I assume you want a thread running in the background which receives SMS, am I right? Since you're listening only one serial port, you don't achieve anything with threads. Instead of threads take a look at BackgroundWorker Class. It offers you a (single) separate thread running in the background and that should be just what you need.

HTH

Hi terne44,

Regarding about Private ReadThread As Thread, i forgot to delete it.
Yes i understand that the SMS length that is required must not exceed to 160 ASCII character but the problem is my message does not exceeded. For the baud rate, i know that the baud rate depend on the modem that are using. The main thing is that i just want to send the SMS.

As for the code,

serialPort.Write("AT+CMGS=" & RichTextBox1.Text & vbCrLf & TextBox1.Text & Chr(26)) 'set command message format to text

is there any solution for the code?what am i doing,is it right?

is there any solution for the code?

Like I said, I'm not (technically) familiar with sending SMS in VB.NET.

One thing you could do, is to check the actual number of Ascii characters

Dim TempStr As String

TempStr = RichTextBox1.Text & vbCrLf & TextBox1.Text
MsgBox(System.Text.Encoding.ASCII.GetByteCount(TempStr))

serialPort.Write("AT+CMGS=" & TempStr & Chr(26)) 'set command message format to text

I did assume that Substitute character (Chr(26)) is part of sending SMS message, and not part of the message itself. One question, why you get message parts from both rich text box and text box control?

what am i doing,is it right?

I suppose you have some manual for the serial device you're using? You have to check from there if your AT commands are correct. You could also try to check if the device manufacturer has some support forum and post your question there. I think your problem is more HW specific and has not that much to do with VB.NET itself.

You also posted earlier that you manage to send (receive?) a part of the message. If you repeat that, do you always send (receive) the partial message of the same length? Could you also post some sample data of what you were sending and what you received?

HTH

Can you post the full string evaluated by the debugger so we can see the actual text you are trying to send? Also indicate where the message is being cut off. You may have a control character in the buffer you're attempting to write.

I am Beginner to vb.net. i tried to use your code. but i hv some prob.

after i click send button its prompt message box but no any more result.

i want to know where should i add my mobile number which i want to receive sms.

please help me.

thank you in advance.

chandi

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.