📄 sms.lst
字号:
665 1 int xdata nLength; // 串口收到的数据长度
666 1 char xdata cmd[16]; // 命令串
667 1 char xdata pdu[512]; // PDU串
668 1 char xdata ans[128]; // 应答串
669 1
670 1
671 1 nPduLength = gsmEncodePdu(pSrc, pdu); // 根据PDU参数,编码PDU串
672 1 strcat(pdu, "\x01a"); // 以Ctrl-Z结束
673 1 gsmString2Bytes(pdu, &nSmscLength, 2); // 取PDU串中的SMSC信息长度
674 1
675 1 nSmscLength++; // 加上长度字节本身
676 1 // 命令中的长度,不包括SMSC信息长度,以数据字节计
677 1 sprintf(cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength); // 生成命令
678 1 WriteComm(cmd, strlen(cmd)); // 先输出命令串
679 1
680 1
681 1 nLength = ReadComm(ans, 128); // 读应答数据
682 1 // 根据能否找到"\r\n> "决定成功与否
683 1 if(nLength == 4 && strncmp(ans, "\r\n> ", 4) == 0)
684 1 {
685 2 WriteComm(pdu, strlen(pdu)); // 得到肯定回答,继续输出PDU串
686 2 nLength = ReadComm(ans, 128); // 读应答数据
687 2 // 根据能否找到"+CMS ERROR"决定成功与否
688 2 if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
689 2 {
690 3 return TRUE;
691 3
692 3 }
693 2 }
694 1
695 1 return FALSE;
696 1 }
697
698
699
700
701 // 读取短消息
702 // 用+CMGL代替+CMGR,可一次性读出全部短消息
703 // pMsg: 短消息缓冲区,必须足够大
C51 COMPILER V7.50 SMS 01/26/2007 17:53:09 PAGE 13
704 // 返回: 短消息条数
705 int gsmReadMessage(SM_PARAM* pMsg)
706 {
707 1 int xdata nLength; // 串口收到的数据长度
708 1 int xdata nMsg; // 短消息计数值
709 1
710 1 char xdata* ptr; // 内部用的数据指针
711 1 char xdata cmd[16]; // 命令串
712 1 char xdata ans[512]; // 应答串
713 1
714 1 nMsg = 0;
715 1 ptr = ans;
716 1 sprintf(cmd, "AT+CMGR=%d\r",pMsg->index); // 生成命令
717 1 WriteComm(cmd, strlen(cmd)); // 输出命令串
718 1 nLength = ReadComm(ans, 256); // 读应答数据
719 1
720 1 // 根据能否找到"+CMS ERROR"决定成功与否
721 1 if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
722 1 {
723 2
724 2 //读取一条短消息, 以"+CMGR:"开头
725 2 if((ptr = strstr(ptr, "+CMGR:")) != NULL)
726 2 {
727 3 #if 1
728 3 ptr += 6; // 跳过"+CMGR:"
729 3 //sscanf(ptr, "%d", &pMsg->index); // 读取序号
730 3 ptr = strstr(ptr, "\r\n"); // 找下一行
731 3 ptr += 2; // 跳过"\r\n"
732 3 gsmDecodePdu(ptr, pMsg); // PDU串解码 1
733 3 nMsg++;
734 3 #endif
735 3 }
736 2
737 2 }
738 1 return nMsg;
739 1
740 1 #if 0
char* ptr; // 内部用的数据指针
char cmd[16]; // 命令串
char ans[1024]; // 应答串
nMsg = 0;
ptr = ans;
sprintf(cmd, "AT+CMGL\r"); // 生成命令
WriteComm(cmd, strlen(cmd)); // 输出命令串
nLength = ReadComm(ans, 1024); // 读应答数据
// 根据能否找到"+CMS ERROR"决定成功与否
if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
{
// 循环读取每一条短消息, 以"+CMGL:"开头
while((ptr = strstr(ptr, "+CMGL:")) != NULL)
{
ptr += 6; // 跳过"+CMGL:"
sscanf(ptr, "%d", &pMsg->index); // 读取序号
TRACE(" index=%d\n",pMsg->index);
ptr = strstr(ptr, "\r\n");
// 找下一行
ptr += 2; // 跳过"\r\n"
gsmDecodePdu(ptr, pMsg); // PDU串解码
pMsg++; // 准备读下一条短消息
nMsg++; // 短消息计数加1
}
}
C51 COMPILER V7.50 SMS 01/26/2007 17:53:09 PAGE 14
return nMsg;
#endif
769 1 }
770
771
772
773
774 // 删除短消息
775 // index: 短消息序号,从1开始
776 BOOL gsmDeleteMessage(int xdata index)
*** WARNING C258 IN LINE 776 OF SMS.C: 'index': mspace on parameter ignored
777 {
778 1 int xdata nLength; // 串口收到的数据长度
779 1 char xdata cmd[16]; // 命令串
780 1 char xdata ans[128]; // 应答串
781 1
782 1 sprintf(cmd, "AT+CMGD=%d\r", index); // 生成命令
783 1 // 输出命令串
784 1 WriteComm(cmd, strlen(cmd));
785 1 // 读应答数据
786 1 nLength = ReadComm(ans, 128);
787 1 // 根据能否找到"+CMS ERROR"决定成功与否
788 1 if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
789 1 {
790 2 return TRUE;
791 2 }
792 1
793 1 return FALSE;
794 1 }
795
796
797
798
799
800 #if 0
/*
以上发送AT命令过程中用到了WriteComm和ReadComm函数,它们是用来读写串口的,依赖于具体的操作系统。
在Windows环境下,除了用MSComm控件,以及某些现成的串口通信类之外,也可以简单地调用一些Windows API用实现。
以下是利用API实现的主要代码,注意我们用的是超时控制的同步(阻塞)模式。
*/
// 串口设备句柄
HANDLE hComm; // 打开串口
// pPort: 串口名称或设备路径,可用"COM1"或"\\.\COM1"两种方式,建议用后者
// nBaudRate: 波特率
// nParity: 奇偶校验
// nByteSize: 数据字节宽度
// nStopBits: 停止位
BOOL OpenComm(const char* pPort, int nBaudRate, int nParity, int nByteSize, int nStopBits)
{
DCB dcb; // 串口控制块
COMMTIMEOUTS timeouts =
{
// 串口超时控制参数
100, // 读字符间隔超时时间: 100 ms
1, // 读操作时每字符的时间: 1 ms (n个字符总共为n ms)
500, // 基本的(额外的)读超时时间: 500 ms
1, // 写操作时每字符的时间: 1 ms (n个字符总共为n ms)
100
C51 COMPILER V7.50 SMS 01/26/2007 17:53:09 PAGE 15
}; // 基本的(额外的)写超时时间: 100 ms
hComm = CreateFile(
pPort, // 串口名称或设备路径
GENERIC_READ GENERIC_WRITE, // 读写方式
0, // 共享方式:独占
NULL, // 默认的安全描述符
OPEN_EXISTING, // 创建方式
0, // 不需设置文件属性
NULL); // 不需参照模板文件
if(hComm == INVALID_HANDLE_VALUE) return FALSE; // 打开串口失败
GetCommState(hComm, &dcb); // 取DCB
dcb.BaudRate = nBaudRate;
dcb.ByteSize = nByteSize;
dcb.Parity = nParity;
dcb.StopBits = nStopBits;
SetCommState(hComm, &dcb); // 设置DCB
SetupComm(hComm, 4096, 1024); // 设置输入输出缓冲区大小
SetCommTimeouts(hComm, &timeouts); // 设置超时
return TRUE;
}
// 关闭串口
BOOL CloseComm()
{
return CloseHandle(hComm);
}
#endif
855
856
857 // 写串口
858 // pData: 待写的数据缓冲区指针
859 // nLength: 待写的数据长度
860 void WriteComm(char xdata* pData, int xdata nLength)
*** WARNING C258 IN LINE 860 OF SMS.C: 'nLength': mspace on parameter ignored
861 {
862 1 putbytes(pData,nLength);
863 1 }
864
865
866 // 读串口
867 // pData: 待读的数据缓冲区指针
868 // nLength: 待读的最大数据长度
869 // 返回: 实际读入的数据长度
870 int ReadComm(char xdata* pData, int xdata nLength)
*** WARNING C258 IN LINE 870 OF SMS.C: 'nLength': mspace on parameter ignored
871 {
872 1 int xdata nNumRead; // 串口收到的数据长度
873 1 ReadSerail(pData, nLength, &nNumRead,3);
*** WARNING C214 IN LINE 873 OF SMS.C: 'Argument': conversion: pointer to non-pointer
874 1 return nNumRead;
875 1
876 1 }
877
878
879
C51 COMPILER V7.50 SMS 01/26/2007 17:53:09 PAGE 16
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION gsmModule_init (BEGIN)
; SOURCE LINE # 72
; SOURCE LINE # 73
; SOURCE LINE # 75
0000 D2B3 SETB GSM_PWON
; SOURCE LINE # 76
0002 7F05 MOV R7,#05H
0004 120000 E LCALL _delay_ms
; SOURCE LINE # 77
0007 C2B3 CLR GSM_PWON
; SOURCE LINE # 78
0009 7FC8 MOV R7,#0C8H
000B 120000 E LCALL _delay_ms
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -