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();
}
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