PDA

View Full Version : مبتدی: ارسال sms چند قسمتی با مد PDU



HM2020
چهارشنبه 08 آذر 1391, 04:18 صبح
سلام

من با کد زیر مشکل دارم برای ارسال پیامک فقط یه پیام میتونم ارسال کنم و برای ارسال پیامک بیشتر نمیشه و خطا میده و میگه که بیش از 70 کاراکتر فارسی و 160 کاراکتر انگلیسی مجاز نیست.

من از دی ال ال ( PDUConverter.dll ) و مودم gsm


Imports GsmComm.GsmCommunication
Imports GsmComm.PduConverter
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data

Namespace SMS
Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim retry As Boolean
Dim port As Integer = 1
Dim baudRate As Integer = &H2580
Dim timeout As Integer = 300
Dim dlg As New frmConnection With { _
.StartPosition = FormStartPosition.CenterScreen _
}
dlg.SetData(port, baudRate, timeout)
If (dlg.ShowDialog(Me) = DialogResult.OK) Then
dlg.GetData(port, baudRate, timeout)
CommSetting.Comm_Port = port
CommSetting.Comm_BaudRate = baudRate
CommSetting.Comm_TimeOut = timeout
Else
MyBase.Close()
Return
End If
Cursor.Current = Cursors.WaitCursor
CommSetting.comm = New GsmCommMain(port, baudRate, timeout)
Cursor.Current = Cursors.Default
AddHandler CommSetting.comm.PhoneConnected, New EventHandler(AddressOf Me.comm_PhoneConnected)
AddHandler CommSetting.comm.MessageReceived, New MessageReceivedEventHandler(AddressOf Me.comm_MessageReceived)
Do
retry = False
Try
Cursor.Current = Cursors.WaitCursor
CommSetting.comm.Open()
Cursor.Current = Cursors.Default
tmSendSMS.Start()
Catch exception1 As Exception
Cursor.Current = Cursors.Default
If (MessageBox.Show(Me, "Unable to open the port.", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) = DialogResult.Retry) Then
retry = True
Else
MyBase.Close()
Exit Do
End If
End Try
Loop While retry
End Sub

Private Function GetMessageStorage() As String
Dim storage As String = String.Empty
storage = "SM"
If (storage.Length = 0) Then
Throw New ApplicationException("Unknown message storage.")
End If
Return storage
End Function

Private Sub MessageReceived()
Cursor.Current = Cursors.WaitCursor
Dim storage As String = Me.GetMessageStorage()
'AddHandler CommSetting.comm.PhoneConnected, New EventHandler(AddressOf Me.comm_PhoneConnected)
'AddHandler CommSetting.comm.MessageReceived, New MessageReceivedEventHandler(AddressOf Me.comm_MessageReceived)
Dim messages As DecodedShortMessage() = CommSetting.comm.ReadMessages(PhoneMessageStatus.R eceivedUnread, storage)
Dim message As DecodedShortMessage
For Each message In messages
Me.Output(String.Format("Message status = {0}, Location = {1}/{2}", Me.StatusToString(message.Status), message.Storage, message.Index))
Me.ShowMessage(message.Data)
Me.Output("")
HandleIncommingMessage(message)
Next
Dim c As Integer = messages.Length
Me.Output(String.Format("{0,9} messages read.", c.ToString))
Me.Output("")
End Sub

Private Sub tmSendSMS_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmSendSMS.Tick
Me.Output("Process SMS sendings ... ")
Try
'1- Retrive passwords that is not send
Dim lsUser As New List(Of user)
lsUser = GetNotSents()

MessageReceived()

'2- for each record send sms to destination
For Each u As user In lsUser
'u.SMS = (Now.Ticks Mod 10000).ToString("0000")
If SendPass(u.MobileNo, u.SMS) Then
UpdateUser(u)
End If
Threading.Thread.Sleep(100)
Next

Catch ex As Exception

End Try
End Sub

Public Class user
Public MobileNo As String
Public SMS As String
Public ID As Integer
End Class

Dim ConnStr As String = "Data Source=TEB506\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"

Private Function GetNotSents() As List(Of user)
Dim conn As New SqlClient.SqlConnection(ConnStr)
Dim cmd As New SqlClient.SqlCommand("Select MobileNo, SMS , ID From tblSMS Where IsSend =0", conn)
Dim lsuser As New List(Of user)

conn.Open()
Dim dr As SqlClient.SqlDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
Dim u As New user
u.MobileNo = dr.Item("MobileNo").ToString().Trim
u.SMS = dr.Item("SMS").ToString()
u.ID = dr.Item("ID")

lsuser.Add(u)

End While

End If
conn.Close()
GetNotSents = lsuser
End Function

Private Function SendPass(ByVal MobileNo As String, ByVal SMS As String) As Boolean
Try
Dim pdu As SmsSubmitPdu

Dim alert As Boolean = False
Dim unicode As Boolean = True
If Not (alert OrElse [unicode]) Then
pdu = New SmsSubmitPdu(SMS, MobileNo, "")
Else
Dim dcs As Byte
If Not (alert OrElse Not [unicode]) Then
dcs = DataCodingScheme.NoClass_16Bit
ElseIf Not (Not alert OrElse [unicode]) Then
dcs = DataCodingScheme.Class0_7Bit
ElseIf (alert AndAlso [unicode]) Then
dcs = DataCodingScheme.Class0_16Bit
Else
dcs = DataCodingScheme.NoClass_7Bit
End If
pdu = New SmsSubmitPdu(SMS, MobileNo, dcs)
End If
pdu.RequestStatusReport = True

CommSetting.comm.SendMessage(pdu)

'Dim pdus() As OutgoingSmsPdu = SmartMessaging.SmartMessageFactory.CreateConcatTex tMessage(SMS, MobileNo)

'For Each x As OutgoingSmsPdu In pdus
' CommSetting.comm.SendMessage(x)
'Next

Me.Output("Message sent to " + MobileNo)
Return True
Catch ex As Exception
Me.Output("failed to send message to " + MobileNo + " error : " + ex.Message)
Return False
End Try

End Function

Public Sub HandleIncommingMessage(ByVal message As DecodedShortMessage)
Try
Dim conn As New SqlClient.SqlConnection(ConnStr)
Dim cmd1 As New SqlClient.SqlCommand("INSERT INTO tblSMSLog (Message, Type, [Date], Status) VALUES ('" & message.Data.UserDataText & "', 0, getdate(), '" & StatusToString(message.Status) & "') ", conn)
conn.Open()
cmd1.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
Me.Output("Failed to Update DB , Error: " & ex.Message)
End Try
End Sub

Private Sub UpdateUser(ByVal u As user)
Try
Dim conn As New SqlClient.SqlConnection(ConnStr)
Dim cmd As New SqlClient.SqlCommand("UPDATE tblSMS set IsSend = 1 , SendID=ident_current('tblSMSLog') Where ID=" & u.ID, conn)
Dim cmd1 As New SqlClient.SqlCommand("INSERT INTO tblSMSLog (Message, Type, [Date], Status) VALUES ('" & u.SMS & "', 1, getdate(), 'Sent') ", conn)
conn.Open()
cmd1.ExecuteNonQuery()
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
Me.Output("Failed to Update DB , Error: " & ex.Message)
End Try
End Sub


End Class
End Namespace

برای ارسال پیام در چند قسمت و چسباندن آن در گوشی گیرنده پیام باید چه کنم ؟؟