Sms pdu encoding class. You can use Comm to connect to the modem to conveniently send text messages.

Source: Internet
Author: User

Many people on the internet use the COM port to connect to the mobile phone, use the mobile phone modem, use the AT command to send text messages, introduce the principles of PDU encoding, and write a ready-made class for reference and use.

Smspduclass. CLs

Option explicit

'Preserve the local variable of the attribute value
Private mvarsmsclen as integer local Replication
Private mvarsmsctype as string' local copy
Private mvarsmsc as string' local copy
Private mvarmsghead as integer local copy
Private mvartpmr as integer local Replication
Private mvardestphonenumlen as integer 'local Replication
Private mvardestphonenumtype as string 'local copy
Private mvardestphonenum as string 'local copy
Private mvartppid as integer local copy
Private mvartpdsc as integer local Replication
Private mvartpvp as integer local copy
Private mvarmsglen as integer local Replication
Private mvarmsgcontent as string 'local copy
Private mvarpdulen as integer local Replication
Private mvarpdu as string 'local copy
'To raise this event, follow the following syntax to use raiseevent:
'Raiseevent validresult [(arg1, arg2,..., argn)]
Public event validresult (byval errorcode as integer, byval errorstring as string)

Public Function genpdu (optional byval smscontent as string ,_
Optional byval destno as string ,_
Optional byval serviceno as string) as string

'Mvarsmsclen = 0
'Mvarsmsctype = ""
'Mvarsmsc = ""
'Varmsghead = 11
'Mvartpmr = 0
'Mvardestphonenumlen = 0
'Mvardestphonenumtype = ""
'Mvardestphonenum = ""
'Mvartppid = 0
'Mvartpdsc = 8
'Mvartpvp = 0
'Mvarmsglen = 0
'Mvarmsgcontent = ""
'Mvarpdulen = 0
'Mvarpdu = ""

If Len (smscontent)> 0 then
Mvarmsgcontent = smscontent

End if

If Len (destno)> 0 then
Mvardestphonenum = destno

End if




If Len (serviceno)> 0 then
Mvarsmsc = serviceno
If Len (mvarsmsc)> 14 then
Raiseevent validresult (7, "SMSC error! ")
Mvarsmsc = "+ 8613800769500"
End if
If Len (mvarsmsc) <11 then
Raiseevent validresult (7, "SMSC error! ")
Mvarsmsc = "+ 8613800769500"
End if
Mvarsmsc = "+ 86" & right (mvarsmsc, 11)

End if



If Len (mvardestphonenum) = 0 then
Genpdu = ""
Raiseevent validresult (3, "destphonenumber is null! ")

Exit Function
End if

If mvartpdsc <> 0 and mvartpdsc <> 8 then
Genpdu = ""
Raiseevent validresult (5, "TP-DCS error! ")

Exit Function
End if

Dim servicenumpdu as string
Dim destphonenumpdu as string
Servicenumpdu = mvarsmsc
Destphonenumpdu = mvardestphonenum

'Msg. destphonenumtype: called number type. If there are more than 86, the value is "91"; otherwise, the value is "81"

If Len (mvarsmsc)> 0 then
Formatphonenum servicenumpdu, mvarsmsctype
Mvarsmsclen = Len (servicenumpdu & mvarsmsctype)/2' short message center address length. (Text message center number type + text message center number length/2 hexadecimal representation)

End if

Mvardestphonenumlen = formatphonenum (destphonenumpdu, mvardestphonenumtype) ''the length of the called number. The hexadecimal representation of the called number length.
'
If Len (mvarmsgcontent)> 70 then
Mvarmsgcontent = left (mvarmsgcontent, 70)

End if

'Mvarmsglen = Len (mvarmsgcontent)

Dim smstext as string
Smstext = mvarmsgcontent

'
Smstext = gb2unicode (smstext) 'converts Chinese characters into Unicode hex encoded strings
'

'
Mvarmsglen = Len (smstext)/2

If Len (mvarsmsc) = 0 then
Mvarsmsclen = 0
Mvarpdu = int2hexstr (mvarsmsclen) & int2hexstr (mvarmsghead) & int2hexstr (mvartpmr) & int2hexstr (duration) & mvardestphonenumtype & destphonenumpdu &_
Int2hexstr (mvartppid) & int2hexstr (mvartpdsc) & int2hexstr (mvartpvp) & int2hexstr (mvarmsglen) & smstext

Mvarpdulen = Len (mvarpdu)/2-1

Else
Mvarpdu = int2hexstr (mvarsmsclen) & mvarsmsctype & servicenumpdu & int2hexstr (mvarmsghead) & int2hexstr (mvartpmr) & int2hexstr (SUCCESS) & Success &_
Int2hexstr (mvartppid) & int2hexstr (mvartpdsc) & int2hexstr (mvartpvp) & int2hexstr (mvarmsglen) & smstext


Mvarpdulen = Len (mvarpdu)/2-9 'pdu String Length
End if

Genpdu = mvarpdu

End Function

'Public property let PDU (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. PDU = 5
'Varpdu = vdata
'End Property

Public property get PDU () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. PDU

Call genpdu

PDU = mvarpdu
End Property

 

'Public property let pdulen (byval vdata as integer)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. pdulen = 5
'Mvarpdulen = vdata
'End Property

Public property get pdulen () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. pdulen
Pdulen = mvarpdulen
End Property

 

Public property let msgcontent (byval vdata as string)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. msgcontent = 5
Mvarmsgcontent = vdata
Mvarmsglen = Len (vdata) * 2

End Property

Public property get msgcontent () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. msgcontent
Msgcontent = mvarmsgcontent
End Property

 

'Public property let msglen (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. msglen = 5
'Varmsglen = vdata
'End Property

Public property get msglen () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. msglen
Msglen = mvarmsglen
End Property

 

Public property let tpvp (byval vdata as integer)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. tpvp = 5
If vdata> = 0 and vdata <256 then
Mvartpvp = vdata

Else
Raiseevent validresult (6, "TP-VP error! ")
End if
End Property

Public property get tpvp () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. tpvp
Tpvp = mvartpvp
End Property

 

Public property let tpdcs (byval vdata as integer)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. tpdsc = 5
If vdata> = 0 and vdata <256 then
Mvartpdsc = vdata
Else
Raiseevent validresult (5, "TP-DCS error! ")
End if
End Property

Public property get tpdcs () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. tpdsc
Tpdcs = mvartpdsc
End Property

 

Public property let tppid (byval vdata as integer)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. tppid = 5
If vdata> = 0 and vdata <256 then
Mvartppid = vdata
Else
Raiseevent validresult (4, "TP-PID error! ")
End if
End Property

Public property get tppid () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. tppid
Tppid = mvartppid
End Property

 

Public property let destphonenum (byval vdata as string)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. destphonenum = 5
If Len (vdata) = 0 then
Raiseevent validresult (3, "destphonenumber is null! ")
Else
Mvardestphonenum = vdata
Mvardestphonenumlen = formatphonenum (vdata, mvardestphonenumtype)
End if
End Property

 

 

Public property get destphonenum () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. destphonenum

Destphonenum = mvardestphonenum

End Property

 

'Public property let destphonenumtype (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. destphonenumtype = 5
'Mvardestphonenumtype = vdata
'End Property
'
'
Public property get destphonenumtype () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. destphonenumtype
If Len (mvardestphonenum) = 0 then
Mvardestphonenumtype = "FF"
Else
Dim STR as string
STR = mvardestphonenum
Formatphonenum STR, mvardestphonenumtype

End if
Destphonenumtype = mvardestphonenumtype
End Property

 

'Public property let destphonenumlen (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. destphonenumlen = 5
'Mvardestphonenumlen = vdata
'End Property
'
'
Public property get destphonenumlen () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. destphonenumlen
If Len (destphonenum) = 0 then
Mvardestphonenumlen = 0
Else
Dim STR as string
STR = destphonenum
Mvardestphonenumlen = formatphonenum (STR, mvardestphonenumtype)
End if
Destphonenumlen = mvardestphonenumlen
End Property

 

Public property let tpmr (byval vdata as integer)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. tpmr = 5
If vdata> = 0 and vdata <256 then
Mvartpmr = vdata

Else
Raiseevent validresult (2, "TP-MR error! ")

End if
End Property

Public property get tpmr () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. tpmr
Tpmr = mvartpmr
End Property

 

Public property let msghead (byval vdata as integer)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. msghead = 5
If vdata> = 0 and vdata <256 then
Mvarmsghead = vdata
Else
Raiseevent validresult (1, "msghead error! ")


End if
End Property

Public property get msghead () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. msghead
Msghead = mvarmsghead
End Property

 

Public property let SMSC (byval vdata as string)
'Used to assign a value to an attribute, which is on the left of the value assignment statement.
'Syntax: X. SMSC = 5

If Len (vdata) = 0 then
Mvarsmsclen = 0
Mvarsmsc = vdata
Else
If Len (vdata)> 14 then
Raiseevent validresult (7, "SMSC error! ")
Vdata = "+ 8613800769500"
End if
If Len (vdata) <11 then
Raiseevent validresult (7, "SMSC error! ")
Vdata = "+ 8613800769500"
End if
Vdata = "+ 86" & right (vdata, 11)
Mvarsmsc = vdata
Mvarsmsclen = formatphonenum (vdata, mvarsmsctype)/2
End if

End Property

Public property get SMSC () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. SMSC
SMSC = mvarsmsc
End Property

 

'Public property let smsctype (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. smsctype = 5
'Varsmsctype = vdata
'End Property

Public property get smsctype () as string
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. smsctype
If Len (SMSC) = 0 then
Mvarsmsctype = "FF"
Else
Dim STR as string
STR = SMSC
Formatphonenum STR, mvarsmsctype
End if
Smsctype = mvarsmsctype
End Property

 

'Public property let smsclen (byval vdata as string)
''Is used to assign a value to an attribute, on the left of the value assignment statement.
''Syntax: X. smsclen = 5
'Mvarsmsclen = vdata
'End Property
'
'
Public property get smsclen () as integer
'Used to retrieve attribute values, which is on the right of the value assignment statement.
'Syntax: Debug. Print X. smsclen
If Len (SMSC) = 0 then
Mvarsmsclen = 0

Else
Dim STR as string
STR = SMSC
Formatphonenum STR, mvarsmsctype
Mvarsmsclen = Len (mvarsmsctype & Str)/2

End if
Smsclen = mvarsmsclen
End Property

 

Private sub class_initialize ()

Mvarsmsclen = 0
Mvarsmsctype = ""
Mvarsmsc = ""
Mvarmsghead = 17
Mvartpmr = 0
Mvardestphonenumlen = 0
Mvardestphonenumtype = ""
Mvardestphonenum = ""
Mvartppid = 0
Mvartpdsc = 8
MVP artpvp = 255
Mvarmsglen = 0
Mvarmsgcontent = ""
Mvarpdulen = 0
Mvarpdu = ""


'Msg. msghead = "11" 'header bytes (header byte, which is a bitmask ). Here 11 refers to sending short messages normally.
'Msg. tpmr = "00" 'information reference number. TP-MR)
'Msg. tppid = "00" ''is generally 00, indicating standard SMS messages to the point.
'Msg. tpvp = "FF" ''validity period (TP-VP), message validity period, 00 or FF indicates valid
'Msg. tpdsc = "08" 'user information encoding method (TP-DCS), 7-bit encoding (08: ucs2 encoding Chinese characters are generally 08)


End sub

Private function int2hexstr (byval arg0 as integer) as string
Dim strchar as string
Strchar = ""

Strchar = hex (arg0)
If Len (strchar) <2 then strchar = "0" & strchar
Int2hexstr = strchar
End Function

'The actual number should be 8613805515500 (the letter F indicates the length minus 1 ),
'This is the number of the author's GSM Short Message Center. (The number processing method is as follows: if it is set to + 86, remove the + number,
'Then judge whether it is an even number, instead of adding f at the end, and then swap the odd and even digits)

Public Function formatphonenum (byref phonenum as string, byref tonnpiflag as string) as integer
Dim I as integer
Dim IASC as integer
Dim strchar as string

'If Len (phonenum) = 14 then
'If left (phonenum, 3) = "+ 86" then
'Phonenum = right (phonenum, 11)
'Else
'If Len (phonenum) <> 11 then
'Formatsmsc = 0
'Exit Function
'End if
'End if
'End if
If Len (phonenum) <= 0 then
Formatphonenum = 0
Exit Function
End if
If left (phonenum, 3) = "+ 86" then
Phonenum = right (phonenum, 13)
Tonnpiflag = "91"
Else
'If Len (phonenum) <> 11 then
'Formatsmsc = 0
'Exit Function
'End if
Tonnpiflag = "81"
End if



For I = 1 to Len (phonenum)
Strchar = mid (phonenum, I, 1)
IASC = ASC (strchar)
If IASC> 57 or IASC <48 then
Formatphonenum = 0
Exit Function
End if
Next I
If Len (phonenum) mod 2 <> 0 then
Phonenum = phonenum & "F"
End if

Dim strtmp2, strtmp1 as string
Strtmp1 = ""
For I = 1 to Len (phonenum) Step 2
Strtmp2 = mid (phonenum, I, 2)
Strtmp1 = strtmp1 & right (strtmp2, 1) & left (strtmp2, 1)
Next I
Phonenum = strtmp1
Formatphonenum = Len (phonenum)-1
End Function

Public Function gb2unicode (byval strgb as string) as string

Dim bytea () as byte

Dim I as integer

Dim strtmpunicode as string
Dim stra as string
Dim strb as string

On Error goto errorunicode

I = lenb (strgb)

Redim bytea (1 to I)

For I = 1 to lenb (strgb)
Stra = midb (strgb, I, 1)
Bytea (I) = ASCB (stra)
Next I

'Strgb has been converted to unicode encoding and saved in the array bytea.
'Next, we need to adjust the order and return it as a string.
Strtmpunicode = ""

For I = 1 to ubound (bytea) Step 2
Stra = hex (bytea (I ))
If Len (stra) <2 then stra = "0" & stra
Strb = hex (bytea (I + 1 ))
If Len (strb) <2 then strb = "0" & strb
Strtmpunicode = strtmpunicode & strb & stra
Next I

Gb2unicode = strtmpunicode
Exit Function

Errorunicode:
'Msgbox "error:" & err & "." & vbcrlf & err. Description
Raiseevent validresult (ERR. Number, Err. description)

Gb2unicode = ""
End Function

Usage:

Dim sms1 as new smspduclass

Sms1.destphonenum = "13912345678"
Sms1.smsc = "+ 8613800769500"
Sms1.msgcontent = "AA"

Sendsms sms1.pdu, sms1.pduleni

Public Function sendsms (byval strsmspdu as string, byval smslen as integer) as Boolean
With mscomm1
If. portopen = true then
'Debug. Print now ()
If smslen> 5 then
. Output = "at + cmgf = 0" & vbcr
. Output = "at + cmgs =" & smslen & vbcr
Else
Sendsms = false
Exit Function
End if

If Len (strsmspdu) = 0 then
Sendsms = false
Exit Function
End if
'Debug. Print now ()

Dim I as long
For I = 0 to 10000 Step 1
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents
Doevents

Next
'Debug. Print now ()

. Output = strsmspdu & CHR (26)
Sendsms = true
'Debug. Print now ()
Else

Sendsms = false
Exit Function
End if

End
End Function

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.