SendDataDlg.cpp
资源名称:vc_demo.rar [点击查看]
上传用户:zcy791212
上传日期:2013-03-06
资源大小:196k
文件大小:8k
源码类别:
Modem编程
开发平台:
Visual C++
- //
- // SendDataDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "vc_demo.h"
- #include "SendDataDlg.h"
- #include "gprsdll.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- int StrParseToHex(CString Str, u32t * pulL)
- //输入:Str ------必须是一个8个字节长的表示16进制数的字符串(前头用0补足位置)
- //输出:pulL
- //返回0表明转换失败
- //否则1表明转换成功
- {
- unsigned long ulL;
- unsigned char uszLong[4];
- //注意,下面这个宏必须保证hex是一个0~9||a~f||A~F的值。
- #define VALUEOFHEX(hex)
- (
- '0'<=hex&&hex<='9'?hex-'0':
- (
- 'a'<=hex&&hex<='f'?hex-'a'+10:hex-'A'+10
- )
- )
- #define IFHEXCHAR(hex)
- (
- '0'<=hex&&hex<='9'
- ||
- 'a'<=hex&&hex<='f'
- ||
- 'A'<=hex&&hex<='F'
- )
- if (!(IFHEXCHAR(Str[0])&&IFHEXCHAR(Str[1])&&IFHEXCHAR(Str[2])&&IFHEXCHAR(Str[3])
- &&IFHEXCHAR(Str[4])&&IFHEXCHAR(Str[5])&&IFHEXCHAR(Str[6])&&IFHEXCHAR(Str[7])))
- {
- return 0;
- }
- uszLong[3]=VALUEOFHEX(Str[0])*16+VALUEOFHEX(Str[1]);
- uszLong[2]=VALUEOFHEX(Str[2])*16+VALUEOFHEX(Str[3]);
- uszLong[1]=VALUEOFHEX(Str[4])*16+VALUEOFHEX(Str[5]);
- uszLong[0]=VALUEOFHEX(Str[6])*16+VALUEOFHEX(Str[7]);
- memcpy(pulL,uszLong,4);
- return (1);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CSendDataDlg dialog
- CSendDataDlg::CSendDataDlg(CWnd* pParent,CTreeCtrl* ptc)
- : CDialog(CSendDataDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CSendDataDlg)
- m_hex = FALSE;
- //}}AFX_DATA_INIT
- m_pTree=NULL;
- if (ptc)
- m_pTree=ptc;
- }
- void CSendDataDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CSendDataDlg)
- DDX_Control(pDX, IDC_CMUSERLIST, m_cmuserlist);
- DDX_Check(pDX, IDC_CKHEX, m_hex);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CSendDataDlg, CDialog)
- //{{AFX_MSG_MAP(CSendDataDlg)
- ON_BN_CLICKED(IDC_BQUIT, OnBquit)
- ON_WM_SHOWWINDOW()
- ON_BN_CLICKED(IDC_CKHEX, OnCkhex)
- ON_WM_SIZE()
- ON_BN_CLICKED(IDC_BSEND, OnBsend)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_CKAUTOSEND, OnCkautosend)
- ON_BN_CLICKED(IDC_BCR, OnBcr)
- ON_BN_CLICKED(IDC_BLF, OnBlf)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSendDataDlg message handlers
- void CSendDataDlg::AddUserId()
- {
- HTREEITEM hti;
- CString str;
- if ((m_pTree) && (m_pTree->GetFirstVisibleItem()))
- {
- hti=m_pTree->GetChildItem(m_pTree->GetFirstVisibleItem());
- while (hti)
- {
- str=m_pTree->GetItemText(hti);
- m_cmuserlist.AddString(str.GetBuffer(0));
- hti=m_pTree->GetNextItem(hti,TVGN_NEXT);
- }
- }
- m_cmuserlist.SetWindowText(m_userid);
- }
- void CSendDataDlg::SetUserId(char *userid)
- {
- m_userid=userid;
- }
- void CSendDataDlg::OnBquit()
- {
- this->OnCancel();
- }
- void CSendDataDlg::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- CDialog::OnShowWindow(bShow, nStatus);
- this->AddUserId();
- this->SendMessage(WM_SIZE);
- ((CEdit *)GetDlgItem(IDC_INTERVAL))->SetWindowText("200");
- }
- int CSendDataDlg::StrToHex(char *str,char *hex)
- {
- int i,slen;
- slen=strlen(str);
- for (i=0;i<slen;i++)
- {
- sprintf(hex+i*3,"%02X ",str[i]&0xFF);
- }
- return 0;
- }
- int CSendDataDlg::HexToStr(char *hex,char *str)
- {
- int i,hlen,iData,iFlag;
- char ch;
- iData=0;
- iFlag=0;
- hlen=strlen(hex);
- for (i=0;i<hlen;i++)
- {
- if (' '==hex[i])
- continue;
- ch=hex[i];
- if ((ch>='0') && (ch<='9'))
- {
- ch-=0x30;
- }
- else if ((ch>='a') && (ch<='f'))
- {
- ch-=0x57;
- }
- else if ((ch>='A') && (ch<='F'))
- {
- ch-=0x37;
- }
- else
- {
- *str=' ';
- return -1;
- }
- if (0==iFlag)
- {
- iData=ch;
- iFlag=1;
- }
- else
- {
- iData=(iData<<4) +ch;
- iFlag=0;
- *str++=iData;
- }
- }
- *str=' ';
- return 0;
- }
- void CSendDataDlg::OnCkhex()
- {
- char *hexbuf,*strbuf;
- int iLen;
- CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDSHOW);
- this->UpdateData(TRUE);
- hexbuf=new char[3096];
- strbuf=new char[1024];
- if (m_hex)
- {
- iLen=pEdit->GetWindowText(strbuf,1024);
- strbuf[iLen]=' ';
- if (0==this->StrToHex(strbuf,hexbuf))
- pEdit->SetWindowText(hexbuf);
- else
- {
- m_hex=FALSE;
- UpdateData(FALSE);
- }
- }
- else
- {
- iLen=pEdit->GetWindowText(hexbuf,3096);
- hexbuf[iLen]=' ';
- if (0==this->HexToStr(hexbuf,strbuf))
- pEdit->SetWindowText(strbuf);
- else
- {
- m_hex=TRUE;
- UpdateData(FALSE);
- }
- }
- delete hexbuf;
- delete strbuf;
- }
- void CSendDataDlg::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);
- if (!this->IsWindowVisible())
- return;
- CRect rt;
- CEdit *pEdit;
- CButton *pButton;
- pEdit=(CEdit *)GetDlgItem(IDC_EDSHOW);
- pEdit->GetWindowRect(&rt);
- pEdit->MoveWindow(11,36,cx-25,cy-77);
- pEdit=(CEdit *)GetDlgItem(IDC_EDSENDCOUNT);
- pEdit->GetWindowRect(&rt);
- pEdit->MoveWindow(cx-104,9,rt.Width(),rt.Height());
- CStatic *pStatic=(CStatic *)GetDlgItem(IDC_STATIC1);
- pStatic->GetWindowRect(&rt);
- pStatic->MoveWindow(cx-164,13,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_CKHEX);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(12,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_BCR);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(60,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_BLF);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(90,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_CKAUTOSEND);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(120,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_INTERVAL);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(180,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_BQUIT);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(cx-74,cy-rt.Height()-10,rt.Width(),rt.Height());
- pButton=(CButton *)GetDlgItem(IDC_BSEND);
- pButton->GetWindowRect(&rt);
- pButton->MoveWindow(cx-144,cy-rt.Height()-10,rt.Width(),rt.Height());
- }
- void CSendDataDlg::OnBsend()
- {
- char *strbuf,*hexbuf;
- CString useridstr;
- this->UpdateData(TRUE);
- hexbuf=new char[3096];
- strbuf=new char[1024];
- if (m_hex)
- {
- ((CEdit *)GetDlgItem(IDC_EDSHOW))->GetWindowText(hexbuf,3096);
- this->HexToStr(hexbuf,strbuf);
- }
- else
- ((CEdit *)GetDlgItem(IDC_EDSHOW))->GetWindowText(strbuf,1024);
- this->m_cmuserlist.GetWindowText(useridstr);
- u32t modemid;
- if (0 == StrParseToHex(useridstr,&modemid))
- {
- MessageBox("注意!Modem的id号错误,将不进行发送!","错误!");
- return;
- }
- //todo_edw :should add
- DSSendData(modemid,
- strlen(strbuf),
- (unsigned char *)strbuf);
- CString text;
- ((CEdit *)GetDlgItem(IDC_EDSENDCOUNT))->GetWindowText(text);
- int count=atoi(text.GetBuffer(0));
- char pbuf[32];
- sprintf(pbuf,"%d",count+strlen(strbuf));
- ((CEdit *)GetDlgItem(IDC_EDSENDCOUNT))->SetWindowText(pbuf);
- delete strbuf;
- delete hexbuf;
- }
- void CSendDataDlg::OnTimer(UINT nIDEvent)
- {
- if (1==nIDEvent)
- this->OnBsend();
- CDialog::OnTimer(nIDEvent);
- }
- void CSendDataDlg::OnCkautosend()
- {
- CButton *pButton;
- CEdit *pEdit;
- char buf[32];
- int interval;
- pEdit=(CEdit *)GetDlgItem(IDC_INTERVAL);
- pEdit->GetWindowText(buf,32);
- interval=atoi(buf);
- if (interval<=0)
- return;
- pButton=(CButton *)GetDlgItem(IDC_CKAUTOSEND);
- if (1==pButton->GetCheck())
- {
- this->SetTimer(1,interval,NULL);
- }
- else
- {
- this->KillTimer(1);
- }
- }
- void CSendDataDlg::OnBcr()
- {
- CString text;
- CEdit *pEdit;
- pEdit=(CEdit *)GetDlgItem(IDC_EDSHOW);
- pEdit->GetWindowText(text);
- UpdateData(TRUE);
- if (m_hex)
- {
- pEdit->SetWindowText(text+"0D ");
- }
- else
- {
- pEdit->SetWindowText(text+"r");
- }
- }
- void CSendDataDlg::OnBlf()
- {
- CString text;
- CEdit *pEdit;
- pEdit=(CEdit *)GetDlgItem(IDC_EDSHOW);
- pEdit->GetWindowText(text);
- UpdateData(TRUE);
- if (m_hex)
- {
- pEdit->SetWindowText(text+"0A ");
- }
- else
- {
- pEdit->SetWindowText(text+"n");
- }
- }