正文

[转帖] 手机短信收发2005-09-25 07:49:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5256.html

分享到:

[转帖] 手机短信收发

            手机短信发送
            ²        中文转换成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串。
            &sup2;        手机短信的发送
            手机短信的发送主要借助于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()函数正是为此而设计的,在发送及删除操作后都会让程序暂停一秒,这样就不至于使得手机过于繁忙。
            手机短信的接收
            &sup2;        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

            &sup2;        手机短信接收函数
            相对于短信的发送函数而言,短信的接收相当简单,只需要以下的三行代码就完成了。但是它使用的技术却决不比短信的发送少,这里主要用到了Mscomm控件的Output属性和AT+CMGR指令。
                  Public Sub readsms(rnum As String)
                  mobcomm.Output = "AT+CMGF=1" + vbCr
                  mobcomm.Output = "AT+CMGR=" & rnum + vbCr
                  End Sub

阅读(1377) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册