?? testdlg.cpp
字號:
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include "Serial.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_HELPINFO()
ON_CBN_SELCHANGE(IDC_PORT, OnSelchangePort)
ON_CBN_SELCHANGE(IDC_BAUD, OnSelchangeBaud)
ON_CBN_SELCHANGE(IDC_DATABITS, OnSelchangeDatabits)
ON_CBN_SELCHANGE(IDC_STOPBITS, OnSelchangeStopbits)
ON_CBN_SELCHANGE(IDC_PARITY, OnSelchangeParity)
ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
ON_BN_CLICKED(IDC_CHKRCVHEX, OnChkrcvhex)
ON_BN_CLICKED(IDC_CHKSNDHEX, OnChksndhex)
ON_BN_CLICKED(IDC_AUTOSEND, OnAutosend)
ON_EN_CHANGE(IDC_EDTFREQENCY, OnChangeEdtfreqency)
ON_EN_KILLFOCUS(IDC_EDTFREQENCY, OnKillfocusEdtfreqency)
ON_BN_CLICKED(IDC_CLSRCV, OnClsrcv)
ON_BN_CLICKED(IDC_CLSSEND, OnClssend)
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_BN_CLICKED(IDC_MANSEND, OnMansend)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
// Init default Value in Main Window
// Also Init struct "DlgAttr",which is used save the changed Value in Main Window
// Port
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_PORT);
pComboBox->SetCurSel (0);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PORT);
// Baud
pComboBox = (CComboBox*)GetDlgItem(IDC_BAUD);
pComboBox->SetCurSel(3);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.BAUD);
// Databits
pComboBox = (CComboBox*)GetDlgItem(IDC_DATABITS);
pComboBox->SetCurSel(2);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.DATABITS);
// Stopbits
pComboBox = (CComboBox*)GetDlgItem(IDC_STOPBITS);
pComboBox->SetCurSel(0);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.STOPBITS);
// Parity
pComboBox = (CComboBox*)GetDlgItem(IDC_PARITY);
pComboBox->SetCurSel(1);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PARITY);
// Show HEX
CButton* pBtn = (CButton*)GetDlgItem(IDC_CHKRCVHEX);
pBtn->SetCheck (0);
DlgAttr.RCVHEX = pBtn->GetCheck ();
// Send Hex
pBtn = (CButton*)GetDlgItem(IDC_CHKSNDHEX);
pBtn->SetCheck (0);
DlgAttr.SENDHEX = pBtn->GetCheck ();
// Auto Send
pBtn = (CButton*)GetDlgItem(IDC_AUTOSEND);
pBtn->SetCheck (0);
DlgAttr.AUTOSEND = pBtn->GetCheck ();
// Auto send frequency
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTFREQUENCY);
pEdt->SetWindowText (_T("1000"));
pEdt->GetWindowText (DlgAttr.FREQUENCY);
// Send bytes count
pEdt = (CEdit*)GetDlgItem(IDC_SNDBYTES);
pEdt->SetWindowText (_T("0"));
// Receive bytes count
pEdt = (CEdit*)GetDlgItem(IDC_RCVBYTES);
pEdt->SetWindowText (_T("0"));
// Set StatusBar text
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATUSBAR);
m_statusbar_str = DlgAttr.PORT+" OPEND! BAUD:" + DlgAttr.BAUD + ",DATABITS:" + DlgAttr.DATABITS + ",STOPBITS:" + DlgAttr.STOPBITS + ",PARITY:" + DlgAttr.PARITY + ".";
pStatic->SetWindowText (m_statusbar_str);
m_com.InitializePort(this->m_hWnd,DlgAttr.PORT ,(UINT)CStringToInt(DlgAttr.BAUD),DlgAttr.PARITY ,(UINT)CStringToInt(DlgAttr.DATABITS),(UINT)CStringToInt(DlgAttr.STOPBITS));
m_com.StartReadThread ();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::OnHelpInfo()
{
// TODO: implement help here
MessageBox(_T("Free Software!\nVersion:1.30\nAuthor:rootgr.wu\nEmail:rootgr@wst.net.cn\nMSN:wutieru@hotmail.com\n"));
}
void CTestDlg::OnSelchangePort()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_PORT);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PORT);
}
void CTestDlg::OnSelchangeBaud()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_BAUD);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.BAUD);
}
void CTestDlg::OnSelchangeDatabits()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_DATABITS);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.DATABITS);
}
void CTestDlg::OnSelchangeStopbits()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_STOPBITS);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.STOPBITS);
}
void CTestDlg::OnSelchangeParity()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_PARITY);
pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PARITY);
}
void CTestDlg::OnBtnopen()
{
// Get Current Button Status
CString btnstr;
CButton* pBtnOpen = (CButton*)GetDlgItem(IDC_BTNOPEN);
pBtnOpen->GetWindowText(btnstr);
m_statusbar_str = DlgAttr.PORT+" OPEND! BAUD:" + DlgAttr.BAUD + ",DATABITS:" + DlgAttr.DATABITS + ",STOPBITS:" + DlgAttr.STOPBITS + ",PARITY:" + DlgAttr.PARITY + ".";
// Set Button Status
if (btnstr=="OPENED")
{
// Set Status Bar Text.
pBtnOpen->SetWindowText(_T("CLOSED"));
m_statusbar_str = DlgAttr.PORT+" CLOSED!";
// Close port Handle
m_com.ClosePort ();
m_com.StopReadThread ();
}
else
{
// Set Status Bar Text.
pBtnOpen->SetWindowText (_T("OPENED"));
m_statusbar_str = DlgAttr.PORT+" OPEND! BAUD:" + DlgAttr.BAUD + ",DATABITS:" + DlgAttr.DATABITS + ",STOPBITS:" + DlgAttr.STOPBITS + ",PARITY:" + DlgAttr.PARITY + ".";
// Open Port
m_com.InitializePort(this->m_hWnd,DlgAttr.PORT ,(UINT)CStringToInt(DlgAttr.BAUD),DlgAttr.PARITY ,(UINT)CStringToInt(DlgAttr.DATABITS),(UINT)CStringToInt(DlgAttr.STOPBITS));
m_com.StartReadThread ();
}
// Get Current Status Bar Handle and Set to window
CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATUSBAR);
pStatic->SetWindowText (m_statusbar_str);
}
void CTestDlg::OnChkrcvhex()
{
CButton* pBtnCHKRCVHEX = (CButton*)GetDlgItem(IDC_CHKRCVHEX);
DlgAttr.RCVHEX = pBtnCHKRCVHEX->GetCheck ();
}
void CTestDlg::OnChksndhex()
{
CButton* pBtnCHKRCVHEX = (CButton*)GetDlgItem(IDC_CHKSNDHEX);
DlgAttr.SENDHEX = pBtnCHKRCVHEX->GetCheck ();
}
void CTestDlg::OnAutosend()
{
CButton* pBtnAUTOSEND = (CButton*)GetDlgItem(IDC_AUTOSEND);
if(pBtnAUTOSEND->GetCheck())
SetTimer(1,(UINT)CStringToInt(DlgAttr.FREQUENCY),NULL);
else
KillTimer(1);
}
void CTestDlg::OnChangeEdtfreqency()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// If the edit value changed, stop auto send and uncheck "Auto Send Data"
// check box first.
CButton* pBtn = (CButton*)GetDlgItem(IDC_AUTOSEND);
pBtn->SetCheck (0);
KillTimer(1);
}
void CTestDlg::OnKillfocusEdtfreqency()
{
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTFREQUENCY);
pEdt->GetWindowText (DlgAttr.FREQUENCY);
}
double CTestDlg::CStringToInt(CString str)
{
char *m_str;
int nSize = 2*str.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, str, nSize+1);
double x = strtod(pAnsiString,&m_str);
delete [] pAnsiString;
return x;
}
void CTestDlg::OnClsrcv()
{
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
pEdt->SetWindowText (_T(""));
}
void CTestDlg::OnClssend()
{
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTSEND);
pEdt->SetWindowText (_T(""));
}
void CTestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
CString tempstr;
CString temp;
char hex_buf[4]; // hex convert buffer
unsigned char ch_buf; // ansi convert buffer
int i; // bytes count in convert buffer
// get current display string for appending.
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
pEdt->GetWindowText (tempstr);
// Ansi data
ch_buf = (unsigned char)ch;
// Hex data
i = sprintf(hex_buf,"%.2X",ch_buf)/2;
// -------------display send bytes count.-----------------
CString rcvcount;
// get current counts in the form.
CEdit* prcvEdt = (CEdit*)GetDlgItem(IDC_RCVBYTES);
prcvEdt->GetWindowText (rcvcount);
// =-----------check if receive hex combox --------------=
if(DlgAttr.RCVHEX == 0) // send ANSI
{
tempstr=tempstr+CString(ch_buf);
temp = UnicodeToAnsi(tempstr);
pEdt->SetWindowText (temp);
}
else // send HEX
{
tempstr=tempstr+hex_buf + " ";
pEdt->SetWindowText (tempstr);
}
// send byte counts to dialog
rcvcount.Format (_T("%d"),(int)CStringToInt(rcvcount)+i);
prcvEdt->SetWindowText(rcvcount);
}
void CTestDlg::OnMansend()
{
CString tempstr;
CString sndstr;
// Get string in IDC_EDTSEND.
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTSEND);
pEdt->GetWindowText(tempstr);
// display bytes count.
pEdt = (CEdit*)GetDlgItem(IDC_SNDBYTES);
pEdt->GetWindowText(sndstr);
// convert unicode to ansi first.
char *buf = UnicodeToAnsi(tempstr);
char *Sendbuf = new char[strlen(buf)];
memset(Sendbuf,0,strlen(buf)/2);
if(DlgAttr.SENDHEX == 0) // Send ANSI data.
{
m_com.WriteChar (buf);
sndstr.Format (_T("%d"),(int)CStringToInt(sndstr)+strlen(buf));
}
else
{
if(IsHex(buf)) // Send HEX data.
{
ToHex(buf,Sendbuf);
m_com.WriteChar (Sendbuf,strlen(buf)/2);
sndstr.Format (_T("%d"),(int)CStringToInt(sndstr)+strlen(buf)/2);
}
}
pEdt->SetWindowText(sndstr);
delete [] Sendbuf;
free(buf);
}
CTestDlg::~CTestDlg()
{
m_statusbar_str = "";
m_com.ClosePort ();
m_com.StopReadThread ();
}
char * CTestDlg::UnicodeToAnsi(LPCTSTR lpString)
{
// Calculate unicode string length.
UINT len = wcslen(lpString)*2;
char *buf = (char *)malloc(len);
UINT i = wcstombs(buf,lpString,len);
return buf;
}
void CTestDlg::OnReset()
{
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_SNDBYTES);
pEdt->SetWindowText (_T("0"));
pEdt = (CEdit*)GetDlgItem(IDC_RCVBYTES);
pEdt->SetWindowText (_T("0"));
}
void CTestDlg::OnSave()
{
FILE *stream;
int writecount;
CString edtrcv;
SYSTEMTIME st;
// get current receive data.
CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
pEdt->GetWindowText (edtrcv);
GetLocalTime(&st);
CString str_time;
str_time.Format (_T("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
edtrcv = str_time + edtrcv;
if((stream = fopen("receive.log","w+t"))!=NULL)
{
writecount = fwrite(UnicodeToAnsi(edtrcv),sizeof(char),strlen(UnicodeToAnsi(edtrcv)),stream);
fclose(stream);
}
else
{
AfxMessageBox(L"Open File FAIL!");
}
free(stream);
}
void CTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnMansend();
CDialog::OnTimer(nIDEvent);
}
void CTestDlg::GetANSI(LPCTSTR lpString,char *charstr)
{
int strlen = wcslen(lpString)*2;
charstr = new char[strlen+1];
wcstombs(charstr, lpString, strlen+1);
}
void CTestDlg::ToHex(char *pSource, char *pDest)
{
int len = strlen(pSource);
char hexchar;
for(int i=0;i<len;i+=2)
{
// 十位數
if( pSource[i] >= 0x30 && pSource[i]<= 0x39)
hexchar = (pSource[i]- 0x30 ) * 16;
else if(pSource[i] >= 0x41 && pSource[i] <= 0x46)
hexchar = (pSource[i] - 0x37) * 16;
else if(pSource[i] >= 0x61 && pSource[i] <= 0x66)
hexchar = (pSource[i] - 0x57) * 16;
// 個位數加十位數
if( pSource[i+1] >= 0x30 && pSource[i+1]<= 0x39)
hexchar = hexchar + (pSource[i+1]- 0x30 );
else if(pSource[i+1] >= 0x41 && pSource[i+1] <= 0x46)
hexchar = hexchar + (pSource[i+1] - 0x37);
else if(pSource[i+1] >= 0x61 && pSource[i+1] <= 0x66)
hexchar = hexchar + (pSource[i+1] - 0x57);
pDest[i/2] = hexchar;
}
}
BOOL CTestDlg::IsHex(char *pString)
{
// must be ansi code here
char HEX[] = "0123456789ABCDEFabcdef"; // support HEX Code
int len = strlen(pString);
char *result;
for (int i=0;i<len;i++)
{
result = strchr(HEX,pString[i]);
if(result==NULL)
{
AfxMessageBox(L"unsupport char in send buffer!");
return FALSE;
}
}
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -