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

📄 smsdlg.cpp

📁 该程序为VC环境下的基于西门子TC35I的短信收发程序,采用USB接口与模块连接,USB的驱动采用CP2010的驱动程序,
💻 CPP
字号:
// smsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sms.h"
#include "smsDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSmsDlg dialog

CSmsDlg::CSmsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSmsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSmsDlg)
	m_content = _T("");
	m_comnumber = -1;
	m_mobilenumber = _T("");
	m_centernumber = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//m_temp="8613800100500";
}

void CSmsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSmsDlg)
	DDX_Control(pDX, IDC_MODEM, m_modem);
	DDX_Control(pDX, IDC_SEND, m_send);
	DDX_Control(pDX, IDC_HANDRECE, m_handrece);
	DDX_Control(pDX, IDC_COMOPEN, m_comopen);
	DDX_Control(pDX, IDC_COMCLOSE, m_comclose);
	DDX_Control(pDX, IDC_CLEARMOBILE, m_clearmobile);
	DDX_Control(pDX, IDC_CLEAR, m_clear);
	DDX_Control(pDX, IDC_RECEIVELIST, m_list);
	DDX_Text(pDX, IDC_CONTENT, m_content);
	DDX_CBIndex(pDX, IDC_COM, m_comnumber);
	DDX_Text(pDX, IDC_MOBILENUMBER, m_mobilenumber);
	DDX_Text(pDX, IDC_centernumber, m_centernumber);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSmsDlg, CDialog)
	//{{AFX_MSG_MAP(CSmsDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_COMOPEN, OnComopen)
	ON_BN_CLICKED(IDC_COMCLOSE, OnComclose)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_CLEARMOBILE, OnClearmobile)
	ON_BN_CLICKED(IDC_HANDRECE, OnHandrece)
	ON_BN_CLICKED(IDC_INTRODUCTION, OnIntroduction)
	ON_BN_CLICKED(IDC_HELPTECH, OnHelptech)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_SETNUM, OnSetnum)
	ON_BN_CLICKED(IDC_MODEM, OnModem)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSmsDlg message handlers

BOOL CSmsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	totalnum=0;
    m_list.InsertColumn(0,"发信人号码",LVCFMT_LEFT,100);
	m_list.InsertColumn(1,"发信时间",LVCFMT_LEFT,140);
	m_list.InsertColumn(2,"短信内容",LVCFMT_LEFT,800);
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	//m_list.SetBkColor(RGB(160,160,90));
	//m_list.SetBkColor(RGB(0,0,0));
	m_list.SetBkColor(RGB(100,150,180));
	m_list.SetTextColor(0xff0000);
	m_comnumber=0;
    UpdateData(FALSE);
	m_comclose.EnableWindow(FALSE);
    m_handrece.EnableWindow(FALSE);
    m_send.EnableWindow(FALSE);
	m_modem.EnableWindow(FALSE);
    m_clearmobile.EnableWindow(FALSE);
	m_clear.EnableWindow(FALSE);
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	SetWindowText("短信收发小灵通");
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSmsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSmsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

bool CSmsDlg::OpenComm(const char *pPort, int nBaudRate, int nParity, int nByteSize, int nStopBits)
{DCB dcb;        // 串口控制块
     COMMTIMEOUTS timeouts = {    // 串口超时控制参数
                              200, // 读字符间隔超时时间: 100 ms
                              1,   // 读操作时每字符的时间: 1 ms (n个字符总共为n ms)
                            200, // 基本的(额外的)读超时时间: 500 ms
                              1,   // 写操作时每字符的时间: 1 ms (n个字符总共为n ms)
                              10}; // 基本的(额外的)写超时时间: 100 ms
     
     m_hCom = CreateFile(pPort,    // 串口名称或设备路径
             GENERIC_READ | GENERIC_WRITE,    // 读写方式
            0,               // 共享方式:独占
            NULL,            // 默认的安全描述符
             OPEN_EXISTING,   // 创建方式
            0,               // 不需设置文件属性
           NULL);           // 不需参照模板文件
     
    if(m_hCom == INVALID_HANDLE_VALUE) return FALSE;        // 打开串口失败
     
     GetCommState(m_hCom, &dcb);        // 取DCB
     
     dcb.BaudRate = nBaudRate;
     dcb.ByteSize = nByteSize;
     dcb.Parity = nParity;
     dcb.StopBits = nStopBits;
     
     SetCommState(m_hCom, &dcb);        // 设置DCB
     
     SetupComm(m_hCom, 4096, 1024);     // 设置输入输出缓冲区大小
     
     SetCommTimeouts(m_hCom, &timeouts);    // 设置超时
     return TRUE;
 


}

void CSmsDlg::OnComopen() 
{
    UpdateData(TRUE);
    CString strCom;
   	char temp[20];
	char cmd[20];
	strCom.Format("COM%1d",m_comnumber+1);
    ComOpen=OpenComm(strCom,9600,0,8,1);
	if(ComOpen)
	{sprintf(cmd,"AT\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);	
	sprintf(cmd,"AT\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);	
	sprintf(cmd,"ATE0\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);
	sprintf(cmd,"AT+CMGF=0\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);
    sprintf(cmd,"AT+CNMI=2,1,0,0,0\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);
	m_comclose.EnableWindow(true);
    m_handrece.EnableWindow(true);
    m_modem.EnableWindow(true);
    m_send.EnableWindow(true);
    m_clearmobile.EnableWindow(true);
	m_clear.EnableWindow(true);
	m_comopen.EnableWindow(false);
	OnSetnum(); 
	SetTimer(1,60000,NULL);
	}
	else
	{AfxMessageBox("您所选择的串口已经打开,请您重新选择一个串口");
	}
}

void CSmsDlg::OnComclose() 
{
  CloseComm();
  m_comopen.EnableWindow(TRUE);
  m_comclose.EnableWindow(FALSE);
  m_handrece.EnableWindow(FALSE);
  m_send.EnableWindow(FALSE);
  m_modem.EnableWindow(FALSE);
  m_clearmobile.EnableWindow(FALSE);
  m_clear.EnableWindow(FALSE);
  KillTimer(1);	
}

bool CSmsDlg::CloseComm()
{ 
	return CloseHandle(m_hCom);
}

void CSmsDlg::OnClear() 
{
  m_content.Format(""); 
  totalnum=0;
  UpdateData(false);
	
}

void CSmsDlg::OnClearmobile() 
{
    char cmd[20];
    char temp[20];
	sprintf(cmd,"AT+CMGD=1,4\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,20);
	OnHandrece();	
}

void CSmsDlg::OnHandrece() 
{
   SM_PARAM *sm_param_temp;
	sm_param_temp= new SM_PARAM[50];
	int numofsms;
	int i;
	bool numtest=false;
	m_list.DeleteAllItems();
	CString str;
    numofsms=gsmReadMessage(m_hCom,sm_param_temp);
	totalnum=numofsms;
	for(i=0;i<=(numofsms-1);i++)
	{m_list.InsertItem(LVIF_TEXT|LVIF_STATE,i,str,0,LVIS_SELECTED,0,0);
	 str.Format("%s",sm_param_temp->TPA); 
	 //str=str.Mid(2,11);
	 m_list.SetItemText(i,0,str);
     str.Format("%s",sm_param_temp->TP_SCTS);  
	 CString strtime;
     strtime.Format("20%s-%s-%s %s:%s:%s",str.Mid(0,2),str.Mid(2,2),str.Mid(4,2),str.Mid(6,2),str.Mid(8,2),str.Mid(10,2)); 	
	 m_list.SetItemText(i,1,strtime);
	 str.Format("%s",sm_param_temp->TP_UD); 	
	 m_list.SetItemText(i,2,str);
	 sm_param_temp++;
	 numtest=true;
	}
	if(!numtest)
	AfxMessageBox("SIM卡里没有短消息");
	}

void CSmsDlg::OnIntroduction() 
{
    char sysdir[MAX_PATH];
	GetSystemDirectory(sysdir,MAX_PATH);
	size_t len=strlen(sysdir);
	while(len--)
	{
		if(sysdir[len]=='\\')break;
	}
	sysdir[len+1]='\0';
	strcat(sysdir,"explorer.exe");
	::ShellExecute(NULL,_T("open"),sysdir,"http://www.tchygprs.com.cn",NULL,SW_SHOWNORMAL);
	
}

void CSmsDlg::OnHelptech() 
{char sysdir[MAX_PATH];
	GetSystemDirectory(sysdir,MAX_PATH);
	size_t len=strlen(sysdir);
	while(len--)
	{
		if(sysdir[len]=='\\')break;
	}
	sysdir[len+1]='\0';
	strcat(sysdir,"explorer.exe");
	::ShellExecute(NULL,_T("open"),sysdir,"http://www.tchygprs.com.cn/other/support.htm",NULL,SW_SHOWNORMAL);
	

}

void CSmsDlg::OnSend() 
{
    bool result;
    UpdateData(true);
	SM_PARAM *sm_param_temp;   
	sm_param_temp= new SM_PARAM;
    sm_param_temp->TP_DCS=GSM_UCS2;
	sm_param_temp->TP_PID='0';
	strcpy(sm_param_temp->SCA,m_temp);
   	m_mobilenumber="86"+m_mobilenumber;
	
	strcpy(sm_param_temp->TPA,m_mobilenumber);
	
	strcpy(sm_param_temp->TP_UD,m_content);
	result=gsmSendMessage(m_hCom,sm_param_temp);
	if(!result)
	{//错误处理
	}	
}

void CSmsDlg::OnTimer(UINT nIDEvent) 
{
    SM_PARAM *sm_param_temp;
	sm_param_temp= new SM_PARAM[10];
	int numofsms;
	int i;
	bool numtest=false;
	CString str;
    numofsms=gsmReadOneMessage(m_hCom,sm_param_temp);
	for(i=1;i<=numofsms;i++)
	{m_list.InsertItem(LVIF_TEXT|LVIF_STATE,totalnum,str,0,LVIS_SELECTED,0,0);
	 str.Format("%s",sm_param_temp->TPA);
	 str=str.Mid(2,11);
	 m_list.SetItemText(totalnum,0,str);
	 str.Format("%s",sm_param_temp->TP_SCTS);
	 CString strtime;
     strtime.Format("20%s-%s-%s %s:%s:%s",str.Mid(0,2),str.Mid(2,2),str.Mid(4,2),str.Mid(6,2),str.Mid(8,2),str.Mid(10,2)); 	
	 m_list.SetItemText(totalnum,1,strtime);
	 str.Format("%s",sm_param_temp->TP_UD); 	
	 m_list.SetItemText(totalnum,2,str);
	 sm_param_temp++;
	 numtest=true;
	 totalnum++;
	}
	CDialog::OnTimer(nIDEvent);
}

void CSmsDlg::OnSetnum() 
{   char temp[40];
	char cmd[20];
	CString str;
	//str.Format("%s",sm_param_temp->TPA);
	//UpdateData(true);
	//m_temp="86"+m_centernumber;
	sprintf(cmd,"AT+CSCA?\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
    str.Format("%s",temp);
	str=str.Mid(11,13);
	m_temp=str;
    m_centernumber=m_temp.Mid(2,11);
    UpdateData(false);
	}

void CSmsDlg::OnModem() 
{   char temp[40];
	char cmd[40];
	CString str;
	sprintf(cmd,"AT+CGCLASS=\"B\"\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
	sprintf(cmd,"AT+CGDCONT=1,\"IP\",\"CMNET\"\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
    sprintf(cmd,"AT+CSQ\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
    str.Format("%s",temp);
	str=str.Mid(8,2);
	int test=atoi(str);
	if(10<=test&&test<=30)
	{
	sprintf(cmd,"AT+CGACT=1,1\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);

    sprintf(cmd,"AT+CGREG?\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
    str.Format("%s",temp);
	str=str.Mid(12,1);
    int test;
	test=atoi(str);
	if(test!=1)
	{
	 AfxMessageBox("您没有开通这项业务");
	 return;
	}
	sprintf(cmd,"AT+IPR=115200\r");
    WriteComm(m_hCom,cmd,strlen(cmd)); 
    ReadComm(m_hCom,temp,40);
    AfxMessageBox("连接已经建立,请您对您的网络进行设置!");
	m_comopen.EnableWindow(TRUE);
    m_comclose.EnableWindow(FALSE);
    m_handrece.EnableWindow(FALSE);
    m_send.EnableWindow(FALSE);
    m_clearmobile.EnableWindow(FALSE);
    m_clear.EnableWindow(FALSE);
	return;
	}
	else
	{AfxMessageBox("线路状态不好,不能建立连接");
	 return;
	}
    	
}

⌨️ 快捷键说明

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