公告:

  没有公告

您现在的位置: 清河在线 >> 设计教程 >> 程序开发 >> 教程正文
最新热门教程
最新推荐教程
广告载入中...
用VB6.0编写手机短信发送       ★★★
用VB6.0编写手机短信发送
副标题:用VB6.0编写手机短信发送
作者:佚名    教程来源:本站原创    点击数:    更新时间:2007-4-20

  因为手机短消息的发送是以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格式,下面的函数就是为了实现这种转换: 

  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$(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 

  2 手机短信接收函数 

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

  Public Sub readsms(rnum As String)
  mobcomm.Output = "AT+CMGF=1" + vbCr
  mobcomm.Output = "AT+CMGR=" & rnum + vbCr
  End Sub 

教程录入:HBctong    责任编辑:HBctong 
  • 上一篇教程:
  • 下一篇教程: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    (只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    姓 名: * Oicq:
    性 别: Msn:
    E-mail: Icq:
    主 页:
    评 分: 1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  • 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 |   

    感谢邢台创通公司 提供空间 | 冀ICP备05018085号

    清河在线 All Rights Reserved (C) Copyright 2006 TEL:0319-7502356     创通网站工作室
    清河在线 *卡通版*