⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsmtouartdlg.cpp

📁 GSM模块的串口操作源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    
    // 返回目标字符串长度
    return nDstLength;
}

// PDU编码,用于编制、发送短消息
// pSrc: 源PDU参数指针
// pDst: 目标PDU串指针
// 返回: 目标PDU串长度
int CGsmToUartDlg::GsmEncodePdu(const SM_PARAM *pSrc, char *pDst)
{
    int nLength;             // 内部用的串长度
    int nDstLength;          // 目标PDU串长度
    unsigned char buf[256];  // 内部用的缓冲区
    
    // SMSC地址信息段
    nLength = strlen(pSrc->SCA);    // SMSC地址字符串的长度    
    buf[0] = (char)((nLength & 1) == 0 ? nLength : nLength + 1) / 2 + 1;    // SMSC地址信息长度
    buf[1] = 0x91;        // 固定: 用国际格式号码
    nDstLength = GsmBytes2String(buf, pDst, 2);        // 转换2个字节到目标PDU串
    nDstLength += GsmInvertNumbers(pSrc->SCA, &pDst[nDstLength], nLength);    // 转换SMSC到目标PDU串
    
    // TPDU段基本参数、目标地址等
    nLength = strlen(pSrc->TPA);    // TP-DA地址字符串的长度
    buf[0] = 0x11;            // 是发送短信(TP-MTI=01),TP-VP用相对格式(TP-VPF=10)
    buf[1] = 0;               // TP-MR=0
    buf[2] = (char)nLength;   // 目标地址数字个数(TP-DA地址字符串真实长度)
    buf[3] = 0x91;            // 固定: 用国际格式号码
    nDstLength += GsmBytes2String(buf, &pDst[nDstLength], 4);  // 转换4个字节到目标PDU串
    nDstLength += GsmInvertNumbers(pSrc->TPA, &pDst[nDstLength], nLength); // 转换TP-DA到目标PDU串
    
    // TPDU段协议标识、编码方式、用户信息等
    nLength = strlen(pSrc->TP_UD);    // 用户信息字符串的长度
    buf[0] = pSrc->TP_PID;        // 协议标识(TP-PID)
    buf[1] = pSrc->TP_DCS;        // 用户信息编码方式(TP-DCS)
    buf[2] = 0;            // 有效期(TP-VP)为5分钟
    if(pSrc->TP_DCS == GSM_7BIT)    
    {
        // 7-bit编码方式
        buf[3] = nLength;            // 编码前长度
        nLength = GsmEncode7bit(pSrc->TP_UD, &buf[4], nLength+1) + 4;    // 转换TP-DA到目标PDU串
    }
    else if(pSrc->TP_DCS == GSM_UCS2)
    {
        // UCS2编码方式
        buf[3] = GsmEncodeUcs2(pSrc->TP_UD, &buf[4], nLength);    // 转换TP-DA到目标PDU串
        nLength = buf[3] + 4;        // nLength等于该段数据长度
    }
    else
    {
        // 8-bit编码方式
        buf[3] = GsmEncode8bit(pSrc->TP_UD, &buf[4], nLength);    // 转换TP-DA到目标PDU串
        nLength = buf[3] + 4;        // nLength等于该段数据长度
    }
    nDstLength += GsmBytes2String(buf, &pDst[nDstLength], nLength);        // 转换该段数据到目标PDU串
    
    // 返回目标字符串长度
    return nDstLength;
}

// PDU解码,用于接收、阅读短消息
// pSrc: 源PDU串指针
// pDst: 目标PDU参数指针
// 返回: 用户信息串长度
int CGsmToUartDlg::GsmDecodePdu(const char *pSrc, SM_PARAM *pDst)
{
    int nDstLength;          // 目标PDU串长度
    unsigned char tmp;       // 内部用的临时字节变量
    unsigned char buf[256];  // 内部用的缓冲区
    
    // SMSC地址信息段
    GsmString2Bytes(pSrc, &tmp, 2);    // 取长度
    tmp = (tmp - 1) * 2;    // SMSC号码串长度
    pSrc += 4;              // 指针后移
    GsmSerializeNumbers(pSrc, pDst->SCA, tmp);    // 转换SMSC号码到目标PDU串
    pSrc += tmp;        // 指针后移
    
    // TPDU段基本参数、回复地址等
    GsmString2Bytes(pSrc, &tmp, 2);    // 取基本参数
    pSrc += 2;        // 指针后移
    if(tmp & 0x80)
    {
        // 包含回复地址,取回复地址信息
        GsmString2Bytes(pSrc, &tmp, 2);    // 取长度
        if(tmp & 1) tmp += 1;    // 调整奇偶性
        pSrc += 4;          // 指针后移
        GsmSerializeNumbers(pSrc, pDst->TPA, tmp);    // 取TP-RA号码
        pSrc += tmp;        // 指针后移
    }
    
    // TPDU段协议标识、编码方式、用户信息等
    GsmString2Bytes(pSrc, (unsigned char*)&pDst->TP_PID, 2);    // 取协议标识(TP-PID)
    pSrc += 2;        // 指针后移
    GsmString2Bytes(pSrc, (unsigned char*)&pDst->TP_DCS, 2);    // 取编码方式(TP-DCS)
    pSrc += 2;        // 指针后移
    GsmSerializeNumbers(pSrc, pDst->TP_SCTS, 14);        // 服务时间戳字符串(TP_SCTS) 
    pSrc += 14;       // 指针后移
    GsmString2Bytes(pSrc, &tmp, 2);    // 用户信息长度(TP-UDL)
    pSrc += 2;        // 指针后移
    if(pDst->TP_DCS == GSM_7BIT)    
    {
        // 7-bit解码
        nDstLength = GsmString2Bytes(pSrc, buf, tmp & 7 ? (int)tmp * 7 / 4 + 2 : (int)tmp * 7 / 4);  // 格式转换
        GsmDecode7bit(buf, pDst->TP_UD, nDstLength);    // 转换到TP-DU
        nDstLength = tmp;
    }
    else if(pDst->TP_DCS == GSM_UCS2)
    {
        // UCS2解码
        nDstLength = GsmString2Bytes(pSrc, buf, tmp * 2);        // 格式转换
        nDstLength = GsmDecodeUcs2(buf, pDst->TP_UD, nDstLength);    // 转换到TP-DU
    }
    else
    {
        // 8-bit解码
        nDstLength = GsmString2Bytes(pSrc, buf, tmp * 2);        // 格式转换
        nDstLength = GsmDecode8bit(buf, pDst->TP_UD, nDstLength);    // 转换到TP-DU
    }
    
    // 返回目标字符串长度
    return nDstLength;
}

// 发送短消息
// pSrc: 源PDU参数指针
BOOL CGsmToUartDlg::GsmSendMessage(const SM_PARAM *pSrc)
{
    int nPduLength;        // PDU串长度
    unsigned char nSmscLength;    // SMSC串长度
    int nLength;           // 串口收到的数据长度
    char cmd[16];          // 命令串
    char pdu[512];         // PDU串
    char ans[128];         // 应答串
    
    nPduLength = GsmEncodePdu(pSrc, pdu);    // 根据PDU参数,编码PDU串
    strcat(pdu, "\x01a");        // 以Ctrl-Z结束
    
    GsmString2Bytes(pdu, &nSmscLength, 2);    // 取PDU串中的SMSC信息长度
    nSmscLength++;        // 加上长度字节本身
    
    // 命令中的长度,不包括SMSC信息长度,以数据字节计
    sprintf(cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength);    // 生成命令
    
    WriteComm(cmd, strlen(cmd));    // 先输出命令串
    
    nLength = ReadComm(ans,128);   // 读应答数据
    
    // 根据能否找到"\r\n> "决定成功与否
    if(nLength == 4 && strncmp(ans, "\r\n> ", 4) == 0)
    {
        WriteComm(pdu, strlen(pdu));        // 得到肯定回答,继续输出PDU串
    
        nLength = ReadComm(ans,128);       // 读应答数据

        // 根据能否找到"+CMS ERROR"决定成功与否
        if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
        {
            return TRUE;
        }
    }
    
    return FALSE;
}

// 读取短消息
// 用+CMGL代替+CMGR,可一次性读出全部短消息
// pMsg: 短消息缓冲区,必须足够大
// 返回: 短消息条数
int CGsmToUartDlg::GsmReadMessage(SM_PARAM *pMsg)
{
    int nLength;        // 串口收到的数据长度
    int nMsg;           // 短消息计数值
    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
        }
    }
    
    return nMsg;
}

// 删除短消息
// index: 短消息序号,从1开始
BOOL CGsmToUartDlg::GsmDeleteMessage(const int index)
{
    int nLength;          // 串口收到的数据长度
    char cmd[16];         // 命令串
    char ans[128];        // 应答串
    
    sprintf(cmd, "AT+CMGD=%d\r", index);    // 生成命令
    
    // 输出命令串
    WriteComm(cmd, strlen(cmd));
    
    // 读应答数据
    nLength = ReadComm(ans,128);

    // 根据能否找到"+CMS ERROR"决定成功与否
    if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0)
    {
        return TRUE;
    }
    
    return FALSE;
}

// 写串口
// pData: 待写的数据缓冲区指针
// nLength: 待写的数据长度
void CGsmToUartDlg::WriteComm(CString tmpSend, int nLength)
{
	DWORD dwNumWrite;    // 串口发出的数据长度
	if (!WriteFile(hCom1,(LPCTSTR)tmpSend,(DWORD)nLength,&dwNumWrite,NULL))
	{
		AfxMessageBox("向端口写数据失败!");
		::CloseHandle(hCom1);
		return ;
	}
	// 选定“感兴趣”的事件、信号
	if (!SetCommMask(hCom1,EV_TXEMPTY))
	{
		AfxMessageBox(_T("选择监听信号失败!"));
		::CloseHandle(hCom1);
		return ;
	}
	// 停止对串口的写操作
	if (!PurgeComm(hCom1,PURGE_TXABORT))
	{
		AfxMessageBox(_T("停止写端口操作时发生错误!"));
		::CloseHandle(hCom1);
		return ;
	}
	// 将缓冲区中的数据发送到串口
	if (!FlushFileBuffers(hCom1))
	{
		AfxMessageBox(_T("处理缓冲数据时发生错误!"));
		::CloseHandle(hCom1);
		return ;
	}
}

// 读串口
// pData: 待读的数据缓冲区指针
// nLength: 待读的最大数据长度
// 返回: 实际读入的数据长度
int CGsmToUartDlg::ReadComm(void * pData , int nLength)
{
    DWORD dwNumRead;    // 串口收到的数据长度
    ReadFile(hCom1, pData, (DWORD)nLength, &dwNumRead, NULL);
    return (int)dwNumRead;
}

// 8-bit编码
// pSrc: 源字符串指针
// pDst: 目标编码串指针
// nSrcLength: 源字符串长度
// 返回: 目标编码串长度
int CGsmToUartDlg::GsmEncode8bit(const char *pSrc, unsigned char *pDst, int nSrcLength)
{
    for(int i=0; i<nSrcLength; i++)
    {
        *pDst++ =*pSrc;
        pSrc++;
    }
	return nSrcLength;
}

// 8-bit解码
// pSrc: 源编码串指针
// pDst: 目标字符串指针
// nSrcLength: 源编码串长度
// 返回: 目标字符串长度
int CGsmToUartDlg::GsmDecode8bit(const unsigned char *pSrc, char *pDst, int nSrcLength)
{
    for(int i=0; i<nSrcLength; i++)
    {
        *pDst++ =*pSrc;
        pSrc++;
    }
	return nSrcLength;
}

void CGsmToUartDlg::OnHelpAt() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg dlg;
	dlg.DoModal();
	
}

void CGsmToUartDlg::OnLoadsms() 
{
	// TODO: Add your control notification handler code here
	char * filters="文本文档(*.txt)|*.txt | 全部文件(*.*)|*.*";
	CFileDialog LoadSmsDlg(TRUE,NULL,"*.txt",NULL,filters);
	int	result=LoadSmsDlg.DoModal();
	if(result==IDOK)
	{
		m_nViewSmsSend="";
		UpdateData(FALSE);
		CString OpenFilePath;
		OpenFilePath=LoadSmsDlg.GetPathName();
		//m_nViewSmsSend=OpenFilePath;
		CFile SmsFile;
		if (!SmsFile.Open (OpenFilePath, CFile::modeRead))
		{
			AfxMessageBox("无法打开文件!");
			return ;
		}
		int FileLength=SmsFile.GetLength();
		char tbuf[102400];
		SmsFile.Read(tbuf,FileLength);
		for(int i=0;i<FileLength;i++)
			m_nViewSmsSend+=*(tbuf+i);
		UpdateData(FALSE);
	}
}

void CGsmToUartDlg::OnSavesms() 
{
	// TODO: Add your control notification handler code here
	char * filters="文本文档(*.txt)|*.txt | 全部文件(*.*)|*.*";
	CFileDialog SaveSmsDlg(FALSE,NULL,"*.txt",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,filters);
	int	result=SaveSmsDlg.DoModal();
	if(result==IDOK)
	{
		CString SaveFilePath;
		SaveFilePath=SaveSmsDlg.GetPathName();
		//m_nViewSmsSend=OpenFilePath;
		CFile SmsFile;
		if (!SmsFile.Open(SaveFilePath, CFile::modeCreate | CFile::modeWrite) )
		{
			AfxMessageBox("无法写文件!");
			return ;
		}
		int FileLength=m_nViewSmsRead.GetLength();	
		//for(int i=0;i<FileLength;i++)

		//m_nViewSmsSend+=*(tbuf+i);
		SmsFile.Write(m_nViewSmsRead,FileLength);
	}	
}

void CGsmToUartDlg::OnViewnotreadsms() 
{
	// TODO: Add your control notification handler code here
	
}

void CGsmToUartDlg::OnDeletesms() 
{
	// TODO: Add your control notification handler code here
	
}

void CGsmToUartDlg::OnSetsmssernumber() 
{
	// TODO: Add your control notification handler code here
	
}

void CGsmToUartDlg::OnGetsmssernumber() 
{
	// TODO: Add your control notification handler code here
	
}

void CGsmToUartDlg::OnClearatresult() 
{
	// TODO: Add your control notification handler code here
	m_nResultAt="";
	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -