选择显示字体大小

用visual basic设计手机短信收发程序

  中文转换成unicode码函数

  因为手机短消息的发送是以pdu串的形式发送出去的,中文字符以unicode码来表示,所以在发送中文短消息之前必须首先将中文字符转换为unicode码,下面的函数将实现这个功能。这个函数主要应用到vb自带的一个格式转换函数:chrw()将中文转换为unicode码。

public function chg(rmsg as string) as string

 dim tep as string
 dim temp as string
 dim i as integer
 dim b as integer

 tep = rmsg
 i = len(tep)
 b = i / 4
 if i = b * 4 then
  b = b - 1
  tep = left(tep, b * 4)
 else
  tep = left(tep, b * 4)
 end if

 chg = ""
 for i = 1 to b
  temp = "&h" & mid(tep, (i - 1) * 4 + 1, 4)
  chg = chg & chrw(cint(val(temp)))
 next i
end function


  短信中心手机号码的pdu串转换函数

  同上,为了发送以pdu模式发送短消息,必须将手机号码和对方手机号码也转换为pdu格式,下面的函数就是为了实现这种转换:

public function telc(num as string) as string

 dim tl as integer
 dim ltem, rtem, ttem as string
 dim ti as integer

 ttem = ""
 tl = len(num)
 if tl <> 11 and tl <> 13 then
  msgbox "wrong number." & tl
  exit function
 end if

 if tl = 11 then
  tl = tl + 2
  num = "86" & num
 end if
 for ti = 1 to tl step 2
  ltem = mid(num, ti, 1)
  rtem = mid(num, ti + 1, 1)
  if ti = tl then rtem = "f"
  ttem = ttem & rtem & ltem
 next ti
 telc = ttem
end function

  手机号码有两种表示方法:11位和13位(带国家码86),一般手机发送时都是以13位形式表示的,所以以上的函数还有一个功能是自动将11位格式手机号码转换为13位形式,然后再转换为pdu串。

  手机短信的发送

  手机短信的发送主要借助于vb的mscomm控件实现,关于mscomm控件,前面的技术介绍部分有详细介绍。短信的发送是由at+cmgs指令完成的,采用pdu模式发送,函数代码如下:

const prex = "0891"
const midx = "11000d91"
const sufx = "000800"

public function sendsms(csca as string, num as string, msg as string) as _boolean
 dim pdu, psmsc, pnum, pmsg as string
 dim leng as string
 dim length as integer

 length = len(msg)
 length = 2 * length
 leng = hex(length)
 if length < 16 then leng = "0" & leng
 psmsc = trim(telc(csca))
 pnum = trim(telc(num))
 pmsg = trim(ascg(msg))
 pdu = prex & psmsc & midx & pnum & sufx & leng & pmsg
 sleep(1)
 mobcomm.output = "at+cmgf=0" + vbcr
 mobcomm.output = "at+cmgs=" & str(15 + length) + vbcr
 mobcomm.output = pdu & chr&#36;(26)
 sleep(1)
 sendsms = true
end function

  因为手机同一时间只能处理一件事情,因此这个函数只负责发送短信,关于短信发送成功与否以及阅读短信的部分集中在一起处理。判断手机短信发送成功与否主要由at+cmgs命令执行以后的返回码来决定(可参见前文的at指令介绍部分)。

  为了防止手机因过于繁忙而出错,这里采取了一定的方法让手机有充分的时间处理发送和接收及删除等操作。sleep()函数正是为此而设计的,在发送及删除操作后都会让程序暂停一秒,这样就不至于使得手机过于繁忙。

  手机短信的接收

  unicode码解码函数

  相比于手机短信的发送而言,手机短信的接收主要的工作正好与之相反。手机短信的发送需要将待发送的短信内容转换为unicode码,而短信的接收则需要将接收到的unicode码转换成中文字符。下面的函数将实现解码功能。同手机短信发送的编码函数一样,这里也应用了一个vb内置的函数ascw()函数来将unicode码转换为中文:

public function ascg(smsg as string) as string

 dim si, sb as integer
 dim stmp as integer
 dim stemp as string

 sb = len(smsg)
 ascg = ""
 for si = 1 to sb
  stmp = ascw(mid(smsg, si, 1))
  if abs(stmp) < 127 then
   stemp = "00" & hex(stmp)
  else
   stemp = hex(stmp)
  end if
  ascg = ascg & stemp
 next si
 ascg = trim(ascg)
end function

  手机短信接收函数

  相对于短信的发送函数而言,短信的接收相当简单,只需要以下的三行代码就完成了。但是它使用的技术却决不比短信的发送少,这里主要用到了mscomm控件的output属性和at+cmgr指令。

public sub readsms(rnum as string)

 mobcomm.output = "at+cmgf=1" + vbcr
 mobcomm.output = "at+cmgr=" & rnum + vbcr

end sub


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons