📄 gprsmsg.cpp
字号:
// GPRSMSG.cpp : implementation file
//
#include "stdafx.h"
#include "Magic2410.h"
#include "GPRSMSG.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// GPRSMSG dialog
#include "gpio.h"
extern HANDLE m_hFileGpio;
GPRSMSG::GPRSMSG(CWnd* pParent /*=NULL*/)
: CDialog(GPRSMSG::IDD, pParent)
{
//{{AFX_DATA_INIT(GPRSMSG)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void GPRSMSG::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(GPRSMSG)
DDX_Control(pDX, IDC_CLOSE_GPRS, m_closeGPRS);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(GPRSMSG, CDialog)
//{{AFX_MSG_MAP(GPRSMSG)
ON_BN_CLICKED(IDC_SENDMSG, OnSendmsg)
ON_BN_CLICKED(IDC_SET_MSGCENTR_TELCODE, OnSetMsgcentrTelcode)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_CLOSE_GPRS, OnCloseGprs)
ON_BN_CLICKED(IDC_BTN_DIAL, OnBtnDial)
ON_BN_CLICKED(IDC_BTN_TAKE, OnBtnTake)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// GPRSMSG message handlers
/*******************************************************************************************
函数名称: CALLBACK CGPRSMsgDlg::OnGPRSRecv
描 述: GPRS 接收回调函数, 当有电话打入或对方挂机时, 将执行该函数
输入参数: DWORD UserParam: 用户在调用 GPRS_Init() 函数时传入的参数.
DWORD Status : GPRS 状态
CString strData: 状态对应的字符串, 如果有电话打入, 则该字符为来电号码
输出参数: 无
返 回: 无
********************************************************************************************/
void CALLBACK GPRSMSG::OnGPRSRecv(DWORD UserParam, DWORD Status, CString strData)
{
GPRSMSG *pDlg = (GPRSMSG *)UserParam;
if (Status == GPRS_STATUS_RECEIVE_CALL) /* 有电话打入 */
{
if (strData == _T(""))
{
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, _T("有电话打进..."));
pDlg->UpdateData(FALSE);
}
else
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, strData); /* 来电显示 */
}
if (Status == GPRS_STATUS_OTHER_SIDE_HANDUP)
{ /* 对方已挂机 */
CButton *ButTake = (CButton *)pDlg->GetDlgItem(IDC_BTN_TAKE);
CButton *ButDial = (CButton *)pDlg->GetDlgItem(IDC_BTN_DIAL);
if ((ButDial->GetState() & 0x003) == 1)
{ /* 本机拨出的情况 */
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, _T("对方已挂机."));
ButTake->SetWindowText(_T("接听")); /* 恢复按键原状态 */
ButDial->SetWindowText(_T("拨号"));
ButTake->SetCheck(0);
ButDial->SetCheck(0);
Sleep(2000);
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, _T(""));
}
else
{ /* 来电的情况 */
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, _T("对方已挂机."));
ButTake->SetWindowText(_T("接听")); /* 恢复按键原状态 */
ButDial->SetWindowText(_T("拨号"));
ButTake->SetCheck(0);
ButDial->SetCheck(0);
Sleep(2000);
pDlg->SetDlgItemText(IDC_RECVMSG_TELCODE, _T(""));
}
}
}
/*******************************************************************************************
函数名称: OnSendmsg
描 述: 发送短信
********************************************************************************************/
void GPRSMSG::OnSendmsg()
{
// TODO: Add your control notification handler code here
CString strTelCode, strMsg;
GetDlgItemText(IDC_SENDMSG_TELCODE, strTelCode); /* 获取发送短信电话号码及内容*/
GetDlgItemText(IDC_EDIT_SENDMSG, strMsg);
if ((strTelCode == "") || (strMsg == ""))
{ /* 判断输入内容是否正确 */
MessageBox(_T("电话号码或短信内容不能为空."));
return;
}
BOOL ret = m_GPRS->GPRS_SendShortMessage(strTelCode, strMsg); /* 发送短信 */
if (ret == TRUE)
MessageBox(_T("短信发送成功."));
else
MessageBox(_T("短信发送失败."));
}
/*******************************************************************************************
函数名称: GPRSDIAL::OnDial()
描 述: 单击 "拨号" 按键事件代码
********************************************************************************************/
void GPRSMSG::OnBtnDial()
{
BOOL ret;
CString strDialCode;
CButton *Key = (CButton *)GetDlgItem(IDC_BTN_DIAL);
GetDlgItemText(IDC_SENDMSG_TELCODE, strDialCode); /* 获取拨号号码 */
UINT state = Key->GetState();
if ((state & 0x0003) == 1) /* 按键按下 */
{
if (strDialCode == _T("")) /* 输入号码有误 */
{
MessageBox(_T("请输入拨号号码."));
Key->SetCheck(0); /* 恢复按键原状态 */
Key->SetWindowText(_T("拨号"));
return;
}
ret = m_GPRS->GPRS_DialUp(strDialCode);
if (ret == FALSE) /* 拨号失败 */
{
Key->SetCheck(0);
Key->SetWindowText(_T("拨号")); /* 恢复按键原状态 */
return;
}
Key->SetWindowText(_T("挂机")); /* 提示下次操作 */
}
else
{ /* 按键未按下 */
ret = m_GPRS->GPRS_DialDown();
if (ret == FALSE)
{ /* 挂机失败 */
MessageBox(_T("挂机失败."));
}
Key->SetWindowText(_T("拨号")); /* 提示下次操作 */
}
}
void GPRSMSG::OnSetMsgcentrTelcode()
{
// TODO: Add your control notification handler code here
CString strCode;
GetDlgItemText(IDC_MSGCENTR_TELCODE, strCode);
BOOL ret = m_GPRS->GPRS_SetShortMSGCenterTel(strCode); /* 发送短信 */
if (ret == TRUE)
MessageBox(_T("设置短信中心号码成功."));
else
MessageBox(_T("设置短信中心号码失败."));
}
void GPRSMSG::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
BOOL ret;
CString strTelCode, strMsg;
char buf[6],i;
ret = m_GPRS->GPRS_ReadShortMessage(1, &strTelCode, &strMsg); /* 读取第 0 条短信 */
if (ret == TRUE)
{
for (int i = 0; i < strMsg.GetLength(); i++)
{
if ((char)strMsg.GetAt(i) == '\n') /* 有过行字符 */
if ((char)strMsg.GetAt(i - 1) != '\r') /* 但没有回车字符 */
{
strMsg.Insert(i, '\r'); /* 插入回车符 */
}
}
SetDlgItemText(IDC_RECVMSG_TELCODE, strTelCode); /* 显示电话号码 */
SetDlgItemText(IDC_DISP_RECVMSG, strMsg); /* 显示短信内容 */
m_GPRS->GPRS_DeleteShortMsg(1); /* 删除短信 */
}
for (i = 0; i < 6; i++)
buf[i] = (char)strMsg.GetAt(i); /* 取出短信的前6个字符 */
if( (buf[0] == 'Z') || (buf[0] == 'z') )
if( (buf[1] == 'Y') || (buf[1] == 'y') )
if( (buf[2] == 'L') || (buf[2] == 'l') )
if( (buf[3] == 'E') || (buf[3] == 'e') )
if( (buf[4] == 'D') || (buf[4] == 'd') )
if( (buf[5] >= '1') && (buf[5] <= '4') )
{
LEDRight(buf[5] - '0'); /* 点亮相应的LED */
}
//SetTimer(1, 1000, NULL); /* 每 1 秒读取一次短信 */
CDialog::OnTimer(nIDEvent);
}
/*******************************************************************************************
函数名称: CGPRSCtrlDlg::LEDRight
描 述: 点亮实验箱上4个LED
输入参数: DWORD num: LED 编号, 取值 1 ~ 4, 对应4个LED, 取值为 0 时, 熄灭所有LED
输出参数: 无
返 回: FALSE: 失败 TRUE: 成功
********************************************************************************************/
BOOL GPRSMSG::LEDRight(DWORD num)
{
BOOL ret = FALSE;
BYTE pinnum;
// 设置 LED1 和 LED2 , 即 GPE11 和 GPE12 为输出口
DWORD GPEMask = (0x01 << 11) + (0x01 << 12);
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPE_SET_MULTI_PIN_OUT, &GPEMask, 1, NULL, NULL, NULL, NULL);
if (ret != TRUE)
{
CloseHandle(m_hFileGpio);
m_hFileGpio = INVALID_HANDLE_VALUE;
return FALSE;
}
// 设置 LED3 和 LED4, 即 GPH4 和 GPH6 为输出口
DWORD GPHMask = (0x01 << 4) + (0x01 << 6);
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPH_SET_MULTI_PIN_OUT, &GPHMask, 1, NULL, NULL, NULL, NULL);
if (ret != TRUE)
{
CloseHandle(m_hFileGpio);
m_hFileGpio = INVALID_HANDLE_VALUE;
return FALSE;
}
// 熄灭所有 LED
pinnum = 11;
ret = DeviceIoControl(m_hFileGpio, IOCTL_GPE_CLR_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
if (ret = FALSE) return ret;
pinnum = 12;
ret = DeviceIoControl(m_hFileGpio, IOCTL_GPE_CLR_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
if (ret = FALSE) return ret;
pinnum = 4;
ret = DeviceIoControl(m_hFileGpio, IOCTL_GPH_CLR_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
if (ret = FALSE) return ret;
pinnum = 6;
ret = DeviceIoControl(m_hFileGpio, IOCTL_GPH_CLR_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
if (ret = FALSE) return ret;
switch(num)
{
case 0:
ret = TRUE;
break;
case 1: /* 点亮 LED1, 即置 GPE11 为高电平 */
pinnum = 11;
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPE_SET_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
break;
case 2: /* 点亮 LED2, 即置 GPE12 为高电平 */
pinnum = 12;
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPE_SET_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
break;
case 3: /* 点亮 LED3, 即置 GPH4 为高电平 */
pinnum = 4;
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPH_SET_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
break;
case 4: /* 点亮 LED4, 即置 GPH6 为高电平 */
pinnum = 6;
ret = ::DeviceIoControl(m_hFileGpio, IOCTL_GPH_SET_PIN, &pinnum, 1, NULL, NULL, NULL, NULL);
break;
default:
break;
}
return ret;
}
#include "DlgBeep.h"
void GPRSMSG::OnCloseGprs()
{
CButton *BtnSetMsgCentre = (CButton *)GetDlgItem(IDC_SET_MSGCENTR_TELCODE);
CButton *BtnDial = (CButton *)GetDlgItem(IDC_BTN_DIAL);
CButton *BtnSendMsg = (CButton *)GetDlgItem(IDC_SENDMSG);
CButton *BtnTake = (CButton *)GetDlgItem(IDC_BTN_TAKE);
if(!GPRSIsOpen)
{
m_GPRS = new CGPRS;
m_GPRS->OnGPRSRecv = OnGPRSRecv; /* 设置回调函数 */
BOOL ret = m_GPRS->GPRS_Init(_T("COM1:"), 115200, m_gprsmsg);
if (ret == FALSE)
{
delete m_GPRS;
MessageBox(_T("GPRS 初始化失败, 请检查是否安装正确, 是否已按GPRS模块上的RST按键."));
return;
}
m_GPRS->GPRS_DeleteShortMsg(1); /* 删除前 1 条短信*/
SetTimer(1, 1000, NULL); /* 每 1 秒读取一次短信 */
m_closeGPRS.SetWindowText (_T("关闭GPRS"));
GPRSIsOpen=TRUE;
BtnSetMsgCentre->EnableWindow(TRUE);
BtnDial->EnableWindow(TRUE);
BtnSendMsg->EnableWindow(TRUE);
BtnTake->EnableWindow(TRUE);
pDlgGPIO->StopBeepDlgled(); /* 关闭GPIO页的LED线程 */
LEDRight(0); /* 熄灭所有LED */
ret = m_GPRS->GPRS_SetShortMSGCenterTel(_T("+8613800200500")); /* 设置短信中心号码 */
if (ret == FALSE)
MessageBox(_T("设置短信中心号码失败."));
}
else
{
delete m_GPRS;
KillTimer(1);
m_closeGPRS.SetWindowText (_T("打开GPRS"));
GPRSIsOpen=FALSE;
BtnSetMsgCentre->EnableWindow(FALSE);
BtnDial->EnableWindow(FALSE);
BtnSendMsg->EnableWindow(FALSE);
BtnTake->EnableWindow(FALSE);
}
}
BOOL GPRSMSG::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
GPRSIsOpen=FALSE;
m_gprsmsg=(DWORD)this;
CButton *BtnSetMsgCentre = (CButton *)GetDlgItem(IDC_SET_MSGCENTR_TELCODE);
CButton *BtnDial = (CButton *)GetDlgItem(IDC_BTN_DIAL);
CButton *BtnSendMsg = (CButton *)GetDlgItem(IDC_SENDMSG);
CButton *BtnTake = (CButton *)GetDlgItem(IDC_BTN_TAKE);
BtnSetMsgCentre->EnableWindow(FALSE);
BtnDial->EnableWindow(FALSE);
BtnSendMsg->EnableWindow(FALSE);
BtnTake->EnableWindow(FALSE);
SetDlgItemText(IDC_MSGCENTR_TELCODE, _T("+8613800200500"));
SetDlgItemText(IDC_EDIT_SENDMSG, _T("Hello,This is MagicARM2410."));
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/*******************************************************************************************
函数名称: GPRSDIAL::OnTake()
描 述: 单击 "接听" 按键事件代码
********************************************************************************************/
void GPRSMSG::OnBtnTake()
{
BOOL ret;
CButton *Key = (CButton *)GetDlgItem(IDC_BTN_TAKE);
UINT state = Key->GetState();
if ((state & 0x0003) == 1) /* 按键按下 */
{
ret = m_GPRS->GPRS_TakeTheCall(); /* 接听来电 */
if (ret == FALSE)
{ /* 接听失败 */
Key->SetCheck(0);
Key->SetWindowText(_T("接听")); /* 按键恢复原来的状态 */
MessageBox(_T("接听失败."));
}
Key->SetWindowText(_T("挂断")); /* 提示下一次操作动作 */
}
else
{
ret = m_GPRS->GPRS_HangUpTheCall(); /* 挂机 */
if (ret == FALSE)
{
MessageBox(_T("挂断失败.")); /* 挂机失败 */
}
Key->SetWindowText(_T("接听")); /* 按键恢复原来的状态 */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -