[VB] sms pdu encoding and decoding 2

Source: Internet
Author: User

'/////////////////////////////////////// ////////////////////////////////
'Validate the Validity Function of the parameter.
'Legal return "true", illegal return "false"
'1. A single character cannot be sent. 2. The mobile phone number must be a number. 3. The phone number must be 11 characters long.
'/////////////////////////////////////// ////////////////////////////////
Private function smsparametervaliditytest (sendstr as string, simnumber as string) as Boolean
Smsparametervaliditytest = true
If Len (sendstr) "" then
'Delete string CHR (13) & CHR (10)
Do While instr (inputstr, CHR (13) & CHR (10 ))
Startposition = instr (inputstr, CHR (13) & CHR (10 ))
Inputstr = mid (inputstr, 1, startposition-1) & "& Mid (inputstr, startposition + 2)
Loop

'Determine whether to add blank lines before the input characters
If vaddblankline = true then
Txtstatus. Text = txtstatus. Text & CHR (13) & CHR (10) & time & ":" & inputstr & CHR (13) & CHR (10)
Else
Txtstatus. Text = txtstatus. Text & time & ":" & inputstr & CHR (13) & CHR (10)
End if
Txtstatus. selstart = Len (txtstatus. Text)
End if

'Automatically Save the prompt information to the "app. Path/commsg" directory.
Msgfilename = right ("0000" & year (now), 4) & right ("00" & month (now), 2) & right ("00" & Day (now), 2 )&". TXT"

'Save the file
Using num = freefile
Open app. Path & "/pubmsg/" & msgfilename for append as same num

If vaddblankline = true then
Print # interval num, CHR (13) & CHR (10) & time & ":" & inputstr
Else
Print # interval num, time & ":" & inputstr
End if
Close interval num
End sub

'/////////////////////////////////////// //////////////////////////
'The input text message is compressed into a hexadecimal string, and the decompressed string is output.
'/////////////////////////////////////// //////////////////////////
Public Function pubunzip_english (byval inputstr as string) as string
Dim tempstr as string
Dim I as integer

'The input string must be a hexadecimal string
If ishexstr (inputstr) = false then
Pubunzip_english = ""
Exit Function
End if

If Len (inputstr)/2 int (LEN (inputstr)/2) then
The number of characters entered by 'msgbox' cannot be an odd number! "
Pubunzip_english = ""
Exit Function
End if

If Len (inputstr) = 14 then
Tempstr = htob_long (inputstr)
Pubunzip_english = pubunzip_english + mid (tempstr, 2, 7) + mid (tempstr, 11, 6) + mid (tempstr, 1, 1 )_
+ Mid (tempstr, 20, 5) + mid (tempstr, 9, 2) + mid (tempstr, 29, 4) + mid (tempstr, 17, 3 )_
+ Mid (tempstr, 38, 3) + mid (tempstr, 25, 4) + mid (tempstr, 47, 2) + mid (tempstr, 33, 5 )_
+ Mid (tempstr, 56, 1) + mid (tempstr, 41, 6)
Else
Do While Len (inputstr)> = 14
Tempstr = htob_long (mid (inputstr, 1, 14 ))
Inputstr = mid (inputstr, 15)
Pubunzip_english = pubunzip_english + mid (tempstr, 2, 7) + mid (tempstr, 11, 6) + mid (tempstr, 1, 1 )_
+ Mid (tempstr, 20, 5) + mid (tempstr, 9, 2) + mid (tempstr, 29, 4) + mid (tempstr, 17, 3 )_
+ Mid (tempstr, 38, 3) + mid (tempstr, 25, 4) + mid (tempstr, 47, 2) + mid (tempstr, 33, 5 )_
+ Mid (tempstr, 56, 1) + mid (tempstr, 41, 6) + mid (tempstr, 49, 7)
Loop
If Len (inputstr)> 0 and Len (inputstr) int (LEN (vinputstr)/2) then
Pubunzip_chinese = ""
'Debug. Print Len (vinputstr)
Exit Function
End if

For I = 1 to Len (vinputstr) Step 4
Pubunzip_chinese = pubunzip_chinese & chrw (htod_2byte (mid (vinputstr, I, 4 )))
Next
End Function

'*************************************** **************************************** ****
'The normal SIM card number is converted to the sending format SIM card number. Input 11 characters and output 12 characters
'*************************************** **************************************** ****
Public Function simsendchange (simstring as string)
Dim I as integer
I = 2
Do while I 0 then
Startposition = instr (strarray (I), "08916831 ")
If startposition-4> 0 then
'
'There' s no '86' before the source SIM no in the SMS from Nanjing, need add '68 'into the SMS before deal with it.
Dim INTP as integer
INTP = 0
If mid (strarray (I), startposition + 24, 2) "68" then
INTP = 1
If INTP = 1 then
Pubstatus "this text message does not contain 86 content:" & strarray (I)
End if
Strarray (I) = mid (strarray (I), 1, startposition + 23) & "68" & Mid (strarray (I), startposition + 24)
End if
'
Pubsimnumber = simreceivechange (mid (strarray (I), startposition + 26, 12 ))
If mid (strarray (I), startposition + 40, 2) = "00" then
'Text messages are handled in English. Note: smscontentlen is the length of the entire text message.
'
'Smscontentlen = Val (mid (strarray (I), startposition-4, 2)-20
Smscontentlen = Val (mid (strarray (I), startposition-4, 2)-20 + INTP
'
Smscontent = mid (strarray (I), startposition + 58, smscontentlen * 2)
Unzipstr = pubunzip_english (smscontent)

Pubstatus "receives" & pubsimnumber & "and sends a text message. ", True
'
Pubstatus "SMS:" & strarray (I)
'
Pubstatus "text message content:" & smscontent
Pubstatus "extract:" & unzipstr
If Len (unzipstr)> 0 then
If Len (unzipstr)> = 5 and Len (unzipstr) "" then
'Anlisysandsend unzipstr, pubsimnumber
End if
Debug. Print strarray (I)
'Save the current information number
If isnumeric (mid (strarray (I), startposition-14, 3) then
Delnum = Val (mid (strarray (I), startposition-14, 3 ))
End if
'If you have read the text message, delete it.
If delnum-1 then
'Delsms_g18 delnum
End if
Elseif mid (strarray (I), startposition + 40, 2) = "08" then
'Text messages are processed in Chinese. Note: smscontentlen is the length of the entire text message.
'
Smscontentlen = Val (htod_1byte (mid (strarray (I), startposition + 56, 2) * 2
Smscontent = mid (strarray (I), startposition + 58, smscontentlen)
'Smscontentlen = Val (mid (strarray (I), startposition-4, 2)-20 + INTP
'Smscontent = mid (strarray (I), startposition + 58, smscontentlen * 2)
'

Unzipstr = pubunzip_chinese (smscontent)

Pubstatus "receives" & pubsimnumber & "and sends a text message. ", True
'
Pubstatus "SMS:" & strarray (I)
'
Pubstatus "text message content:" & smscontent
Pubstatus "extract:" & unzipstr
If Len (unzipstr)> 0 then
If Len (unzipstr)> = 5 and Len (unzipstr) "" then
'Anlisysandsend unzipstr, pubsimnumber
End if
'Save the current information number
If isnumeric (mid (strarray (I), startposition-14, 3) then
Delnum = Val (mid (strarray (I), startposition-14, 3 ))
End if
'If you have read the text message, delete it.
If delnum-1 then
'Delsms_g18 delnum
End if
Else
Pubstatus "unrecognized information type:" & Mid (strarray (I), startposition + 40, 2)
'Save the current information number
If isnumeric (mid (strarray (I), startposition-14, 3) then
Delnum = Val (mid (strarray (I), startposition-14, 3 ))
End if
'Save unrecognized information to the communication text file
Pubstatus "unidentifiable information type received, information content:" & strarray (I), true
'If you have read the text message, delete it.
If delnum-1 then
'Delsms_g18 delnum
End if
End if
Else
The pubstatus text message length is invalid. Strarray ("& I &") = "& strarray (I)
End if

'Latency to prevent repeated receivetime when receiving multiple messages
Pubdelay 1000
End if
Next I

End sub

''////////////////////////////////////// ///////
''Clear the serial receiving and sending buffer for text messages
''////////////////////////////////////// ///////
'Public sub clrsmsinputoutputbuffer ()
'G18.output = "at" & CHR (13)
'Pubdelay (300)
'Do while g18.inbuffercount 0
'G18.inbuffercount = 0
'Pubdelay (200)
'Loop
'G18.outbuffercount = 0
'End sub

'/////////////////////////////////////// /////
'Process SMS
'/////////////////////////////////////// /////
Private sub smstrac (byval vinputstr as string)
Dim startposition as integer
Dim smscontentlen as integer
Dim smscontent as string
Dim pubsimnumber as string
Dim smsstr as string
Dim unzipstr as string
Dim I as integer
Dim tmpstr as string
If Len (vinputstr) = 0 then
Exit sub
End if
If instr (vinputstr, "08916831") 0 then
Startposition = instr (vinputstr, "08916831 ")
If startposition-4> 0 then
Pubsimnumber = simreceivechange (mid (vinputstr, startposition + 26, 12 ))
If mid (vinputstr, startposition + 40, 2) = "00" then
''The text message is processed in English. Note: smscontentlen indicates the length of the entire text message.
'Smscontentlen = Val (htod_1byte (mid (vinputstr, startposition + 56, 2) * 2
Smscontentlen = Val (mid (vinputstr, startposition-5, 3)-20
Smscontent = (mid (vinputstr, startposition + 58, smscontentlen * 2 ))
Unzipstr = pubunzip_english (smscontent)
Pubstatus "receives" & pubsimnumber & "and sends a text message. ", True
'Pubstatus "text message content:" & smscontent
'//////////////
'// Save to database
'///////////////
'Smssavedatabase now, pubsimnumber, unzipstr
Pubstatus "sent content:" & unzipstr
'Apiep 2000,300 0
Elseif mid (vinputstr, startposition + 40, 2) = "08" then
'Text messages are processed in Chinese. Note: smscontentlen is the length of the entire text message.
Pubstatus "text message content:" & vinputstr
Smscontentlen = Val (htod_1byte (mid (vinputstr, startposition + 56, 2) * 2
Smscontent = mid (vinputstr, startposition + 58, smscontentlen)
Unzipstr = pubunzip_chinese (smscontent)
Pubstatus "receives" & pubsimnumber & "and sends a text message. ", True
'Pubstatus "text message content:" & smscontent
'////////////
'// Save to database
'/////////////
'Smssavedatabase now, pubsimnumber, unzipstr
Pubstatus "sent content:" & unzipstr
'Apiep 2000,300 0

Else
Pubstatus "unrecognized information type:" & Mid (vinputstr, startposition + 40, 2)
'Save unrecognized information to the communication text file
'Smssavedatabase now ,"",""
Pubstatus "unidentifiable information type received, information content:" & vinputstr, true
End if
Else
The pubstatus text message length is invalid. "
End if
End if
End sub

'/////////////////////////////////////// /////////////
'Check whether the user's mobile phone call parameters are valid
'/////////////////////////////////////// /////////////
Private function testparameter (byval vinputstr as string) as Boolean
Dim temparray () as string
Dim I as integer

Testparameter = true

'Length cannot be less than 7 bytes and not greater than 40 bytes
If Len (vinputstr) 40 then
Testparameter = false
Exit Function
End if

Vinputstr = mid (vinputstr, 6)
Temparray = Split (vinputstr, "0 ")

For I = lbound (temparray) to ubound (temparray)
If not (temparray (I) = "11" or temparray (I) = "21" or temparray (I) = "31" or temparray (I) = "41" or _
Temparray (I) = "51" or temparray (I) = "61" or temparray (I) = "71" or temparray (I) = "12 "_
Or temparray (I) = "22" or temparray (I) = "32" or temparray (I) = "42" or temparray (I) = "52") then
Testparameter = false
End if
Next
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.