www.pudn.com > MsgEmulatorSerialTest.rar > SerialTestDlg.cpp
// SerialTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialTest.h"
#include "SerialTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//char HexChar(char c);
//int Str2Hex(CString str,CByteArray& data);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialTestDlg dialog
CSerialTestDlg::CSerialTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialTestDlg)
m_strOut = _T("");
m_nTimeOut = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialTestDlg)
DDX_Control(pDX, IDC_BAUDCOMB, m_cbBaud);
DDX_Control(pDX, IDC_PORTCOMB, m_cbPort);
DDX_Text(pDX, IDC_OUTEDIT, m_strOut);
DDX_Text(pDX, IDC_TMREDIT, m_nTimeOut);
DDV_MinMaxInt(pDX, m_nTimeOut, 1, 15);
DDX_Control(pDX, IDC_MSCOMM1, m_comm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialTestDlg, CDialog)
//{{AFX_MSG_MAP(CSerialTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OK, OnClkOk)
ON_BN_CLICKED(IDC_CANCEL, OnClkCancel)
ON_BN_CLICKED(IDC_ADDBTN, OnAddbtn)
ON_LBN_DBLCLK(IDC_CANDMSG, OnDblclkCandmsg)
ON_BN_CLICKED(IDC_RMVBTN, OnRmvbtn)
ON_LBN_DBLCLK(IDC_SENDMSG, OnDblclkSendmsg)
ON_CBN_SELENDOK(IDC_PORTCOMB, OnSelendokPortcomb)
ON_CBN_SELENDOK(IDC_BAUDCOMB, OnSelendokBaudcomb)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialTestDlg message handlers
BOOL CSerialTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
int i;
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
//init spin
CSpinButtonCtrl * pSpin=(CSpinButtonCtrl *)GetDlgItem(IDC_TMRSPIN);
pSpin->SetBuddy(GetDlgItem(IDC_TMREDIT));
pSpin->SetRange(1,15);
m_nTimeOut=3;
//listbox
pLBCand=(CListBox *)GetDlgItem(IDC_CANDMSG);
/* pLBCand->InsertString(-1,"msg1");
pLBCand->InsertString(-1,"msg2");
pLBCand->InsertString(-1,"msg3");
pLBCand->InsertString(-1,"msg4"); */
pLBCand->Dir(0x0000,"*.data");
pLBSend=(CListBox *)GetDlgItem(IDC_SENDMSG);
//outedit
m_strOut+="this is a sample string for serial test\r\n";
CString strtmp="aaabbbcccdddddddddd";
// for(i=0;iGetCurSel())!=LB_ERR)
pLBSend->GetText(n,str);
else
{
AfxMessageBox("select the pending send msg ,pls\r\n");
return;
}
if( !file.Open((LPCTSTR)str,CFile::modeRead|CFile::typeBinary,&e) )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
return;
}
n=file.Read(fbuf,400);
// fbuf[n]=0;
file.Close();
OutBuf.SetSize(n);
strTmpOut.Empty();
for(i=0;i>4;
b=b&0x0F;
a=(a>10)?('a'+a-10):(0x30+a);
b=(b>10)?('a'+b-10):(0x30+b);
str.Format("%c%c ",a,b);
strTmpOut+=str;
OutBuf[i]=fbuf[i];
}
// m_strOut.Empty();
m_strOut+="\r\n>>>>>sending...\r\n";
m_strOut+=strTmpOut;
m_strOut+="\r\n";
UpdateData(FALSE);
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
varOutput = OutBuf;
// m_comm.SetOutput(varOutput);//同步发送,为调测方便暂时注释掉
SetTimer(1,m_nTimeOut*1000,NULL);
}
void CSerialTestDlg::OnClkCancel()
{
// TODO: Add your control notification handler code here
if(m_comm.GetPortOpen()) //如果串口是打开的,则行关闭串口
m_comm.SetPortOpen(FALSE);
CDialog::OnCancel();
}
void CSerialTestDlg::OnAddbtn()
{
// TODO: Add your control notification handler code here
int n;
char tmpbuf[50];
/* if((n=pLBCand->GetCurSel())!=LB_ERR)
{
sprintf(tmpbuf,"message %d selected",n);
pLBSend->InsertString(-1,tmpbuf);
}
else
AfxMessageBox("No msg selected"); */
if((n=pLBCand->GetCurSel())!=LB_ERR)
{
pLBCand->GetText(n,tmpbuf);
pLBSend->InsertString(-1,tmpbuf);
pLBSend->SetCurSel(0);
}
else
AfxMessageBox("No msg selected");
// AfxMessageBox(tmpbuf);
}
void CSerialTestDlg::OnDblclkCandmsg()
{
// TODO: Add your control notification handler code here
int n;
char tmpbuf[50];
if((n=pLBCand->GetCurSel())!=LB_ERR)
{
pLBCand->GetText(n,tmpbuf);
pLBSend->InsertString(-1,tmpbuf);
pLBSend->SetCurSel(0);
}
else
AfxMessageBox("No msg selected");
}
void CSerialTestDlg::OnRmvbtn()
{
// TODO: Add your control notification handler code here
int n;
if((n=pLBSend->GetCurSel())!=LB_ERR)
pLBSend->DeleteString(n);
else
AfxMessageBox("No msg selected");
}
void CSerialTestDlg::OnDblclkSendmsg()
{
// TODO: Add your control notification handler code here
int n;
if((n=pLBSend->GetCurSel())!=LB_ERR)
pLBSend->DeleteString(n);
else
AfxMessageBox("No msg selected");
}
/*
char HexChar(char c)//检测一个字符是不是十六进制字符,若是返回相应的值,否则返回0x10;
{ if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else return 0x10;
}
int Str2Hex(CString str,CByteArray &data)
{//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,返回转换后的字节数组长度,同时字节数组长度自动设置。
int t,t1;
int rlen=0,len=str.GetLength();
data.SetSize(len/2);
for(int i=0;i=len)break;
l=str[i];
t=HexChar(h);
t1=HexChar(l);
if((t==16)||(t1==16))
break;
else t=t*16+t1;
i++;
data[rlen]=(char)t;
rlen++;
}
data.SetSize(rlen);
return rlen;
}
*/
BEGIN_EVENTSINK_MAP(CSerialTestDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CSerialTestDlg)
ON_EVENT(CSerialTestDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSerialTestDlg::OnComm()
{
// TODO: Add your control notification handler code here
// if(stop)return;
VARIANT m_input1;
COleSafeArray m_input2;
long length,i;
BYTE data[1024];
#ifdef _DEBUG
BYTE data1[10]={0x70,0x00,0x80,0x00,0x33,0x12,0x11};
#endif
CString str;
KillTimer(1);
if(m_comm.GetCommEvent()==2)//接收缓冲区内有字符
{
m_input1=m_comm.GetInput();//读取缓冲区内的数据
m_input2=m_input1;//将VARIANT型变量转换为ColeSafeArray型变量
length=m_input2.GetOneDimSize();//确定数据长度
for(i=0;i>4;
b=b&0x0F;
a=(a>10)?('a'+a-10):(0x30+a);
b=(b>10)?('a'+b-10):(0x30+b);
str.Format("%c%c ",a,b);
strTmpOut+=str;
}
#else
for(i=0;i>4;
b=b&0x0F;
a=(a>10)?('a'+a-10):(0x30+a);
b=(b>10)?('a'+b-10):(0x30+b);
str.Format("%c%c ",a,b);
strTmpOut+=str;
}
#endif
m_strOut+="<<<<<>4;
b=b&0x0F;
a=(a>10)?('a'+a-10):(0x30+a);
b=(b>10)?('a'+b-10):(0x30+b);
str.Format("%c%c ",a,b);
strTmpOut+=str;
}
m_strOut+="<<<<<