📄 test3dlg.cpp
字号:
// test3Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test3.h"
#include "test3Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialog
CTest3Dlg::CTest3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest3Dlg)
m_strCommandSend = _T("");
m_CurrentValue = 0;
m_OutputValue = 0;
m_strCOMPort = _T("");
m_bAutoCheck = FALSE;
m_strReturnBuf = _T("");
m_SettingValue = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
cnComm myCom1();
}
void CTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest3Dlg)
DDX_Control(pDX, IDC_CHECK_HEXSEND, m_bHexSend);
DDX_Control(pDX, IDC_CHECK_HEXRECV, m_bHexRecv);
DDX_Control(pDX, IDC_BUTTON_SEND_COMMAND, m_bSendCommand);
DDX_Control(pDX, IDC_BUTTON_QUERY, m_bQuery);
DDX_Text(pDX, IDC_EDIT_COMMAND, m_strCommandSend);
DDX_Text(pDX, IDC_EDIT_CURRENT, m_CurrentValue);
DDX_Text(pDX, IDC_EDIT_OUTPUT, m_OutputValue);
DDX_CBString(pDX, IDC_COMBO_COMPORT, m_strCOMPort);
DDX_Check(pDX, IDC_CHECK_AUTO, m_bAutoCheck);
DDX_Text(pDX, IDC_EDIT_RETURN, m_strReturnBuf);
DDX_Text(pDX, IDC_EDIT_SETTING, m_SettingValue);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest3Dlg, CDialog)
//{{AFX_MSG_MAP(CTest3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
ON_BN_CLICKED(IDC_BUTTON_SEND_COMMAND, OnButtonSendCommand)
ON_BN_CLICKED(IDC_BUTTON_CLEAR_RECV, OnButtonClearRecv)
ON_BN_CLICKED(IDC_BUTTON_CLEAR_SEND, OnButtonClearSend)
ON_BN_CLICKED(IDC_CHECK_AUTO, OnCheckAuto)
ON_WM_TIMER()
//}}AFX_MSG_MAP
//ON_MESSAGE(ON_COM_RECEIVE, On_Receive)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg message handlers
BOOL CTest3Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
m_strCommandSend = "ATI1\r\n";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest3Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTest3Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTest3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
CString StringToHexString(CString cs)
{
CString ret, tmp;
for(int i=0; i<cs.GetLength(); i++)
{
unsigned char c = cs[i];
tmp.Format("%02hX ", c);
ret+=tmp;
}
return ret;
}
CString HexStringToHex(CString cs)
{
CString ret;
char *Input=cs.GetBuffer(cs.GetLength());
char seps[] = " ,\t\n";
char* token = strtok( Input, seps );
int iAscii;
while( token != NULL )
{
sscanf(token ,"%x", &iAscii);
ret += iAscii;
token = strtok( NULL, seps );
}
return ret;
}
void CTest3Dlg::OnButtonQuery()
{
// TODO: Add your control notification handler code here
int len;
char str[1024];
len = myCom1.GetInputSize();
myCom1.ReadString(str,1024);
CString strHEX;
if(len > 0)
{
m_CurrentValue = GetCurrentValue(str);
m_OutputValue = GetOutputValue (str);
m_SettingValue = GetSettingValue(str);
if (m_bHexRecv.GetCheck())
{
m_strReturnBuf = StringToHexString(str);
}
else
{
char com_str[10];
strcpy(com_str, "COM");
ltoa((long)myCom1._dwPort, com_str + 3, 10);
m_strReturnBuf = com_str;
m_strReturnBuf += " : ";
m_strReturnBuf += str;
m_strReturnBuf += "\r\n";
}
}
UpdateData(false);
}
void CTest3Dlg::OnButtonSendCommand()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //读取编辑框内容
try
{
if (!myCom1.Open(3))
MessageBox("Error in opening COM1","ERROR",MB_OK);
else
{
myCom1.SetWnd(m_hWnd);
if (m_bHexSend.GetCheck())
{
m_strCommandSend = HexStringToHex(m_strCommandSend);
//CString str = "\x41\x54\x49\x31\x0D\x0A";
DWORD len = m_strCommandSend.GetLength();
LPVOID p = (LPVOID)m_strCommandSend.GetBuffer(0);
myCom1.Write(p,len);
// myCom1.Write("\x41\x54\x49\x31\x0D\x0A",88);
// myCom1.Write("41 54 49 31 0D 0A",88);
Sleep(500);
}
else
{
myCom1.Write(m_strCommandSend);
Sleep(500);
// myCom1.Close();
}
}
}
catch (exception &e)
{
MessageBox(e.what(),"ERROR",MB_OK);
}
UpdateData(FALSE);
}
void CTest3Dlg::OnButtonClearRecv()
{
// TODO: Add your control notification handler code here
m_strReturnBuf = "";
UpdateData(FALSE);
}
void CTest3Dlg::OnButtonClearSend()
{
// TODO: Add your control notification handler code here
m_strCommandSend = "";
UpdateData(FALSE);
}
void CTest3Dlg::OnCheckAuto()
{
// TODO: Add your control notification handler code here
m_bAutoCheck = !m_bAutoCheck;
if (m_bAutoCheck)
{
SetTimer(1,1000,NULL);//时间为1000毫秒
}
else
{
KillTimer(1);
}
}
void CTest3Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnButtonQuery();
CDialog::OnTimer(nIDEvent);
}
/*
LRESULT CTest3Dlg::On_Receive(WPARAM wp, LPARAM lp)
{
int len;
char str[1024];
len = myCom1.GetInputSize();
myCom1.ReadString(str,1024);
CString strHEX;
if(len > 0)
{
if (m_bHexRecv.GetCheck())
{
m_strReturnBuf = StringToHexString(str);
UpdateData(false);
}
else
{
char com_str[10];
strcpy(com_str, "COM");
ltoa((long)wp, com_str + 3, 10); // WPARAM 保存端口号
m_strReturnBuf += com_str;
m_strReturnBuf += " : ";
m_strReturnBuf += str;
m_strReturnBuf += "\r\n";
UpdateData(false);
}
}
return 0;
}
*/
/*int CSCommTestDlg::String2Hex(CString str, CByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=str.GetLength();
senddata.SetSize(len/2);
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
hexdata=ConvertHexChar(hstr);
lowhexdata=ConvertHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}
senddata.SetSize(hexdatalen);
return hexdatalen;
}
*/
double CTest3Dlg::GetSettingValue(CString cs)
{
double ret;
CString lowHex = cs.Mid(24,2);
CString highHex = cs.Mid(26,2);
char *low = lowHex.GetBuffer(lowHex.GetLength());
char *high = highHex.GetBuffer(highHex.GetLength());
double temp1 = atoi(low);
double temp2 = atoi(high);
// char *Input=cs.GetBuffer(cs.GetLength());
ret = temp2 * 16 + temp1;
return ret;
}
double CTest3Dlg::GetCurrentValue(CString cs)
{
double ret;
CString lowHex = cs.Mid(12,2);
CString highHex = cs.Mid(14,2);
char *low = lowHex.GetBuffer(lowHex.GetLength());
char *high = highHex.GetBuffer(highHex.GetLength());
double temp1 = atoi(low);
double temp2 = atoi(high);
// char *Input=cs.GetBuffer(cs.GetLength());
ret = temp2 * 16 + temp1;
return ret;
}
double CTest3Dlg::GetOutputValue(CString cs)
{
double ret;
return ret;
}
/*
char ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else return (-1);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -