分享
 
 
 

使用串口发送普通短消息

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

At 指令文档

SMS_PDU-mode文档

短消息Text编码原码(下载至PJ Naughter的主页)

pdu编码示例:

//message中的位的设置可以参考文档中的说明

//红色的字表示可能与mfcsms中不同的地方

CString CSMSMessage::CreatePDUMessage() const

{

CByteArray message;

//message.SetSize(0, 280);

//First thing to do is work on the Service Center Number

if (!AddEncodedSMSCNumber(message, m_sServiceCenterNumber, m_ServiceCenterNumberAddressType))

return _T("");

//Form the SMS-SUBMIT octet

BYTE bySMSSubmit = 0x1; //By default set this PDU to an SMS-SUBMIT

if (m_bUseValidityPeriod)

{

if (m_bUseRelativeValidityPeriod)

bySMSSubmit |= 0x10;

else

bySMSSubmit |= 0x18;

}

if (m_bRequireStatusReport)

bySMSSubmit |= 0x20;

message.Add(bySMSSubmit);

//Next is the TP-Message-Reference. We let the phone set the message reference itself

message.Add(0x0);

//Next is the Phone number

ASSERT(m_sPhoneNumber.GetLength()); //Must specify a number to deliver the message to

if (!AddEncodedPhoneNumber(message, m_sPhoneNumber, m_PhoneNumberAddressType))

return _T("");

//Next is the Protocol Identifier

message.Add(0x0);

//Next is the Data encoding scheme (this code always use PDU encoding)

message.Add(0x8);

//Next is the validity period

if (m_bUseValidityPeriod)

{

if (m_bUseRelativeValidityPeriod)

{

//Relative Validity period is pseudo logarithmitly encoded into 1 octet

BYTE byValidityPeriod = 0;

if (m_RelativeValidityPeriod <= CTimeSpan(0, 12, 0, 0))

byValidityPeriod = (BYTE) ((m_RelativeValidityPeriod.GetTotalSeconds() / 300) - 1);

else if (m_RelativeValidityPeriod <= CTimeSpan(1, 0, 0, 0))

byValidityPeriod = (BYTE) (((m_RelativeValidityPeriod.GetTotalSeconds() - 43200) / 1800) + 143);

else if (m_RelativeValidityPeriod <= CTimeSpan(30, 0, 0, 0))

byValidityPeriod = (BYTE) ((m_RelativeValidityPeriod.GetTotalSeconds() / 86400) + 166);

else

byValidityPeriod = (BYTE) ((m_RelativeValidityPeriod.GetTotalSeconds() / 604800) + 192);

message.Add(byValidityPeriod);

}

else

{

//Absolute Validity period is encoded into 7 octets

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wYear % 100));

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wMonth));

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wDay));

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wHour));

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wMinute));

message.Add(SwappedNibble(m_AbsoluteValidityPeriod.wSecond));

long nLocalTimeZone = labs(m_TimeZone);

BYTE byTimeZone = SwappedNibble(nLocalTimeZone / 15);

if (m_TimeZone < 0)

byTimeZone |= 0x80;

message.Add(byTimeZone);

}

}

//Next is the length of the user data

int nMessageLength = m_sMessage.GetLength();

int nActualLen=0;

for(int j=0;j<nMessageLength;j++)

{

BYTE data=(BYTE)m_sMessage.GetAt(j);

int nNibble = (int)data;

if(nNibble>=0&&nNibble<=127)

nActualLen=nActualLen+2;

else

nActualLen=nActualLen+1;

}

if (nMessageLength > 255)

{

TRACE(_T("CSMSMessage::CreatePDUMessage, Cannot encode %s as it is longer than the maximum size of 255\n"), m_sMessage);

return _T("");

}

if (nMessageLength == 0)

{

TRACE(_T("CSMSMessage::CreatePDUMessage, Failing as no message has been specified to be sent\n"));

return _T("");

}

//Initially we just add a place holder as it will be updated later on once we have added the actual data

int nDataLengthIndex = message.GetSize();

message.Add((BYTE) nActualLen);

//Finally before we return from the function, convert from the BYTE array representation to the string version

WCHAR wcBuffer[256];

char cBuffer[256];

memset(cBuffer,0,256);

memset(wcBuffer,0,256*2);

MultiByteToWideChar(CP_ACP, 0, m_sMessage, nMessageLength, wcBuffer, 256);

memcpy((void*)cBuffer,(void*)wcBuffer,256);

char tmp;

for(int k =0; k<nMessageLength; k++)

{

tmp = cBuffer[2*k];

cBuffer[2*k] = cBuffer[2*k+1];

cBuffer[2*k+1]=tmp;

}

for(k=0;k<nActualLen;k++)

message.Add(cBuffer[k]);

int nArraySize = message.GetSize();

CString sMsg;

int nBufferSize = (nArraySize*2) + 1;

TCHAR* pszData = sMsg.GetBuffer(nBufferSize);

int nPDUOutputIndex = 0;

for (int i=0; i<nArraySize; i++)

{

BYTE byData = message.GetAt(i);

int nNibble1 = (byData & 0xF0) >> 4;

if (nNibble1 > 9)

pszData[nPDUOutputIndex] = (TCHAR) (nNibble1 - 10 + _T('A'));

else

pszData[nPDUOutputIndex] = (TCHAR) (nNibble1 + _T('0'));

++nPDUOutputIndex;

int nNibble2 = byData & 0xF;

if (nNibble2 > 9)

pszData[nPDUOutputIndex] = (TCHAR) (nNibble2 - 10 + _T('A'));

else

pszData[nPDUOutputIndex] = (TCHAR) (nNibble2 + _T('0'));

++nPDUOutputIndex;

}

sMsg.ReleaseBuffer(nPDUOutputIndex);

return sMsg;

}

使用at指令发送端消息示例:

//Check to see if there is a modem on the specified serial port

char* pszATMsg = "AT\r";//测试连接是否成功

Write(pszATMsg, strlen(pszATMsg));

if (!WaitForSerialResponse("OK", dwTimeout, 32))

{

CString sError;

sError.LoadString(IDS_SMSSERIALPORT_FAIL_RECEIVE_RESPONSE_FROM_MODEM);

AfxThrowSMSException(sError, E_SMS_NO_MODEM_RESPONSE);

}

char* pszSelectMessageMsg = "AT+CSMS=0\r"; //Select Message service

Write(pszSelectMessageMsg, strlen(pszSelectMessageMsg));

if (!WaitForSerialResponse("OK", dwTimeout, 32))

{

CString sError;

sError.LoadString(IDS_SMSSERIALPORT_MODEM_DOES_NOT_SUPPORT_SMS);

AfxThrowSMSException(sError, E_SMS_NO_SMS_SUPPORT);

}

char* pszMessageFormat = "AT+CMGF=0\r"; //Set the transfer mode to PDU

Write(pszMessageFormat, strlen(pszMessageFormat));

if (!WaitForSerialResponse("OK", dwTimeout, 32))

{

CString sError;

sError.LoadString(IDS_SMSSERIALPORT_FAIL_SET_PDU_MODE);

AfxThrowSMSException(sError, E_SMS_SET_PDU_MODE);

}

CString sSendCommand;

sSendCommand.Format(_T("AT+CMGS=%d\r"), nMsgSize); //Prepare to send the PDU

LPSTR pszAsciiSendCommand = T2A((LPTSTR) (LPCTSTR) sSendCommand);

Write(pszAsciiSendCommand, strlen(pszAsciiSendCommand));

if (!WaitForSerialResponse(">", dwTimeout, 32))

{

CString sError;

sError.LoadString(IDS_SMSSERIALPORT_FAIL_SEND_SMS_MESSAGE_TO_MODEM);

AfxThrowSMSException(sError, E_SMS_SEND_MESSAGE_TO_MODEM);

}

//And finally write out the PDU as a string

LPSTR pszAsciiPDU = T2A((LPTSTR) (LPCTSTR) sData);

int nAsciiPDULength = strlen(pszAsciiPDU);

Write(pszAsciiPDU, nAsciiPDULength);//pszAsciiPDU是短消息的PDU编码

Write("\x1A", 1);

if (!WaitForSerialResponse("OK", dwTimeout, 32 + nAsciiPDULength))

{

CString sError;

sError.LoadString(IDS_SMSSERIALPORT_FAIL_SEND_SMS_MESSAGE);

AfxThrowSMSException(sError, E_SMS_SEND_MESSAGE);

}

我的主页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有