博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using VB.NET to Decode PDU string
阅读量:6457 次
发布时间:2019-06-23

本文共 5322 字,大约阅读时间需要 17 分钟。

我发表在CodeProject上的文章

Sample screenshot

Introduction

Wish to develop SMS or EMS application? What do you read from your mobile? PDU code. PDU is a format which you send to a phone to send a SMS or an EMS. You can find more in GSM 03.40.

What is a PDU code and how can I decode it?

Let us take a simple look at PDU code. Here I created a SMS: "Hello, my pretty world!", in my Siemens M55 mobile, and send it to 1861. I use HyperTerminal to communicate with my phone using AT Command set.

First, I set my character set to Unicode:

AT+CSCS="UCS2"

Then I set my preferred message storage to MT:

AT+CPMS="MT"

At last, I read all my SMS out:

AT+CMGL=4

I find the SMS I just created in PDU format:

0891683108200805F01151048181160000FF16C8329BFD66B5F320B8BC4CA7E741F7B79C4D0E01

Now, I decode it manually:

08: Length of Service Center Number91: Indicate there is a plus at the beginning of Service Center number683108200805F: Service Center number: 8613800280500 Note when the length                                       of number is an odd number, add F.11: Indicate this SMS will be sent.51: TP-MR Message Reference, not common used.04: Length of destination number. Here 1861.81: No plus here.8161: Destination number 1861.00: TP-PID. See GSM 03.4000: TP-DCS. TP-UD is coded by 7bit charactor method. See GSM 03.38FF: TP-VP. Valid Period. Here is infinity.16: Hex value equals to 22. This indicates there are 22 charactors in TP-UDC8329BFD66B5F320B8BC4CA7E741F7B79C4D0E01: TP-UD,            decode it using 7bit charactor method.

Ha-ha, now you may know more about PDU code. Let us start our program.

What is the structure of my PDU Decoder?

I created a must inherit class SMS, it provides basic structure a SMS must have. For example: something relating to service center, first octet, TP_PID, and so on.

In this class I provide a must override sub GetOrignalData, this help me to do further work on the orignal data from PDU code. Here are also a few of shared functions:

  • GetByte()
  • GetString()
  • GetDate()
  • Swap()
  • GetAddress()
  • GetSMSType()
  • DecodeUnicode90()
  • Decode7Bit()

There shared functions will be used frequently. After SMS class, I created SMS_RECEIVED, SMS_SUBMIT, EMS_RECEIVED, EMS_SUBMIT, SMS_STATUS_REPORT classes that inherit from SMS class.

How it works?

I wanted it like, once you create an instance of one class with a PDU code, it decodes it immediately. So, I wrote some code in every Sub New.

Take SMS_RECEIVED, for example:

Sub New(ByVal PDUCode As String)        Type = SMS.SMSType.SMS_RECEIVED        GetOrignalData(PDUCode)    End Sub

The Sub GetOrignalData gets original data from PDU code. This decodes a received SMS's PDU code.

Public Overrides Sub GetOrignalData(ByVal PDUCode As String)        SCAddressLength = GetByte(PDUCode)        SCAddressType = GetByte(PDUCode)        SCAddressValue = GetAddress((GetString(PDUCode, (SCAddressLength - 1) * 2)))        FirstOctet = GetByte(PDUCode)        SrcAddressLength = GetByte(PDUCode)        SrcAddressType = GetByte(PDUCode)        SrcAddressLength += SrcAddressLength Mod 2        SrcAddressValue = GetAddress((GetString(PDUCode, SrcAddressLength)))        TP_PID = GetByte(PDUCode)        TP_DCS = GetByte(PDUCode)        TP_SCTS = GetDate(GetString(PDUCode, 14))        TP_UDL = GetByte(PDUCode)        TP_UD = GetString(PDUCode, TP_UDL * 2)    End Sub

It is so easy to write some code like this after you read GSM 03.40. Shared function here helps me get what I want, a byte or a string or even a date. Note that when you get something from PDU code, it will be got rid of from the PDU code. This seemed like a good way after I failed to control the position of what PDU code I should get, because every time, the shared functions started from the beginning of PDU code.

In EMS, I write a function to get Information Element from PDU code and a structure to store it.

Public Structure InfoElem               Public Identifier As Byte        Public Length As Byte        Public Data As String    End Structure
Shared Function GetIE(ByVal IECode As String) As InfoElem()        Dim tmp As String = IECode, t As Integer = 0        Dim result() As InfoElem        Do Until IECode = ""            ReDim Preserve result(t)            With result(t)                .Identifier = GetByte(IECode)                .Length = GetByte(IECode)                .Data = GetString(IECode, .Length * 2)            End With            t += 1        Loop        Return result    End Function

Maybe, my class is not the best, but it works well :).

How can I use this class?

If you know what the type of PDU code is, you simply create an instance of a certain type in SMS or EMS. But when you don't know the type, you use shared function GetSMSType to get the type. Then according to the type, create a certain instance of SMS class, it is decoded automatically.

Still don't know? Sorry for my poor English, run my program and see my code, you will know it better.

Are there some bugs?

Sorry, I don't find bugs when I decode PDU code from Siemens M55 mobile and Nokia 8xxx mobile, I think there will be no bugs. If you find some, please contact me. Thanks!

Some useful documents and sites

  • (this site provides GSM documents).

Contact me and make friends with me

My email: ; . My QQ (only in China): 38288890. My MSN: hesicong@mail.sc.cninfo.net. . . In the end, thanks for using these classes!

转载地址:http://zgnzo.baihongyu.com/

你可能感兴趣的文章
MySQL my.cnf参数配置优化详解
查看>>
JavaNIO基础02-缓存区基础
查看>>
日本开设无人机专业,打造无人机“人才市场”
查看>>
190行代码实现mvvm模式
查看>>
PXE部署实例
查看>>
cobbler初探------实现自动安装centos6.4
查看>>
Android Studio 2.0 preview3 BUG
查看>>
兼容几乎所有浏览器的透明背景效果
查看>>
Go语言4
查看>>
jeesite 框架搭建与配置
查看>>
Adb移植(一)简单分析
查看>>
Linux VNC server的安装及简单配置使用
查看>>
阿里宣布开源Weex ,亿级应用匠心打造跨平台移动开发工具
查看>>
Android项目——实现时间线程源码
查看>>
招商银行信用卡重要通知:消费提醒服务调整,300元以下消费不再逐笔发送短信...
查看>>
python全栈_002_Python3基础语法
查看>>
C#_delegate - 调用列表
查看>>
交换机二层接口access、trunk、hybird三种模式对VLAN的处理过程
查看>>
jQuery.extend 函数详解
查看>>
[转]Windows的批处理脚本
查看>>