用于 SMS 的 STL PDU 库






3.22/5 (6投票s)
这是一个轻量级的库,用于解码和编码 GSM 短信 PDU 格式。
引言
根据 Etsi 组织(文档 GSM 03.40 和 GSM 03.38)的规范,短信消息最长可达 160 个字符,每个字符根据 7 位默认字母表为 7 位。八位消息(最多 140 个字符)通常不能在手机上以文本消息的形式查看;相反,它们用于数据输入,例如,智能消息(图像和铃声)以及 WAP 设置的 OTA 配置。16 位消息(最多 70 个字符)用于 Unicode (UCS2) 文本消息,大多数手机可以查看。 16 位文本消息,如果属于 0 类,在某些手机上将显示为 Flash SMS(也称为闪烁短信或警报短信)。
此库用于在 7 位或 UCS2 下将纯文本编码/解码为 PDU 格式(是的,目前尚不支持 EMS)。此库易于使用,但并非没有错误,请耐心等待 :)。
我搜索过互联网,发现了一些针对 .NET Framework 下 PDU 格式的优秀库,但我无法在当前工作中使用的库,所以我为自己的需求编写了这个库。
源代码
源代码非常简单,只有两个文件;您可以将它们添加到您的项目中或构建一个库。
如何使用...
该库有两个主要函数:Compose()
和 Fetch()
。使用这两个函数,您可以组合消息或从 PDU 字符串中提取数据。
/**
* Compose a shot message
* @para pdudata pdu: [out] The pdu result
* @para string msg: data need to be sent
* @para string phone: the destination phone number
* @para string msc: message center number, if null, use the preset number
* @para EEncodeMethod eMethod: the encode method, default is UCS2
*
* @return the data length without the SMSC part[you need it in AT+CMGS command]
*/
int Compose (pdudata& pdu,
std::tstring msg, std::tstring phone,
std::tstring msc = _T(""),
EEncodeMethod eMethod = EEncodeUCS2) throw(...);
/**
* Fetch data form a pdu format data
* only return the message part(USERDATA),
use other member function to fetch information
* @para pdudata pdu: the source data
*
* @return The nomral message
*/
std::tstring Fetch(pdudata pdu) throw(...);
组合短信消息
要组合消息,您需要执行以下操作
CPDU pdu;
LPCTSTR szMsg = m_szEncodeContents.GetBuffer(m_szEncodeContents.GetLength());
pdu.SetMSCNumber(_T("13800210500")); // Set the message center number here
try {
CPDU::pdudata strPDU;
// Compose the message
int iLen = pdu.Compose(strPDU, szMsg, _T("13813878775"));
WTL::CString csPDU;
// Show the message to the output edit control
#ifdef _UNICODE
MultiByteToWideChar(
CP_ACP,
MB_PRECOMPOSED,
strPDU.c_str(),
-1,
csPDU.GetBuffer(1024),
1024
);
#else
csPDU = strPDU.c_str();
#endif // _UNICODE
m_szDecodeContents = csPDU;
} catch (CPDUException e) {
m_szDecodeContents = e.GetMessage().c_str();
}
提取数据
当您需要提取消息时,您需要执行以下操作
CPDU pdu;
WTL::CString szMessage = pdu.Fetch(pdustring);
如果没有异常,那么您可以使用这些函数来提取其他数据
/**
* Fetch Data
*/
// The caller/sender number
std::tstring GetCaller() { return m_szCaller; }
// The callee/recevier number
std::tstring GetCallee() { return m_szCallee; }
// Message time stamp
std::tstring GetTimeStamp() { return m_szTimeStamp; }
Notice
tstl.h 文件是用于 STL 字符串和流的 TCHAR
风格的头文件。
更新
- 2007 年 5 月 22 日:修复了
Decode7bit
函数中的一个错误,感谢 alexafros12345 :)