|
发表于 2012-1-10 16:27:16
|
显示全部楼层
中英文短信都不用短信中心号码
------------------------------------------------------------------------------------------------------------------------
#include "main.h"
#include "SysTick.h"
#include "Serial.h"
#include "Delay.h"
#include "Watchdog.h"
#include "eeprom.h"
#include "GSM_V1.h"
#include "Ex_Isr.h"
#include "Sms.h"
const unsigned char code *SCA_FO_MR_DA = "0011000D9168";
const unsigned char code *SMS_parameter = "0008FF";
const char code Ctrl_z[] = {0x1A, 0x00};
const unsigned char code ASIIC[]=
{
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'
};
// 正常顺序的字符串转换为两两颠倒的字符串,若长度为奇数,补'F'凑成偶数
// 如:"8613851872468" --> "683158812764F8"
// pSrc: 待转换的源字符串指针
// pDst: 转换后的目标字符串指针
// nSrcLength: 等转换的源字符串长度
// 返回: 转换后的目标字符串长度,包含了字符串结束符'\0'的长度
unsigned char GsmInvertNumbers(unsigned char *pSrc, unsigned char *pDst, unsigned char nSrcLength)
{
unsigned char nDstLength; // 目标字符串长度
unsigned char ch; // 用于保存一个字符
unsigned char i;
nDstLength = nSrcLength; // 复制串长度
for (i=0; i<nSrcLength; i+=2) // 两两颠倒
{
ch = *pSrc++; // 保存先出现的字符
*pDst++ = *pSrc++; // 复制后出现的字符
*pDst++ = ch; // 复制先出现的字符
}
if (nSrcLength & 1) // 源串长度是奇数吗?
{
*(pDst-2) = 'F'; // 补'F'
nDstLength++; // 目标串长度加1
}
*pDst = '\0'; // 输出字符串加个结束符
return nDstLength; // 返回目标字符串长度,加上了'\0'的长度
}
/*
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 测试ok
// 字节数据转换为可打印字符串
// 如:{0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01} --> "C8329BFD0E01"
// pSrc: 待转换的源数据指针
// pDst: 转换后存放目标字符串指针
// nSrcLength: 待转换的源数据长度,数据长度不能大于256个字节,从1开始
// 返回: 转换后的目标字符串长度 <=0xffff,带字符串结束符'\0'。
unsigned char GsmBytes2String(const unsigned char *pSrc, unsigned char *pDst, unsigned char nSrcLength)
{
unsigned char i;
const unsigned char code tab[] = "0123456789ABCDEF"; // 0x0-0xf的字符查找表
for (i=0; i<nSrcLength; i++)
{
*pDst = tab[pSrc >> 4]; // 输出高4位
pDst++;
*pDst = tab[pSrc & 0x0f]; // 输出低4位
pDst++; // 转换下一个十六进制字节
}
pDst++;
*pDst = '\0'; // 输出字符串加个结束符
return (nSrcLength * 2 + 1); // 返回目标字符串长度,带字符串结束符'\0'
}
*/
/*------------------------------------------------------------------------------------------*-
* 函数名称: SendSMS()
* 参 数: pSMS_data 要发送的字符串
* 返 回: xx
* 函数功能: 发送SMS
*
* 说 明:
*
* 当前版本: V1.0 * 取代版本:
* 作 者: wsl * 修 改:
* 版本信息: 2010-10-10 * 修改信息:
-*-------------------------------------------------------------------------------------------*/
void SendPduSms(unsigned char *pSMS_data, unsigned char *phone)
{
unsigned char xdata SMS_Buffer[340];
unsigned char idata PhoneNumber[15];
unsigned char idata tmp[4] = {0};
unsigned char i;
if ((phone[0]==0xFF) || (phone[0]==0))
{
return;//当传递的号码不空时返回
}
memset(SMS_Buffer, 0, 340); // 清缓冲区
memset(PhoneNumber, 0, 15);
GsmInvertNumbers(phone, PhoneNumber, strlen((const char*)phone)); //号码转换
//信息头
strncpy((char*)SMS_Buffer, (const char*)SCA_FO_MR_DA, strlen((const char*)SCA_FO_MR_DA));
//增加目标手机号码
strcat((char*)SMS_Buffer, (const char*)PhoneNumber);
//参数
strcat((char*)SMS_Buffer, (const char*)SMS_parameter);
//信息数据长度
i = strlen((char const*)pSMS_data)/2;
memset(tmp, 0, 4);
tmp[0] = ASIIC[i >> 4];
tmp[1] = ASIIC[i & 0x0f];
strcat((char*)SMS_Buffer, (const char*)tmp);
//信息数据
strcat((char*)SMS_Buffer, (const char*)pSMS_data);
//ctrl+z
strcat((char*)SMS_Buffer, Ctrl_z);
memset(tmp, 0, 4);
//计算
i = (strlen((const char*)SMS_Buffer) - 2) / 2;
tmp[0] = i / 100 + 0x30;
i = i % 100;
tmp[1] = i / 10 + 0x30;
tmp[2] = i % 10 + 0x30;
bSentSuccessfully = 0;
i = 0;
while ((!bSentSuccessfully) && (++i <= 8))
{
SendString("ATH\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CMGF=0\r\n", 0); //短信息设置为PDU格式
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CSCS=\"UCS2\"\r\n", 0); //PDU字符集
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CMGS=", 0);
SendString(tmp, 0);
SendString("\r\n", 0);
GetGsmResponseValue(0, 1, "OK"); //在此处串口收到的有可能是+CMS ERROR而不是>
SendString(SMS_Buffer, 0);
GetGsmResponseValue(1, 10, "OK");
}
SendString("ATH\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
}
/*------------------------------------------------------------------------------------------*-
* 函数名称: Send_SMS()
* 参 数: pSMS_data 要发送的字符串
* 返 回: xx
* 函数功能: 发送SMS
*
* 说 明:
*
* 当前版本: V1.0 * 取代版本:
* 作 者: wsl * 修 改:
* 版本信息: 2010-10-10 * 修改信息:
-*-------------------------------------------------------------------------------------------*/
void SendTxtSms(unsigned char *pNumber, unsigned char *pSMS_data)
{
unsigned char i=0;
bSentSuccessfully = 0;
i = 0;
while ((!bSentSuccessfully) && (++i <= 8))
{
SendString("ATH\r\n", 0); //挂机
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CMGF=1\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CSCS=\"GSM\"\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
SendString("AT+CMGS=", 0);
SendString(pNumber, 0);
SendCharacter(';');
SendString("\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
SendString(pSMS_data, 0);
SendCharacter(0x1A);
GetGsmResponseValue(1, 10, "OK");
}
SendString("ATH\r\n", 0);
GetGsmResponseValue(0, 1, "OK");
} |
|