📄 datatran.cpp
字号:
// DataTRAN.cpp : implementation file
//
#include "stdafx.h"
#include "FFT_PROJECT.h"
#include "DataTRAN.h"
#include "FFT_PROJECTView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDataTRAN dialog
CDataTRAN::CDataTRAN(CWnd* pParent /*=NULL*/)
: CDialog(CDataTRAN::IDD, pParent)
{
//{{AFX_DATA_INIT(CDataTRAN)
//}}AFX_DATA_INIT
myView=(CScrollView *) pParent;
pDoc=(CFFT_PROJECTDoc*)myView->GetDocument();
}
void CDataTRAN::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDataTRAN)
DDX_Control(pDX, IDC_MSCOMM2, m_mscomm2);
DDX_Control(pDX, IDC_MSCOMM1, m_mscomm1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDataTRAN, CDialog)
//{{AFX_MSG_MAP(CDataTRAN)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDataTRAN message handlers
void CDataTRAN::OnOK()
{
// TODO: Add extra validation here
//AfxMessageBox("串口1打开");
// m_mscomm1.SetPortOpen(false);
//CDialog::OnOK();
/* CString filename,str;
str=pDoc->receivedata;
int i=0;
char c;
CFileDialog dlg(false);
if(dlg.DoModal())
filename=dlg.GetPathName();
CFile file(filename,CFile::modeCreate|CFile::modeWrite);
//file.Write(pDoc->receivedata,sizeof(pDoc->receivedata));
while((c=str.GetAt(i++)))
file.Write(&c,1);
file.Close();
AfxMessageBox("数据已保存");*/
CString filename;
CStdioFile *file;
CFileDialog dlg(false);
if(dlg.DoModal())
file=new CStdioFile(dlg.GetPathName(),CFile::modeCreate|CFile::modeWrite);
file->WriteString(pDoc->receivedata);
file->Close();
file=NULL;
this->ShowWindow(SW_HIDE);
// return;
}
void CDataTRAN::OnCancel()
{ this->ShowWindow(SW_HIDE);
//GetDlgItem(IDC_STATIC)->SetWindowText("数据正在处理中,要取消吗。。。。");
// TODO: Add extra cleanup here
// m_mscomm1.SetPortOpen(false);
//DestroyWindow();
//m_mscomm2.SetCommPort(3);
//CDialog::OnCancel();
}
BEGIN_EVENTSINK_MAP(CDataTRAN, CDialog)
//{{AFX_EVENTSINK_MAP(CDataTRAN)
ON_EVENT(CDataTRAN, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
ON_EVENT(CDataTRAN, IDC_MSCOMM2, 1 /* OnComm */, OnOnCommMscomm2, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CDataTRAN::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
if(!m_mscomm1.GetPortOpen()||!pDoc->Com1Open)//判断是否选择COM1进行通信;
return;
//{if(!pDoc->Com1Open)
//return;}
VARIANT variant_inp; //从缓冲取出数据的数组;
COleSafeArray safearray_inp;//操作接受数据数组;
LONG len,k;
CString str;
BOOL Finshed=false;//标志接收完成;
BYTE rxdata[2048];
BYTE bt;
if(!m_mscomm1.GetPortOpen())
return;
if(m_mscomm1.GetCommEvent()==2)
{
// MessageBox("接收");
variant_inp=m_mscomm1.GetInput();//取进数据;
safearray_inp=variant_inp; //存进操作字符串;
len=safearray_inp.GetOneDimSize(); //获得有用数据的长度;
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k); //存进字节数组中方便操作;
for(k=0;k<len;k++)
{ bt=*(char*)(rxdata+k); //取出字节;
if(pDoc->endChar) //结束的后不接收任何数据
{bt='e';continue;
}
str.Format("%c",bt);
pDoc->receivedata+=str; //存入接受字符串中为转化为所需的数据坐准备;
if(bt=='e'||bt=='E')//设置各个指示;
{ pDoc->endChar=true;
Finshed=true;
//pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
break;}
/*if(bt!='s'||bt!='S')
if(bt!=' ')
pDoc->strtemp+=bt;
else
{ pDoc->origindata[pDoc->nCount++]=atof(pDoc->strtemp);
AfxMessageBox(pDoc->strtemp);
pDoc->strtemp=_T("");}*/
/*if(bt=='e'||bt=='E')
{ AfxMessageBox("已经关闭");
m_mscomm1.SetRThreshold(0);
m_mscomm1.SetPortOpen(false);
// pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
}*/
//MessageBox("显示");
}
}
if(bt=='e'||bt=='E')//关闭串口,防止下次打开时弹出串口打开对话框警告;
{ //AfxMessageBox("已经关闭");
m_mscomm1.SetRThreshold(0);
m_mscomm1.SetPortOpen(false);
this->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC)->SetWindowText("数据接收完毕,是否要保存数据?。");
// pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
}
//MessageBox("显示");
if(Finshed)//如果接受结束的就进行数据处理;
{char c;
CString strtemp;//临时字符串,为转化成浮点数坐准备;
len=0;
for(int i=0;;i++)
{c=pDoc->receivedata.GetAt(i);
if(c=='e'||c=='E')//结束字符跳出;
break;
if(c=='s'||c=='S')//开始字符忽略,是传输协议中加的标志字符;
continue;
if(c!=' ')//如果没把一个数据接受完,继续加入字符串;
strtemp+=c;
else //一个浮点数的字符串类型全部取到,进行转化并把临时字符串清空为下个数据坐准备;
{ pDoc->origindata[len++]=atof(strtemp);
//AfxMessageBox(pDoc->strtemp);
strtemp=_T("");
}
}
//AfxMessageBox(strtemp);
pDoc->origindata[len]=atof(strtemp);//接受最后一个数据
//AfxMessageBox(strtemp);
pDoc->nCount=++len;
pDoc->datareceived=true;
CMainFrame *pframe;
pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar *pstatus=&pframe->m_wndStatusBar;
int nIndex=pstatus->CommandToIndex(ID_INDICATOR_DATAIN );
pstatus->SetPaneInfo(nIndex,ID_INDICATOR_DATAIN ,SBPS_NORMAL,60);
pstatus->SetPaneText(nIndex,"数据接收好");
//MessageBox(pDoc->receivedata);
}
UpdateData(false);
Invalidate(true);
}
void CDataTRAN::OnOnCommMscomm2()
{
// TODO: Add your control notification handler code here
//AfxMessageBox("nishi1");
if(!m_mscomm2.GetPortOpen()||!pDoc->Com2Open)//判断是否选择COM2
return;
//AfxMessageBox("nishi1");
VARIANT variant_inp; //从缓冲取出数据的数组;
COleSafeArray safearray_inp;//操作接受数据数组;
LONG len,k;
CString str;
BOOL Finshed=false; //标志接受完成;
BYTE rxdata[2048];
BYTE bt;
if(!m_mscomm2.GetPortOpen())
return;
if(m_mscomm2.GetCommEvent()==2)
{
// MessageBox("接收");
variant_inp=m_mscomm2.GetInput();//取进数据;
safearray_inp=variant_inp; //存进操作字符串;
len=safearray_inp.GetOneDimSize(); //获得有用数据的长度;
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k); //存进字节数组中方便操作;
for(k=0;k<len;k++)
{ bt=*(char*)(rxdata+k); //取出字节;
if(pDoc->endChar) //结束的后不接收任何数据
{bt='e';continue;
}
str.Format("%c",bt);
pDoc->receivedata+=str; //存入接受字符串中为转化为所需的数据坐准备;
if(bt=='e'||bt=='E')//设置各个指示;
{ pDoc->endChar=true;
Finshed=true;
//pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
break;}
/*if(bt!='s'||bt!='S')
if(bt!=' ')
pDoc->strtemp+=bt;
else
{ pDoc->origindata[pDoc->nCount++]=atof(pDoc->strtemp);
AfxMessageBox(pDoc->strtemp);
pDoc->strtemp=_T("");}*/
/*if(bt=='e'||bt=='E')
{ AfxMessageBox("已经关闭");
m_mscomm1.SetRThreshold(0);
m_mscomm1.SetPortOpen(false);
// pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
}*/
//MessageBox("显示");
}
}
if(bt=='e'||bt=='E')//关闭串口,防止下次打开时弹出串口打开对话框警告;
{ //AfxMessageBox("已经关闭");
m_mscomm2.SetRThreshold(0);
m_mscomm2.SetPortOpen(false);
this->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STATIC)->SetWindowText("数据接收完毕,是否要保存数据?。");
// pDoc->origindata[pDoc->nCount]=atof(pDoc->strtemp);
}
//MessageBox("显示");
if(Finshed)//如果接受结束的就进行数据处理;
{char c;
CString strtemp;//临时字符串,为转化成浮点数坐准备;
len=0;
for(int i=0;;i++)
{c=pDoc->receivedata.GetAt(i);
if(c=='e'||c=='E')//结束字符跳出;
break;
if(c=='s'||c=='S')//开始字符忽略,是传输协议中加的标志字符;
continue;
if(c!=' ')//如果没把一个数据接受完,继续加入字符串;
strtemp+=c;
else //一个浮点数的字符串类型全部取到,进行转化并把临时字符串清空为下个数据坐准备;
{ pDoc->origindata[len++]=atof(strtemp);
//AfxMessageBox(pDoc->strtemp);
strtemp=_T("");
}
}
//AfxMessageBox(strtemp);
pDoc->origindata[len]=atof(strtemp);//接受最后一个数据
//AfxMessageBox(strtemp);
pDoc->nCount=++len;
pDoc->datareceived=true;
//MessageBox(pDoc->receivedata);
CMainFrame *pframe;
pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar *pstatus=&pframe->m_wndStatusBar;
int nIndex=pstatus->CommandToIndex(ID_INDICATOR_DATAIN );
pstatus->SetPaneInfo(nIndex,ID_INDICATOR_DATAIN ,SBPS_NORMAL,60);
pstatus->SetPaneText(nIndex,"数据接收好");
}
UpdateData(false);
Invalidate(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -