?? smsdlg.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 + -