在vc中使用静态变量的问题++ MFC
mydialog 类声明。 …………………… 我的工作是创建一个带有按钮和文本框的窗口,当我第一次单击该按钮时,将创建一个线程,该线程将在所选端口上读取,对于按钮上文本框中的后续数据,单击它只会写入端口而不是创建线程进行读取。
#include "resource.h"
class mydialog : public CDialog
{
DECLARE_DYNAMIC(mydialog)
public:
mydialog(CWnd* pParent = NULL); // standard constructor
virtual ~mydialog();
static HANDLE hSerial4;
static int loop_count;
// Dialog Data
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
CString m_name;
afx_msg void OnBnClickedButton1();
BOOL OnInitDialog();
static UINT read_data(LPVOID hSerial);
// void read_data(HANDLE);
CString select_port;
};
cpreparationapp.cpp ........................
#include "stdafx.h"
#include "mydialog.h"
class CPreparationApp : public CWinApp
{
public:
BOOL InitInstance();
};
BOOL CPreparationApp::InitInstance()
{
mydialog Dlg;
Dlg.DoModal();
m_pMainWnd = &Dlg;
return TRUE;
}
CPreparationApp theApp;
mydialog.cpp ……………………
#include "stdafx.h"
#include "mydialog.h"
#include <windows.h>
#include <process.h>
// mydialog dialog
IMPLEMENT_DYNAMIC(mydialog, CDialog)
mydialog::mydialog(CWnd* pParent /*=NULL*/)
: CDialog(mydialog::IDD, pParent)
, m_name(_T(""))
, select_port(_T(""))
{
}
mydialog::~mydialog()
{
}
void mydialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_name);
DDX_CBString(pDX, IDC_COMBO1, select_port);
}
BEGIN_MESSAGE_MAP(mydialog, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &mydialog::OnBnClickedButton1)
END_MESSAGE_MAP()
// mydialog message handlers
void mydialog::OnBnClickedButton1()
{
CEdit* abc=(CEdit*)GetDlgItem(IDC_EDIT1);
LPTSTR xyz123=new TCHAR[50];
CString text;
int k=abc->GetWindowTextA(xyz123,20);
CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
int item = pCombo->GetCurSel();
if(item != CB_ERR)
{
pCombo->GetLBText(item,text);
}
TRACE("%s\n",text);
loop_count++;
HANDLE hSerial;
if(loop_count==1)
{
hSerial = CreateFile(text,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
TRACE("serial port does not exist for writing\n");
//serial port does not exist. Inform user.
}
TRACE("some other error,serial port does not exist for writing\n");
//some other error occurred. Inform user.
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
TRACE("error getting state for writing\n");
//error getting state
}
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams))
{
TRACE("error setting state for writing\n");
//error setting serial port state
}
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts))
{
TRACE("some error occured for writing\n");
//error occureed. Inform user
}
mydialog::hSerial4=hSerial;
}
int n=100;
char szBuff1[100];
int m=0;
while(m<100 && xyz123[m]!='\0')
{
szBuff1[m]=xyz123[m];
m++;
}
szBuff1[m]='\0';
DWORD dwByteswrote = 0;
if(!WriteFile(mydialog::hSerial4, szBuff1, n, &dwByteswrote, NULL))
{
TRACE("error writing \n");
}
TRACE("%d\n",dwByteswrote);
// mydialog Dlg1;
if(loop_count==1)
{
CWinThread *m_pThread;
m_pThread=AfxBeginThread(read_data,(LPVOID)hSerial);
}
}
UINT mydialog::read_data(LPVOID hSerial5)
{
HANDLE hSerial1=(HANDLE)hSerial5;
int n=100;
char szBuff[100];
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial1, szBuff, n, &dwBytesRead, NULL))
{
TRACE("error reading \n");
//error occurred. Report to user.
}
TRACE("%s\n",szBuff);
// TRACE("%d\n",dwBytesRead);
CloseHandle(hSerial1);
return 1;
}
BOOL mydialog::OnInitDialog()
{
// BOOL x;
mydialog dialog2;
HANDLE hSerial3;
CString temp,temp1;
int n=0;
CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
pCombo->AddString("SAMPLE1");
pCombo->AddString("SAMPLE2");
for(n=0;n<9;n++)
{
temp1.Format("%d",n);
temp="COM"+temp1;
hSerial3 = CreateFile(temp,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial3==INVALID_HANDLE_VALUE)
{
TRACE("port not available\n");
}
else
{
pCombo->AddString(temp);
CloseHandle(hSerial3);
TRACE("port available\n");
}
}
// x=dialog2.check_open_port();
return CDialog::OnInitDialog();
}
class mydialog declaration.
...........................
my work is to create a window with a button and a textbox and when i click that button for the 1st time a thread will be created which will be reading on the port selected and for the subsequent data in the textbox on button click it will just write to the port rather than creating a thread for reading.
#include "resource.h"
class mydialog : public CDialog
{
DECLARE_DYNAMIC(mydialog)
public:
mydialog(CWnd* pParent = NULL); // standard constructor
virtual ~mydialog();
static HANDLE hSerial4;
static int loop_count;
// Dialog Data
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
CString m_name;
afx_msg void OnBnClickedButton1();
BOOL OnInitDialog();
static UINT read_data(LPVOID hSerial);
// void read_data(HANDLE);
CString select_port;
};
cpreparationapp.cpp
...........................
#include "stdafx.h"
#include "mydialog.h"
class CPreparationApp : public CWinApp
{
public:
BOOL InitInstance();
};
BOOL CPreparationApp::InitInstance()
{
mydialog Dlg;
Dlg.DoModal();
m_pMainWnd = &Dlg;
return TRUE;
}
CPreparationApp theApp;
mydialog.cpp
..........................
#include "stdafx.h"
#include "mydialog.h"
#include <windows.h>
#include <process.h>
// mydialog dialog
IMPLEMENT_DYNAMIC(mydialog, CDialog)
mydialog::mydialog(CWnd* pParent /*=NULL*/)
: CDialog(mydialog::IDD, pParent)
, m_name(_T(""))
, select_port(_T(""))
{
}
mydialog::~mydialog()
{
}
void mydialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_name);
DDX_CBString(pDX, IDC_COMBO1, select_port);
}
BEGIN_MESSAGE_MAP(mydialog, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &mydialog::OnBnClickedButton1)
END_MESSAGE_MAP()
// mydialog message handlers
void mydialog::OnBnClickedButton1()
{
CEdit* abc=(CEdit*)GetDlgItem(IDC_EDIT1);
LPTSTR xyz123=new TCHAR[50];
CString text;
int k=abc->GetWindowTextA(xyz123,20);
CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
int item = pCombo->GetCurSel();
if(item != CB_ERR)
{
pCombo->GetLBText(item,text);
}
TRACE("%s\n",text);
loop_count++;
HANDLE hSerial;
if(loop_count==1)
{
hSerial = CreateFile(text,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
TRACE("serial port does not exist for writing\n");
//serial port does not exist. Inform user.
}
TRACE("some other error,serial port does not exist for writing\n");
//some other error occurred. Inform user.
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
TRACE("error getting state for writing\n");
//error getting state
}
dcbSerialParams.BaudRate=CBR_19200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams))
{
TRACE("error setting state for writing\n");
//error setting serial port state
}
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts))
{
TRACE("some error occured for writing\n");
//error occureed. Inform user
}
mydialog::hSerial4=hSerial;
}
int n=100;
char szBuff1[100];
int m=0;
while(m<100 && xyz123[m]!='\0')
{
szBuff1[m]=xyz123[m];
m++;
}
szBuff1[m]='\0';
DWORD dwByteswrote = 0;
if(!WriteFile(mydialog::hSerial4, szBuff1, n, &dwByteswrote, NULL))
{
TRACE("error writing \n");
}
TRACE("%d\n",dwByteswrote);
// mydialog Dlg1;
if(loop_count==1)
{
CWinThread *m_pThread;
m_pThread=AfxBeginThread(read_data,(LPVOID)hSerial);
}
}
UINT mydialog::read_data(LPVOID hSerial5)
{
HANDLE hSerial1=(HANDLE)hSerial5;
int n=100;
char szBuff[100];
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial1, szBuff, n, &dwBytesRead, NULL))
{
TRACE("error reading \n");
//error occurred. Report to user.
}
TRACE("%s\n",szBuff);
// TRACE("%d\n",dwBytesRead);
CloseHandle(hSerial1);
return 1;
}
BOOL mydialog::OnInitDialog()
{
// BOOL x;
mydialog dialog2;
HANDLE hSerial3;
CString temp,temp1;
int n=0;
CComboBox * pCombo=(CComboBox *)GetDlgItem(IDC_COMBO1);
pCombo->AddString("SAMPLE1");
pCombo->AddString("SAMPLE2");
for(n=0;n<9;n++)
{
temp1.Format("%d",n);
temp="COM"+temp1;
hSerial3 = CreateFile(temp,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial3==INVALID_HANDLE_VALUE)
{
TRACE("port not available\n");
}
else
{
pCombo->AddString(temp);
CloseHandle(hSerial3);
TRACE("port available\n");
}
}
// x=dialog2.check_open_port();
return CDialog::OnInitDialog();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已在类中声明了静态成员但尚未定义它们,因此您会收到链接器错误。
在类体之外提供定义
You have declared the static members in your class but haven't defined them, so you are getting linker errors.
Provide the definitions outside the class body