类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();
}
答案 0 :(得分:1)
您已在类中声明了静态成员但尚未定义它们,因此您将收到链接器错误。
提供类主体外的定义
HANDLE mydialog::hSerial;
int mydialog::loop_count;