📄 testdlg.cpp
字号:
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "SerialControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_bauapar = 1;
m_compar = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_DATA_RECIEVE, m_datarecieve);
DDX_Control(pDX, IDC_AD_START, m_adstart);
DDX_Control(pDX, IDC_SETSYS, m_setsys);
DDX_CBIndex(pDX, IDC_BAUD, m_bauapar);
DDX_CBIndex(pDX, IDC_COM, m_compar);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_BAUD, OnSelchangeBaud)
ON_CBN_SELCHANGE(IDC_COM, OnSelchangeCom)
ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
ON_BN_CLICKED(IDC_CLOSECOM, OnClosecom)
ON_BN_CLICKED(IDC_SETSYS, OnSetsys)
ON_BN_CLICKED(IDC_AD_START, OnAdStart)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_DATA_RECIEVE,OnDataRecieve)
ON_MESSAGE(COM_RECVDATA,OnTranData)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
countdata=0; //数据接受个数
rxstaute=false;
for(int i=0;i<4100;i++) //初始化数据区为'0'
{
largebuff[i]=0;
}
savefile.Open("ADdata.xls", CFile::modeCreate|CFile::modeWrite);
SetDlgItemText(IDC_PINLV,"16");
SetDlgItemText(IDC_DATA,"4");
m_setsys.EnableWindow(false);
m_adstart.EnableWindow(false);
m_datarecieve.EnableWindow(false);
// GetDlgItem(IDC_DATA_RECIEVE)->GetWindowRect(&rect);
// ScreenToClient(rect);
// oripoint.x=rect.left+100;
// oripoint.y=rect.bottom+100;
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDlg::OnSelchangeBaud() //设置波特率
{
// TODO: Add your control notification handler code here
CString str;
GetDlgItemText(IDC_BAUD,str);
if("4800"==str)
{combaud=4800;}
if("9600"==str)
{combaud=9600;}
if("38400"==str)
{combaud=38400;}
if("115200"==str)
{combaud=115200;}
}
void CTestDlg::OnSelchangeCom()
{
// TODO: Add your control notification handler code here
CString str;
GetDlgItemText(IDC_COM,str);
if(str=="COM1")
{comnum="COM1";}
if(str=="COM2")
{comnum="COM2";}
}
void CTestDlg::OnOpencom()
{
// TODO: Add your control notification handler code here
if (true == opencom()) //打开串口通讯
{
m_setsys.EnableWindow(true);
m_adstart.EnableWindow(true);
m_datarecieve.EnableWindow(true);
}
DWORD threadID; //启动串口监视线程
hcomcontrol = ::CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0,
(LPTHREAD_START_ROUTINE)SerialPortThread, AfxGetMainWnd()->m_hWnd, 0, &threadID);
if (hcomcontrol == NULL)
{
::AfxMessageBox("串口数据接收线程创建失败");
::PostQuitMessage(0);
}
}
void CTestDlg::OnClosecom()
{
// TODO: Add your control notification handler code here
if(hcom!=NULL)
{
CloseHandle(hcom);
AfxMessageBox("已经关闭串口");
}
m_setsys.EnableWindow(false);
m_adstart.EnableWindow(false);
m_datarecieve.EnableWindow(false);
}
void CTestDlg::OnSetsys() //系统设定
{
// TODO: Add your control notification handler code here
//PurgeComm(hcom,PURGE_TXCLEAR);
OVERLAPPED wol;
wol.Internal=0;
wol.InternalHigh=0;
wol.Offset=0;
wol.OffsetHigh=0;
wol.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
char *ptr,point[5];
CString str;
ptr=point;
*ptr++=char(255); //协议头
*ptr++=char(1);
GetDlgItemText(IDC_PINLV,str);//微镜频率设置
pinlv = atof(str);
*ptr++ = (char)pinlv;
GetDlgItemText(IDC_DATA,str);//一次采集数据设置
rxdatenum=atof(str);
*ptr++=(char)rxdatenum;
*ptr=(char)85;
WriteFile(hcom,point,5,NULL,&wol);
/*char point;
point=(char)255;
WriteFile(hcom,&point,1,NULL,&wol);*/
AfxMessageBox("微镜开始转动,系统参数设定成功!");
}
void CTestDlg::OnAdStart()
{
// TODO: Add your control notification handler code here
//PurgeComm(hcom,PURGE_TXCLEAR);
OVERLAPPED wol;
wol.Internal=0;
wol.InternalHigh=0;
wol.Offset=0;
wol.OffsetHigh=0;
wol.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
char *ptr,point[5];
ptr=point;
*ptr++=(char)255;
*ptr++=(char)2;
*ptr++ = (char)0;
*ptr++=(char)0;
*ptr=(char)85;
WriteFile(hcom,point,5,NULL,&wol);
SetTimer(1,1000,NULL); //延迟1秒钟,等待数据采集结束
}
void CTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
KillTimer(1);
AfxMessageBox("数据采集完毕,并保存在实验板内存中!");
}
void CTestDlg::OnDataRecieve()
{
// TODO: Add your message handler code here and/or call default
SetDlgItemText(IDC_EDITDATA,"开始传输数据,并保存到ADdata文件中,请等待......");
UpdateData(false);
//PurgeComm(hcom,PURGE_TXCLEAR|PURGE_RXCLEAR);
OVERLAPPED wol;
wol.Internal=0;
wol.InternalHigh=0;
wol.Offset=0;
wol.OffsetHigh=0;
wol.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
char *ptr,point[5];
ptr=point;
*ptr++=(char)255;
*ptr++=(char)3;
*ptr++ = (char)0;
*ptr++=(char)0;
*ptr=(char)85;
WriteFile(hcom,point,5,NULL,&wol);
}
//serailportthread()线程路由消息的相应函数
void CTestDlg::OnTranData(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your message handler code here and/or call default
int getdata;
getdata=(int)wParam; //得到串口传过来的数据
if(rxstaute==false) //接受高字节数据;
{
highbyte=getdata*256;
rxstaute=true;
}
else //接受低字节数据
{
rxstaute=false;
CArchive ar(&savefile, CArchive::store); //接受到的数据保存到文中
getdata=highbyte+getdata;
if (countdata==0)
{
int time=(1/(pinlv*4096))*1000000000;
int temp2,temp3=time/1000000;
ar<<char(temp3+48);
temp3=(temp2=(time-temp3*1000000))/100000;
ar<<char(temp3+48);
temp3=(temp2=(temp2-temp3*100000))/10000;
ar<<char(temp3+48);
temp3=(temp2=(temp2-temp3*10000))/1000;
ar<<char(temp3+48);
temp3=(temp2=(temp2-temp3*1000))/100;
ar<<char(temp3+48);
temp3=(temp2=(temp2-temp3*100))/10;
ar<<char(temp3+48);
temp3=(temp2=(temp2-temp3*10))/1;
ar<<char(temp3+48);
ar<<'n';
ar<<'s';
ar<<'\r';
}
int temp1,temp=getdata/10000;
ar<<char(temp+48);
temp=(temp1=(getdata-temp*10000))/1000;
ar<<char(temp+48);
temp=(temp1=(temp1-temp*1000))/100;
ar<<char(temp+48);
temp=(temp1=(temp1-temp*100))/10;
ar<<char(temp+48);
temp=(temp1=(temp1-temp*10))/1;
ar<<char(temp+48);
ar<<'\r';
largebuff[countdata]=getdata;
countdata++;
if (countdata==4095)
{
countdata=0;
SetDlgItemText(IDC_EDITDATA,"数据存到ADdata文件中......");
UpdateData(false);
// file.Write(largebuff,4095);
// file.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -