网上有很多利用com口连接手机,利用手机modem,使用at指令发送短信,介绍pdu编码的原理很多,写一个现成的类出来,给有需要的人参考和使用。
smspduclass.cls
option explicit
'保持属性值的局部变量
private mvarsmsclen as integer '局部复制
private mvarsmsctype as string '局部复制
private mvarsmsc as string '局部复制
private mvarmsghead as integer '局部复制
private mvartpmr as integer '局部复制
private mvardestphonenumlen as integer '局部复制
private mvardestphonenumtype as string '局部复制
private mvardestphonenum as string '局部复制
private mvartppid as integer '局部复制
private mvartpdsc as integer '局部复制
private mvartpvp as integer '局部复制
private mvarmsglen as integer '局部复制
private mvarmsgcontent as string '局部复制
private mvarpdulen as integer '局部复制
private mvarpdu as string '局部复制
'要引发该事件,请遵循下列语法使用 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 = ""
'mvarmsghead = 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 被叫号码类型。有+86时候为"91",否则为"81"
if len(mvarsmsc) > 0 then
formatphonenum servicenumpdu, mvarsmsctype
mvarsmsclen = len(servicenumpdu & mvarsmsctype) / 2 '短信息中心地址长度。(短信息中心号码类型 + 短信息中心号码长度 /2 的十六进制表示)
end if
mvardestphonenumlen = formatphonenum(destphonenumpdu, mvardestphonenumtype) ''被叫号码长度。被叫号码长度的十六进制表示。
'
if len(mvarmsgcontent) > 70 then
mvarmsgcontent = left(mvarmsgcontent, 70)
end if
' mvarmsglen = len(mvarmsgcontent)
dim smstext as string
smstext = mvarmsgcontent
'
smstext = gb2unicode(smstext) '把汉字符转化为unicode的hex编码字符串
'
'
mvarmsglen = len(smstext) / 2
if len(mvarsmsc) = 0 then
mvarsmsclen = 0
mvarpdu = int2hexstr(mvarsmsclen) & int2hexstr(mvarmsghead) & int2hexstr(mvartpmr) & int2hexstr(mvardestphonenumlen) & 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(mvardestphonenumlen) & mvardestphonenumtype & destphonenumpdu & _
int2hexstr(mvartppid) & int2hexstr(mvartpdsc) & int2hexstr(mvartpvp) & int2hexstr(mvarmsglen) & smstext
mvarpdulen = len(mvarpdu) / 2 - 9 'pdu字符串长度
end if
genpdu = mvarpdu
end function
'public property let pdu(byval vdata as string)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.pdu = 5
' mvarpdu = vdata
'end property
public property get pdu() as string
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.pdu
call genpdu
pdu = mvarpdu
end property
'public property let pdulen(byval vdata as integer)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.pdulen = 5
' mvarpdulen = vdata
'end property
public property get pdulen() as integer
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.pdulen
pdulen = mvarpdulen
end property
public property let msgcontent(byval vdata as string)
'向属性指派值时使用,位于赋值语句的左边。
'syntax: x.msgcontent = 5
mvarmsgcontent = vdata
mvarmsglen = len(vdata) * 2
end property
public property get msgcontent() as string
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.msgcontent
msgcontent = mvarmsgcontent
end property
'public property let msglen(byval vdata as string)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.msglen = 5
' mvarmsglen = vdata
'end property
public property get msglen() as integer
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.msglen
msglen = mvarmsglen
end property
public property let tpvp(byval vdata as integer)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.tpvp
tpvp = mvartpvp
end property
public property let tpdcs(byval vdata as integer)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.tpdsc
tpdcs = mvartpdsc
end property
public property let tppid(byval vdata as integer)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.tppid
tppid = mvartppid
end property
public property let destphonenum(byval vdata as string)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.destphonenum
destphonenum = mvardestphonenum
end property
'public property let destphonenumtype(byval vdata as string)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.destphonenumtype = 5
' mvardestphonenumtype = vdata
'end property
'
'
public property get destphonenumtype() as string
'检索属性值时使用,位于赋值语句的右边。
'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)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.destphonenumlen = 5
' mvardestphonenumlen = vdata
'end property
'
'
public property get destphonenumlen() as integer
'检索属性值时使用,位于赋值语句的右边。
'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)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.tpmr
tpmr = mvartpmr
end property
public property let msghead(byval vdata as integer)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.msghead
msghead = mvarmsghead
end property
public property let smsc(byval vdata as string)
'向属性指派值时使用,位于赋值语句的左边。
'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
'检索属性值时使用,位于赋值语句的右边。
'syntax: debug.print x.smsc
smsc = mvarsmsc
end property
'public property let smsctype(byval vdata as string)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.smsctype = 5
' mvarsmsctype = vdata
'end property
public property get smsctype() as string
'检索属性值时使用,位于赋值语句的右边。
'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)
''向属性指派值时使用,位于赋值语句的左边。
''syntax: x.smsclen = 5
' mvarsmsclen = vdata
'end property
'
'
public property get smsclen() as integer
'检索属性值时使用,位于赋值语句的右边。
'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
mvartpvp = 255
mvarmsglen = 0
mvarmsgcontent = ""
mvarpdulen = 0
mvarpdu = ""
' msg.msghead = "11" '文件头字节 (header byte, 是一种 bitmask) 。这里 11 指正常地发送短信息。
' msg.tpmr = "00" '信息参考号。( tp-mr )
' msg.tppid = "00" '‘一般都是 00 ,表示点到点的标准短信
' msg.tpvp = "ff" '‘有效期 (tp-vp), 短信的有效时间 ,00或ff表示有效
' msg.tpdsc = "08" '用户信息编码方式 (tp-dcs) , 7-bit 编码( 08 : ucs2 编码 汉字一般为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
'由于位置上略有处理,实际号码应为: 8613805515500( 字母 f 意指长度减 1),
'这是作者所在地 gsm 短信息中心的号码。 ( 号码处理方法为 , 如果为 +86 开始 , 将 + 号去掉 ,
'然后判断是否为偶数 , 不是在末尾补 f, 然后将奇数位和偶数位互换 )
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转换为unicode编码,保存在数组bytea()中。
'下面需要调整顺序并以字符串的形式返回
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 "错误:" & err & "." & vbcrlf & err.description
raiseevent validresult(err.number, err.description)
gb2unicode = ""
end function
使用方法:
dim sms1 as new smspduclass
sms1.destphonenum = "13922992078"
sms1.smsc = "+861380076950011"
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 with
end function
新闻热点
疑难解答