class A
{…,int x;}
class B
{……}
源文件B.cpp
class A
{…,y=x;}
class B
{…,x=3;}
现在要从B中传递一个值给A的x。A,B必须定义在一个文件中的情况下,怎样才能实现这种传递呢?
6 个解决方案
#1
你要传递也是要向A的一个对象传递
#2
你指的是友元的方式?
#3
直接调用A类的成员函数
#4
跟是否在同一个文件没有关系吧。
也许是指的友元
也许是指的友元
#5
这是一个串口软件的一部分
// GyroHandlerDlg.h : header file
//
#pragma once
#include "ComEngineCE.h"
#include "DatagramFilter.h"
#include "GyroParaQueue.h"
#include "ComConfigDlg.h"
#include "ComPortEnum.h"
#include "ResolveDlg.h"
#include "TotalStationDlg.h"
#include "afxwin.h"
#define implements
//////////////////////////////////////////////////////////////////////////////////////////////////////
// class CLogFile is used to implement information log
// you do not need to call it directly, you just need to call macro
//
// ADD_LOG(format_string,...) // ADD information to log file, and at the same time output to the screen
// ADD_LOG1(format_string,...) // Add information to log file, but show nothing in the screen
// ADD_LOG2(format_string,...) // show information in the screen, but do not store in the screen
////////////////////////////////////////////////////////////////////////////////////////////////
#define ADD_LOG CLogFile::GetInstance()->AddLog // add log informaiton both to log file and screen
#define ADD_LOG1 CLogFile::GetInstance()->AddLogOnly // only put information to log file
#define ADD_LOG2 printf
// only put information to screen
class CLogFile
{
public:
CLogFile::CLogFile();
CLogFile::~CLogFile();
public:
void CLogFile::operator <<(const char* str);
void CLogFile::AddLog(const char* format,...)
{
if(!format)
{
return;
}
CStringA s;
va_list argList;
va_start(argList, format);
s.FormatV(format, argList);
va_end(argList);
AddLogInner(LPCSTR(s));
}
void CLogFile::AddLogOnly(const char* format,...)
{
//printf(" --- before initiate call stack object of AddLogOnly() --\n");
//CALL_STACK("void CLogFile::AddLogOnly(const char* format,...)");
if(!format)
{
return;
}
//printf("----- xxxx.1 ----\n");
CStringA s;
va_list argList;
va_start(argList, format);
//printf("---- xxxx.2 ----\n");
//fflush(NULL);
s.FormatV(format, argList);
va_end(argList);
//printf("---- xxxx.3 ----\n");
AddLogOnlyInner(LPCSTR(s));
}
static CLogFile* CLogFile::GetInstance();
protected:
void CLogFile::AddLogInner(const char* str);
void CLogFile::AddLogOnlyInner(const char* str);
protected:
/*-------------------------------------------
class members
-------------------------------------------*/
FILE* m_fpLog;
protected:
static CLogFile m_Log;
public:
int savenametest;//这是要接受值得变量
};
// CGyroHandlerDlg dialog
class CGyroHandlerDlg :
public CDialog,
implements public CComReadWriteHandler,
implements public CDatagramFilterHandler,
implements public CComConfigDlgHandler
{
// Construction
public:
CGyroHandlerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_GyroHandler_DIALOG };
public:
virtual void OnConfig(const TCHAR* pPort,const int baudrate, const int databits, const int stopbits, const int parity_mode )
{
this->m_port = pPort;
this->m_baudrate = baudrate;
this->m_databits = databits;
this->m_stopbits = stopbits;
this->m_parity_mode = parity_mode;
GetDlgItem(IDC_START_STOP)->EnableWindow(TRUE); //enable start Window now
}
public:
virtual void OnValidMessage(char* str,int size) // API of interface CDatagramFilterHandler
{
byte* bStr = (byte*) str;
byte b = 0;
CStringA errMsg;
CGyroParameter GyroPara;
TRACE(L"\r\n RECEIVED - head removed [");
for ( int i = 0; i < size; i ++)
{
TRACE(L" %x ",bStr[i]);
}
TRACE(L" ] \r\n");
//////verifying data validation
for ( int i = 0; i < size -1; i ++)
{
b = b ^ bStr[i];
}
if (b != bStr[size - 1])
{
errMsg = "Error! XOR verifying failure in CDatagramFilterHandler::OnValidMessage(...)\r\n";
/*ADD_LOG(LPCSTR(errMsg));*/
}
GyroPara.m_PlusNum = ( ( ( bStr[0] << 1) & 0x000000FF ) >> 1) |
( ( bStr[1] << 1 & 0x000000FF ) >> 1 << 7) |
( ( bStr[2] << 1 & 0x000000FF ) >> 1 << 14) |
( ( bStr[3] << 1 & 0x000000FF ) >> 1 << 21) |
( ( bStr[4] << 1 & 0x000000FF ) >> 1 << 28);
CStringA cs;
GyroPara.ToStringA(cs);
cs += "\n";
ADD_LOG(LPCSTR(cs)); // now gyro parameter is written to log file
///////////////////////////////////////////////////////////////////////////////////
CStringW csWindow;
m_queue.Append(GyroPara);
m_queue.ToStringW(csWindow);
/////////////////////////////////now update UI///////////////////////////////////
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_GYRO_OUT_WND);
if(pStatic)
{
pStatic->SetWindowText(LPCTSTR(csWindow));
}
if (ACQnumbertest==m_iACQnumber)
{
m_com.Close();
}
else
{
ACQnumbertest++;
}
}
// data read already, please delete buf in handler
virtual void OnDataReceived(BYTE* buf, int bufLen) // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Serier port data comes\r\n");
if(buf)
{
for(int i = 0; i < bufLen; i ++)
{
m_pMsgFilter->AddChar(buf[i]); // it will trig CDatagramFilterHandler::OnValidMessage(...)
}
}
else
{
TRACE(L"DEBUG:Error, NULL string received????\r\n");
}
}
// data already sent
virtual void OnDataSent() // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Finish Writing ...\r\n");
}
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
CComEngineCE m_com;
CDatagramFilter* m_pMsgFilter;
CGyroParaQueue m_queue;
// Generated message map functions
// virtual BOOL OnInitDialog();
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
afx_msg void OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/);
#endif
DECLARE_MESSAGE_MAP()
public:
CButton m_StartStopButton;
afx_msg void OnDestroy();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedStartStop();
afx_msg void OnBnClickedConfigComButton();
protected:
CComConfigDlg m_config_dlg;
CString m_port;
int m_baudrate;
int m_databits;
int m_stopbits;
int m_parity_mode;
public:
virtual BOOL OnInitDialog();
CComboBox m_comboSaveName;
int m_iSaveName;
int m_iACQnumber;
int ACQnumbertest;
afx_msg void OnBnClickedStationBtn();
CButton m_WestButton;
afx_msg void OnBnClickedWestBtn();
};
// GyroHandlerDlg.h : header file
//
#pragma once
#include "ComEngineCE.h"
#include "DatagramFilter.h"
#include "GyroParaQueue.h"
#include "ComConfigDlg.h"
#include "ComPortEnum.h"
#include "ResolveDlg.h"
#include "TotalStationDlg.h"
#include "afxwin.h"
#define implements
//////////////////////////////////////////////////////////////////////////////////////////////////////
// class CLogFile is used to implement information log
// you do not need to call it directly, you just need to call macro
//
// ADD_LOG(format_string,...) // ADD information to log file, and at the same time output to the screen
// ADD_LOG1(format_string,...) // Add information to log file, but show nothing in the screen
// ADD_LOG2(format_string,...) // show information in the screen, but do not store in the screen
////////////////////////////////////////////////////////////////////////////////////////////////
#define ADD_LOG CLogFile::GetInstance()->AddLog // add log informaiton both to log file and screen
#define ADD_LOG1 CLogFile::GetInstance()->AddLogOnly // only put information to log file
#define ADD_LOG2 printf
// only put information to screen
class CLogFile
{
public:
CLogFile::CLogFile();
CLogFile::~CLogFile();
public:
void CLogFile::operator <<(const char* str);
void CLogFile::AddLog(const char* format,...)
{
if(!format)
{
return;
}
CStringA s;
va_list argList;
va_start(argList, format);
s.FormatV(format, argList);
va_end(argList);
AddLogInner(LPCSTR(s));
}
void CLogFile::AddLogOnly(const char* format,...)
{
//printf(" --- before initiate call stack object of AddLogOnly() --\n");
//CALL_STACK("void CLogFile::AddLogOnly(const char* format,...)");
if(!format)
{
return;
}
//printf("----- xxxx.1 ----\n");
CStringA s;
va_list argList;
va_start(argList, format);
//printf("---- xxxx.2 ----\n");
//fflush(NULL);
s.FormatV(format, argList);
va_end(argList);
//printf("---- xxxx.3 ----\n");
AddLogOnlyInner(LPCSTR(s));
}
static CLogFile* CLogFile::GetInstance();
protected:
void CLogFile::AddLogInner(const char* str);
void CLogFile::AddLogOnlyInner(const char* str);
protected:
/*-------------------------------------------
class members
-------------------------------------------*/
FILE* m_fpLog;
protected:
static CLogFile m_Log;
public:
int savenametest;//这是要接受值得变量
};
// CGyroHandlerDlg dialog
class CGyroHandlerDlg :
public CDialog,
implements public CComReadWriteHandler,
implements public CDatagramFilterHandler,
implements public CComConfigDlgHandler
{
// Construction
public:
CGyroHandlerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_GyroHandler_DIALOG };
public:
virtual void OnConfig(const TCHAR* pPort,const int baudrate, const int databits, const int stopbits, const int parity_mode )
{
this->m_port = pPort;
this->m_baudrate = baudrate;
this->m_databits = databits;
this->m_stopbits = stopbits;
this->m_parity_mode = parity_mode;
GetDlgItem(IDC_START_STOP)->EnableWindow(TRUE); //enable start Window now
}
public:
virtual void OnValidMessage(char* str,int size) // API of interface CDatagramFilterHandler
{
byte* bStr = (byte*) str;
byte b = 0;
CStringA errMsg;
CGyroParameter GyroPara;
TRACE(L"\r\n RECEIVED - head removed [");
for ( int i = 0; i < size; i ++)
{
TRACE(L" %x ",bStr[i]);
}
TRACE(L" ] \r\n");
//////verifying data validation
for ( int i = 0; i < size -1; i ++)
{
b = b ^ bStr[i];
}
if (b != bStr[size - 1])
{
errMsg = "Error! XOR verifying failure in CDatagramFilterHandler::OnValidMessage(...)\r\n";
/*ADD_LOG(LPCSTR(errMsg));*/
}
GyroPara.m_PlusNum = ( ( ( bStr[0] << 1) & 0x000000FF ) >> 1) |
( ( bStr[1] << 1 & 0x000000FF ) >> 1 << 7) |
( ( bStr[2] << 1 & 0x000000FF ) >> 1 << 14) |
( ( bStr[3] << 1 & 0x000000FF ) >> 1 << 21) |
( ( bStr[4] << 1 & 0x000000FF ) >> 1 << 28);
CStringA cs;
GyroPara.ToStringA(cs);
cs += "\n";
ADD_LOG(LPCSTR(cs)); // now gyro parameter is written to log file
///////////////////////////////////////////////////////////////////////////////////
CStringW csWindow;
m_queue.Append(GyroPara);
m_queue.ToStringW(csWindow);
/////////////////////////////////now update UI///////////////////////////////////
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_GYRO_OUT_WND);
if(pStatic)
{
pStatic->SetWindowText(LPCTSTR(csWindow));
}
if (ACQnumbertest==m_iACQnumber)
{
m_com.Close();
}
else
{
ACQnumbertest++;
}
}
// data read already, please delete buf in handler
virtual void OnDataReceived(BYTE* buf, int bufLen) // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Serier port data comes\r\n");
if(buf)
{
for(int i = 0; i < bufLen; i ++)
{
m_pMsgFilter->AddChar(buf[i]); // it will trig CDatagramFilterHandler::OnValidMessage(...)
}
}
else
{
TRACE(L"DEBUG:Error, NULL string received????\r\n");
}
}
// data already sent
virtual void OnDataSent() // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Finish Writing ...\r\n");
}
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
CComEngineCE m_com;
CDatagramFilter* m_pMsgFilter;
CGyroParaQueue m_queue;
// Generated message map functions
// virtual BOOL OnInitDialog();
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
afx_msg void OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/);
#endif
DECLARE_MESSAGE_MAP()
public:
CButton m_StartStopButton;
afx_msg void OnDestroy();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedStartStop();
afx_msg void OnBnClickedConfigComButton();
protected:
CComConfigDlg m_config_dlg;
CString m_port;
int m_baudrate;
int m_databits;
int m_stopbits;
int m_parity_mode;
public:
virtual BOOL OnInitDialog();
CComboBox m_comboSaveName;
int m_iSaveName;
int m_iACQnumber;
int ACQnumbertest;
afx_msg void OnBnClickedStationBtn();
CButton m_WestButton;
afx_msg void OnBnClickedWestBtn();
};
#6
// GyroHandlerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GyroHandler.h"
#include "GyroHandlerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*##########################################################################################
CLASS METHOD of CLogFile
###########################################################################################*/
CLogFile CLogFile::m_Log;
CLogFile::CLogFile():m_fpLog(NULL)
{
// 2. create log file itself
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
CStringA sFileName;
switch (isavenametest)
{
case 1:
sFileName.Format("西向位置.txt");
break;
case 2:
sFileName.Format("东向补偿.txt");
break;
case 3:
sFileName.Format("西向补偿.txt");
break;
default:
sFileName.Format("东向位置.txt");
break;
}
m_fpLog = fopen(LPCSTR(sFileName),"w");
if(!m_fpLog)
{
fprintf(stderr,"ERROR: fail to create log file\n");
return;
}
//--------------------------------------------------------------------
//printf("created log file, return\n");
CStringA cs;
cs.Format("");
AddLogOnlyInner(LPCSTR(cs));
return;
}
CLogFile::~CLogFile()
{
fclose(m_fpLog);
}
void CLogFile::operator <<(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
printf(str);
}
void CLogFile::AddLogInner(const char* str)
{
operator <<(str);
}
void CLogFile::AddLogOnlyInner(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
}
CLogFile* CLogFile::GetInstance()
{
return &m_Log;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
CGyroHandlerDlg::CGyroHandlerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGyroHandlerDlg::IDD, pParent)
, m_iSaveName(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// for ComMessageHandler
char msgHead[2];
msgHead[0] = (char)0x80;
msgHead[1] = 0;
this->m_pMsgFilter = new CDatagramFilter(msgHead,this,6,1);
VERIFY(m_pMsgFilter);
m_com.RegisterHandler(this);
m_config_dlg.RegisterHandler(this);
}
void CGyroHandlerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_START_STOP, m_StartStopButton);
DDX_Control(pDX, IDC_COMBO_SAVENAME, m_comboSaveName);
DDX_CBIndex(pDX, IDC_COMBO_SAVENAME, m_iSaveName);
DDX_Control(pDX, IDC_WEST_BTN, m_WestButton);
}
BEGIN_MESSAGE_MAP(CGyroHandlerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, &CGyroHandlerDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_START_STOP, &CGyroHandlerDlg::OnBnClickedStartStop)
ON_BN_CLICKED(IDC_CONFIG_COM_BUTTON, &CGyroHandlerDlg::OnBnClickedConfigComButton)
ON_BN_CLICKED(IDC_STATION_BTN, &CGyroHandlerDlg::OnBnClickedStationBtn)
ON_BN_CLICKED(IDC_WEST_BTN, &CGyroHandlerDlg::OnBnClickedWestBtn)
END_MESSAGE_MAP()
BOOL CGyroHandlerDlg::OnInitDialog()
{
__super::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDC_START_STOP)->EnableWindow(FALSE); // only after setting com port, then can start
//CProgressCtrl *pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
//pProg->SetRange(0,100);//设置范围
//pProg->SetPos(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGyroHandlerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG));
}
}
#endif
void CGyroHandlerDlg::OnDestroy()
{
// TODO: Add your message handler code here
m_com.Close();
if(m_pMsgFilter)
{
delete m_pMsgFilter;
m_pMsgFilter = NULL;
}
//---------------------------------------------------
__super::OnDestroy();
}
void CGyroHandlerDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
BYTE b[7];
b[0] = 0x80;
b[1] = rand() % 256;
b[2] = rand() % 256;
b[3] = rand() % 256;
b[4] = rand() % 256;
b[5] = rand() % 256;
b[6] = b[1] ^ b[2] ^ b[3] ^ b[4] ^ b [5];
TRACE(L"\r\n SEND [");
for ( int i = 0; i < 7; i ++)
{
TRACE(L" %x ",b[i]);
}
TRACE(L" ] \r\n");
m_com.SimulateDataCome(b,7);
}
// thread for "turning on" button
static DWORD WINAPI TurnOnButton(LPVOID lparam)
{
Sleep(600);
CWnd* pWnd = (CWnd*)lparam;
pWnd->EnableWindow(TRUE);
return TRUE;
}
void CGyroHandlerDlg::OnBnClickedConfigComButton()
{
// TODO: Add your control notification handler code here
m_config_dlg.DoModal();
}
void CGyroHandlerDlg::OnBnClickedStationBtn()
{
// TODO: 在此添加控件通知处理程序代码
/*CString strBtntext;
this->GetDlgItemText(IDC_START_STOP,strBtntext);*/
UpdateData(TRUE);
if (m_comboSaveName.GetCurSel()==0||m_comboSaveName.GetCurSel()==2)
{
CTotalStationDlg stationDlg;
stationDlg.DoModal();
}
}
void CGyroHandlerDlg::OnBnClickedWestBtn()
{
// TODO: 在此添加控件通知处理程序代码
CString strBtntext,ACQTime;
this->GetDlgItemText(IDC_WEST_BTN,strBtntext);
this->GetDlgItemText(IDC_EDIT_ACQUISITION_TIME,ACQTime);
m_iACQnumber=_wtoi(ACQTime)*400;
ACQnumbertest=0;
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
pApp->iSaveName=1;
savenametest=1;
if(m_com.IsOpen())
{
m_com.Close();
}
else
{
if(m_com.IsOpen() == FALSE)
{
if (! m_com.Open(LPCTSTR(m_port),m_baudrate,m_databits,m_parity_mode,m_stopbits))
{
MessageBox(L"Error! Fail to open Com port!",L"Error",MB_OKCANCEL | MB_ICONEXCLAMATION);
return; }
// write one string, later we need delete it!
CStringA sWrite = "Hi, How are you?\r\n";
m_com.Write((const BYTE*)LPCSTR(sWrite),sWrite.GetLength());
}
}
m_WestButton.EnableWindow(FALSE);
HANDLE hThread;
DWORD iThreadID;
hThread = CreateThread(NULL,0,TurnOnButton,(LPDWORD)(void*)&m_WestButton,0,&iThreadID);
if(hThread == NULL)
{
// fail to create thread, now directly enable button
TRACE(L"Error, fail to create thread!\r\n");
REPORT_ERROR();
m_WestButton.EnableWindow(TRUE);
}
}
//
#include "stdafx.h"
#include "GyroHandler.h"
#include "GyroHandlerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*##########################################################################################
CLASS METHOD of CLogFile
###########################################################################################*/
CLogFile CLogFile::m_Log;
CLogFile::CLogFile():m_fpLog(NULL)
{
// 2. create log file itself
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
CStringA sFileName;
switch (isavenametest)
{
case 1:
sFileName.Format("西向位置.txt");
break;
case 2:
sFileName.Format("东向补偿.txt");
break;
case 3:
sFileName.Format("西向补偿.txt");
break;
default:
sFileName.Format("东向位置.txt");
break;
}
m_fpLog = fopen(LPCSTR(sFileName),"w");
if(!m_fpLog)
{
fprintf(stderr,"ERROR: fail to create log file\n");
return;
}
//--------------------------------------------------------------------
//printf("created log file, return\n");
CStringA cs;
cs.Format("");
AddLogOnlyInner(LPCSTR(cs));
return;
}
CLogFile::~CLogFile()
{
fclose(m_fpLog);
}
void CLogFile::operator <<(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
printf(str);
}
void CLogFile::AddLogInner(const char* str)
{
operator <<(str);
}
void CLogFile::AddLogOnlyInner(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
}
CLogFile* CLogFile::GetInstance()
{
return &m_Log;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
CGyroHandlerDlg::CGyroHandlerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGyroHandlerDlg::IDD, pParent)
, m_iSaveName(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// for ComMessageHandler
char msgHead[2];
msgHead[0] = (char)0x80;
msgHead[1] = 0;
this->m_pMsgFilter = new CDatagramFilter(msgHead,this,6,1);
VERIFY(m_pMsgFilter);
m_com.RegisterHandler(this);
m_config_dlg.RegisterHandler(this);
}
void CGyroHandlerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_START_STOP, m_StartStopButton);
DDX_Control(pDX, IDC_COMBO_SAVENAME, m_comboSaveName);
DDX_CBIndex(pDX, IDC_COMBO_SAVENAME, m_iSaveName);
DDX_Control(pDX, IDC_WEST_BTN, m_WestButton);
}
BEGIN_MESSAGE_MAP(CGyroHandlerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, &CGyroHandlerDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_START_STOP, &CGyroHandlerDlg::OnBnClickedStartStop)
ON_BN_CLICKED(IDC_CONFIG_COM_BUTTON, &CGyroHandlerDlg::OnBnClickedConfigComButton)
ON_BN_CLICKED(IDC_STATION_BTN, &CGyroHandlerDlg::OnBnClickedStationBtn)
ON_BN_CLICKED(IDC_WEST_BTN, &CGyroHandlerDlg::OnBnClickedWestBtn)
END_MESSAGE_MAP()
BOOL CGyroHandlerDlg::OnInitDialog()
{
__super::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDC_START_STOP)->EnableWindow(FALSE); // only after setting com port, then can start
//CProgressCtrl *pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
//pProg->SetRange(0,100);//设置范围
//pProg->SetPos(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGyroHandlerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG));
}
}
#endif
void CGyroHandlerDlg::OnDestroy()
{
// TODO: Add your message handler code here
m_com.Close();
if(m_pMsgFilter)
{
delete m_pMsgFilter;
m_pMsgFilter = NULL;
}
//---------------------------------------------------
__super::OnDestroy();
}
void CGyroHandlerDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
BYTE b[7];
b[0] = 0x80;
b[1] = rand() % 256;
b[2] = rand() % 256;
b[3] = rand() % 256;
b[4] = rand() % 256;
b[5] = rand() % 256;
b[6] = b[1] ^ b[2] ^ b[3] ^ b[4] ^ b [5];
TRACE(L"\r\n SEND [");
for ( int i = 0; i < 7; i ++)
{
TRACE(L" %x ",b[i]);
}
TRACE(L" ] \r\n");
m_com.SimulateDataCome(b,7);
}
// thread for "turning on" button
static DWORD WINAPI TurnOnButton(LPVOID lparam)
{
Sleep(600);
CWnd* pWnd = (CWnd*)lparam;
pWnd->EnableWindow(TRUE);
return TRUE;
}
void CGyroHandlerDlg::OnBnClickedConfigComButton()
{
// TODO: Add your control notification handler code here
m_config_dlg.DoModal();
}
void CGyroHandlerDlg::OnBnClickedStationBtn()
{
// TODO: 在此添加控件通知处理程序代码
/*CString strBtntext;
this->GetDlgItemText(IDC_START_STOP,strBtntext);*/
UpdateData(TRUE);
if (m_comboSaveName.GetCurSel()==0||m_comboSaveName.GetCurSel()==2)
{
CTotalStationDlg stationDlg;
stationDlg.DoModal();
}
}
void CGyroHandlerDlg::OnBnClickedWestBtn()
{
// TODO: 在此添加控件通知处理程序代码
CString strBtntext,ACQTime;
this->GetDlgItemText(IDC_WEST_BTN,strBtntext);
this->GetDlgItemText(IDC_EDIT_ACQUISITION_TIME,ACQTime);
m_iACQnumber=_wtoi(ACQTime)*400;
ACQnumbertest=0;
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
pApp->iSaveName=1;
savenametest=1;
if(m_com.IsOpen())
{
m_com.Close();
}
else
{
if(m_com.IsOpen() == FALSE)
{
if (! m_com.Open(LPCTSTR(m_port),m_baudrate,m_databits,m_parity_mode,m_stopbits))
{
MessageBox(L"Error! Fail to open Com port!",L"Error",MB_OKCANCEL | MB_ICONEXCLAMATION);
return; }
// write one string, later we need delete it!
CStringA sWrite = "Hi, How are you?\r\n";
m_com.Write((const BYTE*)LPCSTR(sWrite),sWrite.GetLength());
}
}
m_WestButton.EnableWindow(FALSE);
HANDLE hThread;
DWORD iThreadID;
hThread = CreateThread(NULL,0,TurnOnButton,(LPDWORD)(void*)&m_WestButton,0,&iThreadID);
if(hThread == NULL)
{
// fail to create thread, now directly enable button
TRACE(L"Error, fail to create thread!\r\n");
REPORT_ERROR();
m_WestButton.EnableWindow(TRUE);
}
}
#1
你要传递也是要向A的一个对象传递
#2
你指的是友元的方式?
#3
直接调用A类的成员函数
#4
跟是否在同一个文件没有关系吧。
也许是指的友元
也许是指的友元
#5
这是一个串口软件的一部分
// GyroHandlerDlg.h : header file
//
#pragma once
#include "ComEngineCE.h"
#include "DatagramFilter.h"
#include "GyroParaQueue.h"
#include "ComConfigDlg.h"
#include "ComPortEnum.h"
#include "ResolveDlg.h"
#include "TotalStationDlg.h"
#include "afxwin.h"
#define implements
//////////////////////////////////////////////////////////////////////////////////////////////////////
// class CLogFile is used to implement information log
// you do not need to call it directly, you just need to call macro
//
// ADD_LOG(format_string,...) // ADD information to log file, and at the same time output to the screen
// ADD_LOG1(format_string,...) // Add information to log file, but show nothing in the screen
// ADD_LOG2(format_string,...) // show information in the screen, but do not store in the screen
////////////////////////////////////////////////////////////////////////////////////////////////
#define ADD_LOG CLogFile::GetInstance()->AddLog // add log informaiton both to log file and screen
#define ADD_LOG1 CLogFile::GetInstance()->AddLogOnly // only put information to log file
#define ADD_LOG2 printf
// only put information to screen
class CLogFile
{
public:
CLogFile::CLogFile();
CLogFile::~CLogFile();
public:
void CLogFile::operator <<(const char* str);
void CLogFile::AddLog(const char* format,...)
{
if(!format)
{
return;
}
CStringA s;
va_list argList;
va_start(argList, format);
s.FormatV(format, argList);
va_end(argList);
AddLogInner(LPCSTR(s));
}
void CLogFile::AddLogOnly(const char* format,...)
{
//printf(" --- before initiate call stack object of AddLogOnly() --\n");
//CALL_STACK("void CLogFile::AddLogOnly(const char* format,...)");
if(!format)
{
return;
}
//printf("----- xxxx.1 ----\n");
CStringA s;
va_list argList;
va_start(argList, format);
//printf("---- xxxx.2 ----\n");
//fflush(NULL);
s.FormatV(format, argList);
va_end(argList);
//printf("---- xxxx.3 ----\n");
AddLogOnlyInner(LPCSTR(s));
}
static CLogFile* CLogFile::GetInstance();
protected:
void CLogFile::AddLogInner(const char* str);
void CLogFile::AddLogOnlyInner(const char* str);
protected:
/*-------------------------------------------
class members
-------------------------------------------*/
FILE* m_fpLog;
protected:
static CLogFile m_Log;
public:
int savenametest;//这是要接受值得变量
};
// CGyroHandlerDlg dialog
class CGyroHandlerDlg :
public CDialog,
implements public CComReadWriteHandler,
implements public CDatagramFilterHandler,
implements public CComConfigDlgHandler
{
// Construction
public:
CGyroHandlerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_GyroHandler_DIALOG };
public:
virtual void OnConfig(const TCHAR* pPort,const int baudrate, const int databits, const int stopbits, const int parity_mode )
{
this->m_port = pPort;
this->m_baudrate = baudrate;
this->m_databits = databits;
this->m_stopbits = stopbits;
this->m_parity_mode = parity_mode;
GetDlgItem(IDC_START_STOP)->EnableWindow(TRUE); //enable start Window now
}
public:
virtual void OnValidMessage(char* str,int size) // API of interface CDatagramFilterHandler
{
byte* bStr = (byte*) str;
byte b = 0;
CStringA errMsg;
CGyroParameter GyroPara;
TRACE(L"\r\n RECEIVED - head removed [");
for ( int i = 0; i < size; i ++)
{
TRACE(L" %x ",bStr[i]);
}
TRACE(L" ] \r\n");
//////verifying data validation
for ( int i = 0; i < size -1; i ++)
{
b = b ^ bStr[i];
}
if (b != bStr[size - 1])
{
errMsg = "Error! XOR verifying failure in CDatagramFilterHandler::OnValidMessage(...)\r\n";
/*ADD_LOG(LPCSTR(errMsg));*/
}
GyroPara.m_PlusNum = ( ( ( bStr[0] << 1) & 0x000000FF ) >> 1) |
( ( bStr[1] << 1 & 0x000000FF ) >> 1 << 7) |
( ( bStr[2] << 1 & 0x000000FF ) >> 1 << 14) |
( ( bStr[3] << 1 & 0x000000FF ) >> 1 << 21) |
( ( bStr[4] << 1 & 0x000000FF ) >> 1 << 28);
CStringA cs;
GyroPara.ToStringA(cs);
cs += "\n";
ADD_LOG(LPCSTR(cs)); // now gyro parameter is written to log file
///////////////////////////////////////////////////////////////////////////////////
CStringW csWindow;
m_queue.Append(GyroPara);
m_queue.ToStringW(csWindow);
/////////////////////////////////now update UI///////////////////////////////////
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_GYRO_OUT_WND);
if(pStatic)
{
pStatic->SetWindowText(LPCTSTR(csWindow));
}
if (ACQnumbertest==m_iACQnumber)
{
m_com.Close();
}
else
{
ACQnumbertest++;
}
}
// data read already, please delete buf in handler
virtual void OnDataReceived(BYTE* buf, int bufLen) // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Serier port data comes\r\n");
if(buf)
{
for(int i = 0; i < bufLen; i ++)
{
m_pMsgFilter->AddChar(buf[i]); // it will trig CDatagramFilterHandler::OnValidMessage(...)
}
}
else
{
TRACE(L"DEBUG:Error, NULL string received????\r\n");
}
}
// data already sent
virtual void OnDataSent() // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Finish Writing ...\r\n");
}
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
CComEngineCE m_com;
CDatagramFilter* m_pMsgFilter;
CGyroParaQueue m_queue;
// Generated message map functions
// virtual BOOL OnInitDialog();
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
afx_msg void OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/);
#endif
DECLARE_MESSAGE_MAP()
public:
CButton m_StartStopButton;
afx_msg void OnDestroy();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedStartStop();
afx_msg void OnBnClickedConfigComButton();
protected:
CComConfigDlg m_config_dlg;
CString m_port;
int m_baudrate;
int m_databits;
int m_stopbits;
int m_parity_mode;
public:
virtual BOOL OnInitDialog();
CComboBox m_comboSaveName;
int m_iSaveName;
int m_iACQnumber;
int ACQnumbertest;
afx_msg void OnBnClickedStationBtn();
CButton m_WestButton;
afx_msg void OnBnClickedWestBtn();
};
// GyroHandlerDlg.h : header file
//
#pragma once
#include "ComEngineCE.h"
#include "DatagramFilter.h"
#include "GyroParaQueue.h"
#include "ComConfigDlg.h"
#include "ComPortEnum.h"
#include "ResolveDlg.h"
#include "TotalStationDlg.h"
#include "afxwin.h"
#define implements
//////////////////////////////////////////////////////////////////////////////////////////////////////
// class CLogFile is used to implement information log
// you do not need to call it directly, you just need to call macro
//
// ADD_LOG(format_string,...) // ADD information to log file, and at the same time output to the screen
// ADD_LOG1(format_string,...) // Add information to log file, but show nothing in the screen
// ADD_LOG2(format_string,...) // show information in the screen, but do not store in the screen
////////////////////////////////////////////////////////////////////////////////////////////////
#define ADD_LOG CLogFile::GetInstance()->AddLog // add log informaiton both to log file and screen
#define ADD_LOG1 CLogFile::GetInstance()->AddLogOnly // only put information to log file
#define ADD_LOG2 printf
// only put information to screen
class CLogFile
{
public:
CLogFile::CLogFile();
CLogFile::~CLogFile();
public:
void CLogFile::operator <<(const char* str);
void CLogFile::AddLog(const char* format,...)
{
if(!format)
{
return;
}
CStringA s;
va_list argList;
va_start(argList, format);
s.FormatV(format, argList);
va_end(argList);
AddLogInner(LPCSTR(s));
}
void CLogFile::AddLogOnly(const char* format,...)
{
//printf(" --- before initiate call stack object of AddLogOnly() --\n");
//CALL_STACK("void CLogFile::AddLogOnly(const char* format,...)");
if(!format)
{
return;
}
//printf("----- xxxx.1 ----\n");
CStringA s;
va_list argList;
va_start(argList, format);
//printf("---- xxxx.2 ----\n");
//fflush(NULL);
s.FormatV(format, argList);
va_end(argList);
//printf("---- xxxx.3 ----\n");
AddLogOnlyInner(LPCSTR(s));
}
static CLogFile* CLogFile::GetInstance();
protected:
void CLogFile::AddLogInner(const char* str);
void CLogFile::AddLogOnlyInner(const char* str);
protected:
/*-------------------------------------------
class members
-------------------------------------------*/
FILE* m_fpLog;
protected:
static CLogFile m_Log;
public:
int savenametest;//这是要接受值得变量
};
// CGyroHandlerDlg dialog
class CGyroHandlerDlg :
public CDialog,
implements public CComReadWriteHandler,
implements public CDatagramFilterHandler,
implements public CComConfigDlgHandler
{
// Construction
public:
CGyroHandlerDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_GyroHandler_DIALOG };
public:
virtual void OnConfig(const TCHAR* pPort,const int baudrate, const int databits, const int stopbits, const int parity_mode )
{
this->m_port = pPort;
this->m_baudrate = baudrate;
this->m_databits = databits;
this->m_stopbits = stopbits;
this->m_parity_mode = parity_mode;
GetDlgItem(IDC_START_STOP)->EnableWindow(TRUE); //enable start Window now
}
public:
virtual void OnValidMessage(char* str,int size) // API of interface CDatagramFilterHandler
{
byte* bStr = (byte*) str;
byte b = 0;
CStringA errMsg;
CGyroParameter GyroPara;
TRACE(L"\r\n RECEIVED - head removed [");
for ( int i = 0; i < size; i ++)
{
TRACE(L" %x ",bStr[i]);
}
TRACE(L" ] \r\n");
//////verifying data validation
for ( int i = 0; i < size -1; i ++)
{
b = b ^ bStr[i];
}
if (b != bStr[size - 1])
{
errMsg = "Error! XOR verifying failure in CDatagramFilterHandler::OnValidMessage(...)\r\n";
/*ADD_LOG(LPCSTR(errMsg));*/
}
GyroPara.m_PlusNum = ( ( ( bStr[0] << 1) & 0x000000FF ) >> 1) |
( ( bStr[1] << 1 & 0x000000FF ) >> 1 << 7) |
( ( bStr[2] << 1 & 0x000000FF ) >> 1 << 14) |
( ( bStr[3] << 1 & 0x000000FF ) >> 1 << 21) |
( ( bStr[4] << 1 & 0x000000FF ) >> 1 << 28);
CStringA cs;
GyroPara.ToStringA(cs);
cs += "\n";
ADD_LOG(LPCSTR(cs)); // now gyro parameter is written to log file
///////////////////////////////////////////////////////////////////////////////////
CStringW csWindow;
m_queue.Append(GyroPara);
m_queue.ToStringW(csWindow);
/////////////////////////////////now update UI///////////////////////////////////
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_GYRO_OUT_WND);
if(pStatic)
{
pStatic->SetWindowText(LPCTSTR(csWindow));
}
if (ACQnumbertest==m_iACQnumber)
{
m_com.Close();
}
else
{
ACQnumbertest++;
}
}
// data read already, please delete buf in handler
virtual void OnDataReceived(BYTE* buf, int bufLen) // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Serier port data comes\r\n");
if(buf)
{
for(int i = 0; i < bufLen; i ++)
{
m_pMsgFilter->AddChar(buf[i]); // it will trig CDatagramFilterHandler::OnValidMessage(...)
}
}
else
{
TRACE(L"DEBUG:Error, NULL string received????\r\n");
}
}
// data already sent
virtual void OnDataSent() // API of interface CComReadWriteHandler
{
TRACE(L"DEBUG: Finish Writing ...\r\n");
}
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
CComEngineCE m_com;
CDatagramFilter* m_pMsgFilter;
CGyroParaQueue m_queue;
// Generated message map functions
// virtual BOOL OnInitDialog();
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
afx_msg void OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/);
#endif
DECLARE_MESSAGE_MAP()
public:
CButton m_StartStopButton;
afx_msg void OnDestroy();
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedStartStop();
afx_msg void OnBnClickedConfigComButton();
protected:
CComConfigDlg m_config_dlg;
CString m_port;
int m_baudrate;
int m_databits;
int m_stopbits;
int m_parity_mode;
public:
virtual BOOL OnInitDialog();
CComboBox m_comboSaveName;
int m_iSaveName;
int m_iACQnumber;
int ACQnumbertest;
afx_msg void OnBnClickedStationBtn();
CButton m_WestButton;
afx_msg void OnBnClickedWestBtn();
};
#6
// GyroHandlerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "GyroHandler.h"
#include "GyroHandlerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*##########################################################################################
CLASS METHOD of CLogFile
###########################################################################################*/
CLogFile CLogFile::m_Log;
CLogFile::CLogFile():m_fpLog(NULL)
{
// 2. create log file itself
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
CStringA sFileName;
switch (isavenametest)
{
case 1:
sFileName.Format("西向位置.txt");
break;
case 2:
sFileName.Format("东向补偿.txt");
break;
case 3:
sFileName.Format("西向补偿.txt");
break;
default:
sFileName.Format("东向位置.txt");
break;
}
m_fpLog = fopen(LPCSTR(sFileName),"w");
if(!m_fpLog)
{
fprintf(stderr,"ERROR: fail to create log file\n");
return;
}
//--------------------------------------------------------------------
//printf("created log file, return\n");
CStringA cs;
cs.Format("");
AddLogOnlyInner(LPCSTR(cs));
return;
}
CLogFile::~CLogFile()
{
fclose(m_fpLog);
}
void CLogFile::operator <<(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
printf(str);
}
void CLogFile::AddLogInner(const char* str)
{
operator <<(str);
}
void CLogFile::AddLogOnlyInner(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
}
CLogFile* CLogFile::GetInstance()
{
return &m_Log;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
CGyroHandlerDlg::CGyroHandlerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGyroHandlerDlg::IDD, pParent)
, m_iSaveName(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// for ComMessageHandler
char msgHead[2];
msgHead[0] = (char)0x80;
msgHead[1] = 0;
this->m_pMsgFilter = new CDatagramFilter(msgHead,this,6,1);
VERIFY(m_pMsgFilter);
m_com.RegisterHandler(this);
m_config_dlg.RegisterHandler(this);
}
void CGyroHandlerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_START_STOP, m_StartStopButton);
DDX_Control(pDX, IDC_COMBO_SAVENAME, m_comboSaveName);
DDX_CBIndex(pDX, IDC_COMBO_SAVENAME, m_iSaveName);
DDX_Control(pDX, IDC_WEST_BTN, m_WestButton);
}
BEGIN_MESSAGE_MAP(CGyroHandlerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, &CGyroHandlerDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_START_STOP, &CGyroHandlerDlg::OnBnClickedStartStop)
ON_BN_CLICKED(IDC_CONFIG_COM_BUTTON, &CGyroHandlerDlg::OnBnClickedConfigComButton)
ON_BN_CLICKED(IDC_STATION_BTN, &CGyroHandlerDlg::OnBnClickedStationBtn)
ON_BN_CLICKED(IDC_WEST_BTN, &CGyroHandlerDlg::OnBnClickedWestBtn)
END_MESSAGE_MAP()
BOOL CGyroHandlerDlg::OnInitDialog()
{
__super::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDC_START_STOP)->EnableWindow(FALSE); // only after setting com port, then can start
//CProgressCtrl *pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
//pProg->SetRange(0,100);//设置范围
//pProg->SetPos(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGyroHandlerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG));
}
}
#endif
void CGyroHandlerDlg::OnDestroy()
{
// TODO: Add your message handler code here
m_com.Close();
if(m_pMsgFilter)
{
delete m_pMsgFilter;
m_pMsgFilter = NULL;
}
//---------------------------------------------------
__super::OnDestroy();
}
void CGyroHandlerDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
BYTE b[7];
b[0] = 0x80;
b[1] = rand() % 256;
b[2] = rand() % 256;
b[3] = rand() % 256;
b[4] = rand() % 256;
b[5] = rand() % 256;
b[6] = b[1] ^ b[2] ^ b[3] ^ b[4] ^ b [5];
TRACE(L"\r\n SEND [");
for ( int i = 0; i < 7; i ++)
{
TRACE(L" %x ",b[i]);
}
TRACE(L" ] \r\n");
m_com.SimulateDataCome(b,7);
}
// thread for "turning on" button
static DWORD WINAPI TurnOnButton(LPVOID lparam)
{
Sleep(600);
CWnd* pWnd = (CWnd*)lparam;
pWnd->EnableWindow(TRUE);
return TRUE;
}
void CGyroHandlerDlg::OnBnClickedConfigComButton()
{
// TODO: Add your control notification handler code here
m_config_dlg.DoModal();
}
void CGyroHandlerDlg::OnBnClickedStationBtn()
{
// TODO: 在此添加控件通知处理程序代码
/*CString strBtntext;
this->GetDlgItemText(IDC_START_STOP,strBtntext);*/
UpdateData(TRUE);
if (m_comboSaveName.GetCurSel()==0||m_comboSaveName.GetCurSel()==2)
{
CTotalStationDlg stationDlg;
stationDlg.DoModal();
}
}
void CGyroHandlerDlg::OnBnClickedWestBtn()
{
// TODO: 在此添加控件通知处理程序代码
CString strBtntext,ACQTime;
this->GetDlgItemText(IDC_WEST_BTN,strBtntext);
this->GetDlgItemText(IDC_EDIT_ACQUISITION_TIME,ACQTime);
m_iACQnumber=_wtoi(ACQTime)*400;
ACQnumbertest=0;
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
pApp->iSaveName=1;
savenametest=1;
if(m_com.IsOpen())
{
m_com.Close();
}
else
{
if(m_com.IsOpen() == FALSE)
{
if (! m_com.Open(LPCTSTR(m_port),m_baudrate,m_databits,m_parity_mode,m_stopbits))
{
MessageBox(L"Error! Fail to open Com port!",L"Error",MB_OKCANCEL | MB_ICONEXCLAMATION);
return; }
// write one string, later we need delete it!
CStringA sWrite = "Hi, How are you?\r\n";
m_com.Write((const BYTE*)LPCSTR(sWrite),sWrite.GetLength());
}
}
m_WestButton.EnableWindow(FALSE);
HANDLE hThread;
DWORD iThreadID;
hThread = CreateThread(NULL,0,TurnOnButton,(LPDWORD)(void*)&m_WestButton,0,&iThreadID);
if(hThread == NULL)
{
// fail to create thread, now directly enable button
TRACE(L"Error, fail to create thread!\r\n");
REPORT_ERROR();
m_WestButton.EnableWindow(TRUE);
}
}
//
#include "stdafx.h"
#include "GyroHandler.h"
#include "GyroHandlerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*##########################################################################################
CLASS METHOD of CLogFile
###########################################################################################*/
CLogFile CLogFile::m_Log;
CLogFile::CLogFile():m_fpLog(NULL)
{
// 2. create log file itself
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
CStringA sFileName;
switch (isavenametest)
{
case 1:
sFileName.Format("西向位置.txt");
break;
case 2:
sFileName.Format("东向补偿.txt");
break;
case 3:
sFileName.Format("西向补偿.txt");
break;
default:
sFileName.Format("东向位置.txt");
break;
}
m_fpLog = fopen(LPCSTR(sFileName),"w");
if(!m_fpLog)
{
fprintf(stderr,"ERROR: fail to create log file\n");
return;
}
//--------------------------------------------------------------------
//printf("created log file, return\n");
CStringA cs;
cs.Format("");
AddLogOnlyInner(LPCSTR(cs));
return;
}
CLogFile::~CLogFile()
{
fclose(m_fpLog);
}
void CLogFile::operator <<(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
printf(str);
}
void CLogFile::AddLogInner(const char* str)
{
operator <<(str);
}
void CLogFile::AddLogOnlyInner(const char* str)
{
if(m_fpLog)
{
fprintf(m_fpLog,str);
fflush(m_fpLog);
}
}
CLogFile* CLogFile::GetInstance()
{
return &m_Log;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
CGyroHandlerDlg::CGyroHandlerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGyroHandlerDlg::IDD, pParent)
, m_iSaveName(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// for ComMessageHandler
char msgHead[2];
msgHead[0] = (char)0x80;
msgHead[1] = 0;
this->m_pMsgFilter = new CDatagramFilter(msgHead,this,6,1);
VERIFY(m_pMsgFilter);
m_com.RegisterHandler(this);
m_config_dlg.RegisterHandler(this);
}
void CGyroHandlerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_START_STOP, m_StartStopButton);
DDX_Control(pDX, IDC_COMBO_SAVENAME, m_comboSaveName);
DDX_CBIndex(pDX, IDC_COMBO_SAVENAME, m_iSaveName);
DDX_Control(pDX, IDC_WEST_BTN, m_WestButton);
}
BEGIN_MESSAGE_MAP(CGyroHandlerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON1, &CGyroHandlerDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_START_STOP, &CGyroHandlerDlg::OnBnClickedStartStop)
ON_BN_CLICKED(IDC_CONFIG_COM_BUTTON, &CGyroHandlerDlg::OnBnClickedConfigComButton)
ON_BN_CLICKED(IDC_STATION_BTN, &CGyroHandlerDlg::OnBnClickedStationBtn)
ON_BN_CLICKED(IDC_WEST_BTN, &CGyroHandlerDlg::OnBnClickedWestBtn)
END_MESSAGE_MAP()
BOOL CGyroHandlerDlg::OnInitDialog()
{
__super::OnInitDialog();
// TODO: Add extra initialization here
GetDlgItem(IDC_START_STOP)->EnableWindow(FALSE); // only after setting com port, then can start
//CProgressCtrl *pProg=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
//pProg->SetRange(0,100);//设置范围
//pProg->SetPos(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CGyroHandlerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
if (AfxIsDRAEnabled())
{
DRA::RelayoutDialog(
AfxGetResourceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_GyroHandler_DIALOG));
}
}
#endif
void CGyroHandlerDlg::OnDestroy()
{
// TODO: Add your message handler code here
m_com.Close();
if(m_pMsgFilter)
{
delete m_pMsgFilter;
m_pMsgFilter = NULL;
}
//---------------------------------------------------
__super::OnDestroy();
}
void CGyroHandlerDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
BYTE b[7];
b[0] = 0x80;
b[1] = rand() % 256;
b[2] = rand() % 256;
b[3] = rand() % 256;
b[4] = rand() % 256;
b[5] = rand() % 256;
b[6] = b[1] ^ b[2] ^ b[3] ^ b[4] ^ b [5];
TRACE(L"\r\n SEND [");
for ( int i = 0; i < 7; i ++)
{
TRACE(L" %x ",b[i]);
}
TRACE(L" ] \r\n");
m_com.SimulateDataCome(b,7);
}
// thread for "turning on" button
static DWORD WINAPI TurnOnButton(LPVOID lparam)
{
Sleep(600);
CWnd* pWnd = (CWnd*)lparam;
pWnd->EnableWindow(TRUE);
return TRUE;
}
void CGyroHandlerDlg::OnBnClickedConfigComButton()
{
// TODO: Add your control notification handler code here
m_config_dlg.DoModal();
}
void CGyroHandlerDlg::OnBnClickedStationBtn()
{
// TODO: 在此添加控件通知处理程序代码
/*CString strBtntext;
this->GetDlgItemText(IDC_START_STOP,strBtntext);*/
UpdateData(TRUE);
if (m_comboSaveName.GetCurSel()==0||m_comboSaveName.GetCurSel()==2)
{
CTotalStationDlg stationDlg;
stationDlg.DoModal();
}
}
void CGyroHandlerDlg::OnBnClickedWestBtn()
{
// TODO: 在此添加控件通知处理程序代码
CString strBtntext,ACQTime;
this->GetDlgItemText(IDC_WEST_BTN,strBtntext);
this->GetDlgItemText(IDC_EDIT_ACQUISITION_TIME,ACQTime);
m_iACQnumber=_wtoi(ACQTime)*400;
ACQnumbertest=0;
CGyroHandlerApp *pApp = (CGyroHandlerApp *)AfxGetApp();
pApp->iSaveName=1;
savenametest=1;
if(m_com.IsOpen())
{
m_com.Close();
}
else
{
if(m_com.IsOpen() == FALSE)
{
if (! m_com.Open(LPCTSTR(m_port),m_baudrate,m_databits,m_parity_mode,m_stopbits))
{
MessageBox(L"Error! Fail to open Com port!",L"Error",MB_OKCANCEL | MB_ICONEXCLAMATION);
return; }
// write one string, later we need delete it!
CStringA sWrite = "Hi, How are you?\r\n";
m_com.Write((const BYTE*)LPCSTR(sWrite),sWrite.GetLength());
}
}
m_WestButton.EnableWindow(FALSE);
HANDLE hThread;
DWORD iThreadID;
hThread = CreateThread(NULL,0,TurnOnButton,(LPDWORD)(void*)&m_WestButton,0,&iThreadID);
if(hThread == NULL)
{
// fail to create thread, now directly enable button
TRACE(L"Error, fail to create thread!\r\n");
REPORT_ERROR();
m_WestButton.EnableWindow(TRUE);
}
}