📄 voltagemeterdlg.cpp
字号:
// VoltageMeterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "VoltageMeter.h"
#include "VoltageMeterDlg.h"
#include "Splash.h"
#define CLOSE 0
#define OPEN 1
#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()
/////////////////////////////////////////////////////////////////////////////
// CVoltageMeterDlg dialog
CVoltageMeterDlg::CVoltageMeterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVoltageMeterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVoltageMeterDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVoltageMeterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVoltageMeterDlg)
DDX_Control(pDX, IDC_MSCOMM1, m_CtrlComm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVoltageMeterDlg, CDialog)
//{{AFX_MSG_MAP(CVoltageMeterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_BN_CLICKED(IDC_OPEN, OnOpenPort)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVoltageMeterDlg message handlers
BOOL CVoltageMeterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
//设置字体
m_font.CreatePointFont(230,"黑体");
CEdit *pEdit1 = (CEdit*)GetDlgItem(IDC_EDIT_RX1);
CEdit *pEdit2 = (CEdit*)GetDlgItem(IDC_EDIT_RX2);
pEdit1->SetFont(&m_font);
pEdit2->SetFont(&m_font);
// 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
// SetDlgItemText(IDC_EDIT_RX1,"0.00 V");
// SetDlgItemText(IDC_EDIT_RX2,"0.00 V");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->AddString("COM1");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->AddString("COM2");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->AddString("COM3");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->AddString("COM4");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->SetCurSel(0);
PortState = CLOSE;
m_timeout = TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CVoltageMeterDlg::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 CVoltageMeterDlg::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 CVoltageMeterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int CVoltageMeterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CSplashWnd::ShowSplashScreen(this);
return 0;
}
void CVoltageMeterDlg::OnOpenPort()
{
// TODO: Add your control notification handler code here
if(PortState == CLOSE) //端口未打开 则打开端口
{
int index;
index = ((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->GetCurSel(); //获取串口号索引值
if(m_CtrlComm.GetPortOpen())
{
m_CtrlComm.SetPortOpen(FALSE);
}
m_CtrlComm.SetCommPort(index+1); //设置端口号
m_CtrlComm.SetInBufferSize(32); //设置收发缓冲区大小
m_CtrlComm.SetOutBufferSize(16);
if(!m_CtrlComm.GetPortOpen())
{
m_CtrlComm.SetPortOpen(TRUE); //成功打开串口
m_CtrlComm.SetInputMode(1);
m_CtrlComm.SetSettings("4800,n,8,1"); //通信格式
m_CtrlComm.SetRThreshold(1); //缓冲区中有多于或等于1个字符引发事件
m_CtrlComm.SetInputLen(0); //设置当前接收区数据长度为0
m_CtrlComm.GetInput(); //先预读缓冲区以清除残留数据
SetDlgItemText(IDC_OPEN,"关闭端口");
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->EnableWindow(FALSE); //组合框disabled
SetTimer(1,150,NULL); //发送周期150毫秒
PortState = OPEN;
m_timeout = TRUE;
}
else
{
AfxMessageBox("无法打开该通信端口");
}
}
else
{
if(m_CtrlComm.GetPortOpen())
{
m_CtrlComm.SetPortOpen(FALSE);
}
KillTimer(1); //取消定时
KillTimer(3);
// m_timeout = FALSE;
((CComboBox*)GetDlgItem(IDC_COMBOX_COMSEL))->EnableWindow(TRUE);
SetDlgItemText(IDC_OPEN,"打开端口");
SetDlgItemText(IDC_EDIT_RX1," ");
SetDlgItemText(IDC_EDIT_RX2," ");
PortState = CLOSE;
}
}
void CVoltageMeterDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent == 1)
{
m_CtrlComm.SetOutput(COleVariant("A")); //发送数据
if(m_timeout == TRUE)
{
SetTimer(3,500,NULL); //设置超时定时器
m_timeout = FALSE;
}
}
if(nIDEvent == 3) //连接超时
{
SetDlgItemText(IDC_EDIT_RX1,"----");
SetDlgItemText(IDC_EDIT_RX2,"----");
m_timeout = TRUE;
KillTimer(3);
}
CDialog::OnTimer(nIDEvent);
}
BEGIN_EVENTSINK_MAP(CVoltageMeterDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CVoltageMeterDlg)
ON_EVENT(CVoltageMeterDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CVoltageMeterDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[64];
CString tempdata;
if(m_CtrlComm.GetCommEvent() == 2) //接收到字符
{
variant_inp = m_CtrlComm.GetInput(); //读取缓冲
safearray_inp = variant_inp; //VARIANT类型转换成COLeSafeArray型
len = safearray_inp.GetOneDimSize();
for(k=0;k<len;k++)
{
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
}
if(rxdata[0] == 1) //通道一数据
{
tempdata = rxdata[1]+0x30;
tempdata += '.';
tempdata += rxdata[2]+0x30;
tempdata += rxdata[3]+0x30;
tempdata += ' ';
tempdata += 'V';
SetDlgItemText(IDC_EDIT_RX1,tempdata);
tempdata =' ';
SetTimer(3,500,NULL); //复位超链接超时定时器
// m_timeout = FALSE;
}
if(rxdata[4] == 2) //通道二数据
{
tempdata = rxdata[5]+0x30;
tempdata += '.';
tempdata += rxdata[6]+0x30;
tempdata += rxdata[7]+0x30;
tempdata += ' ';
tempdata += 'V';
SetDlgItemText(IDC_EDIT_RX2,tempdata);
tempdata =' ';
SetTimer(3,500,NULL);
// m_timeout = FALSE;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -