5.4 VB对英文和汉字的处理
英文的数据编码方式:
GSM只支持ASCII码值从0x00到0x7f的128个字符。这些值只需要7bit去定义,而SMS短信息是以8bit字节序列传输的,因此GSM使用一种编码方式将7bit ASCII码值序列压缩成8 bit字节序列。
数据编码过程事例如下:“TESTU”
英文短信息: “T” “E” “S” “T” “U” ASCII码值: 0x54 0x45 0x53 0x54 0x20 二进制表示: 101 0100 100 0101 101 0011 101 0100 010 0000
压缩码过程: 11010100 11100010 10010100 00001010 00000010 目标字节串: 0XD4 0XE2 0x94 0x0A 0x02
当原文长度达到8位时,经编码后长度只有7位,原文到第9位时又如第1位开始编码,所不同是位移的序号比原文减1,依次补齐8位。这样“TESTU “的8bit编码为D4E2940A02。 中文数据的编码方式:
对于中文的编码,即采用UCS2编码(Unicode(Universal Multiple Octet coded Character Set))是用两个字节表示每个字符的字符编码方案。它以ASCII为设计基础,而统一的编码方式。 中文转换成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
26
tep = Left(tep, b * 4) Else
tep = Left(tep, b * 4) End If chg = \ For i = 1 To b
temp = \ 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 \ Exit Function End If
If tl = 11 Then tl = tl + 2 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 = \ ttem = ttem & rtem & ltem Next ti telc = ttem End Function
27
手机号码有两种表示方法:11位和13位(带国家码86),一般手机发送时都是以13位形式表示的,所以以上的函数还有一个功能是自动将11位格式手机号码转换为13位形式,然后再转换为PDU串。
短信的发送主要借助于VB的Mscomm控件实现,关于Mscomm控件,前面的技术介绍部分有详细介绍。短信的发送是由AT+CMGS指令完成的,采用PDU模式发送,函数代码如下: Const prex = \Const midx = \Const sufx = \
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 = \ psmsc = Trim(telc(csca)) pnum = Trim(telc(num)) pmsg = Trim(ascg(msg))
pdu = prex & psmsc & midx & pnum & sufx & leng & pmsg sleep(1)
mobcomm.Output = \
mobcomm.Output = \ mobcomm.Output = pdu & Chr$(26) sleep(1) Sendsms = True End Function
Unicode码解码函数
相比于短信的发送而言,短信的接收主要的工作正好与之相反。短信的发送需要将待发送的短信内容转换为Unicode码,而短信的接收则需要将接收到的Unicode码转换成中文字符。下面的函数将实现解码功能。同短信发送的编码函数一样,这里也应用了一个VB内置的函数AscW()函数来将Unicode码转换为中文:Public Function ascg(smsg As String) As String
28
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 = \ 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 = \ mobcomm.Output = \End Sub
5.4短信息收发系统的实现
通过VB6.0来实现短信息收发系统的源程序如下: ‘定义变量
Dim bb_b 'unicode转换变量 Dim vv(1) As Byte ' 换行符 Dim ac(0) As Byte '删除符 Dim av(0) As Byte '08 Dim adv(0) As Byte '0d Dim ert(0) As Byte Dim biai
29
Dim byu(5) Dim c_c Dim mma Dim ced Dim reah Dim ruc
Dim TOOP(2) Dim op Dim biao Dim opq Dim read
‘结束按钮
'''''''''''''''''''''''''''''''''''''' '使用命令按钮控件 '关闭通信端口 '结束系统
'''''''''''''''''''''''''''''''''''''' Private Sub cmdEnd_Click() MSComm1.PortOpen = False End End Sub
‘发送按钮
'''''''''''''''''''''''''''''''''''''' '使用命令按钮控件
'将定时器控件的启动/关闭状态作切换 '''''''''''''''''''''''''''''''''''''' Private Sub Command1_Click() Dim weq(0) As Byte biai = 0 Call aa
30
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库基于VB实现短信息收发功能(7)在线全文阅读。
相关推荐: