📄 a808mdlg.cpp
字号:
// A808MDlg.cpp : implementation file
//
#include "stdafx.h"
#include "A808M.h"
#include "A808MDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
/*1、首先打开串口,然后开始点击“开始发送”,程序从容不迫SendData()开始,在其中分主数据的通讯:当前温度,
另外:加入有读参数表,有读单个参数,及写参数
2、读参数时,正常情况下,由ProcessArray()进行处理,不断的SetTimer()和KillTimer(),并SendData(),只有不正常时,才会由
OnTimer()来启动SendData()来保持不断的通讯。
3、使用了大量的GetDlgItem()用控件ID来获得控件的一些控制。
4、其中BOOL m_shortProcess是用来在前一数据处理未完成,后面的动作已发生时,来处理这段时间的动作。
后面发生动作时,让其为T,当数据处理完成时,让其为F。short即时间很短的意思
*/
#define Random(nNum) (unsigned int)(((LONG)rand() * (nNum)) / (RAND_MAX + 1))
#define Randomize() srand((unsigned)time(NULL))
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()
/////////////////////////////////////////////////////////////////////////////
// CA808MDlg dialog
CA808MDlg::CA808MDlg(CWnd* pParent /*=NULL*/)
: CDialog(CA808MDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CA808MDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_ctrlHistogram=new CM808[2];
}
void CA808MDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CA808MDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CA808MDlg, CDialog)
//{{AFX_MSG_MAP(CA808MDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN_COM, OnButtonOpenCom)
ON_BN_CLICKED(IDC_BUTTON_START_WORK, OnButtonStartWork)
ON_WM_TIMER()
ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CA808MDlg message handlers
BOOL CA808MDlg::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
//设定串口Com1
CComboBox* pcombo=(CComboBox*)GetDlgItem(IDC_COMBO_COM_PORT);
pcombo->SetCurSel(5);
//设置串口通讯方式
CEdit* pcomSet=(CEdit*)GetDlgItem(IDC_EDIT_COM_SET);
pcomSet->SetWindowText("9600,e,7,1");
CRect rect;
GetDlgItem(IDC_STATIC_M808)->GetWindowRect(rect);
ScreenToClient(rect);
m_ctrlHistogram[0].Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP, rect, this, PARAMETER_ONE);
m_ctrlHistogram[0].SetRange(1, 150);
m_ctrlHistogram[0].SetSpeed(CHistogramCtrl::HIGH_SPEED);
m_ctrlHistogram[0].SetMeterIndex(1);
GetDlgItem(IDC_STATIC_M809)->GetWindowRect(rect);
ScreenToClient(rect);
m_ctrlHistogram[1].Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP, rect, this, PARAMETER_ONE+1);
m_ctrlHistogram[1].SetRange(1, 150);
m_ctrlHistogram[1].SetSpeed(CHistogramCtrl::HIGH_SPEED);
m_ctrlHistogram[1].SetMeterIndex(2);
// SetTimer(0,2000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CA808MDlg::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 CA808MDlg::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 CA808MDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//打开串口按钮
void CA808MDlg::OnButtonOpenCom()
{
int m_nBaud;
int m_nCom1;
char m_cParity;
int m_nDatabits;
int m_nStopbits;
DWORD m_dwCommEvents = EV_RXFLAG | EV_RXCHAR;
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTON_OPEN_COM);
CString strButton;
pButton->GetWindowText(strButton);
CComboBox* pcomport=(CComboBox*)GetDlgItem(IDC_COMBO_COM_PORT);
m_nCom1=pcomport->GetCurSel();
//读串口通讯方式设置
CEdit* pcomSet=(CEdit*)GetDlgItem(IDC_EDIT_COM_SET);
CString str;
pcomSet->GetWindowText(str);
int x=str.Find(',',0);
int len=str.GetLength();
if(len>x+5)
{
m_nBaud=atoi((const char*)str.Mid(0,x));
m_cParity=str.GetAt(x+1);
m_nDatabits=atoi((const char*)str.Mid(x+3,1));
m_nStopbits=atoi((const char*)str.Mid(x+5,1));
}
if(strButton=="打开串口")
{
//if (m_Port.InitPort(this, 1, 9600,'N',8,1,dwCommEvents,512))
if (m_ctrlComm.InitPort(this, m_nCom1, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
{
m_ctrlComm.StartMonitoring();
pButton->SetWindowText("关闭串口");
}
else
{
AfxMessageBox("没有发现此串口");
}
}
else
{
m_ctrlComm.ClosePort(); //关闭串口
pButton->SetWindowText("打开串口");
}
}
BEGIN_EVENTSINK_MAP(CA808MDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CA808MDlg)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
//串口接收到数据
LONG CA808MDlg::OnCommunication(WPARAM ch,LPARAM port)
{
// CString str;
//
// str.Format("%02X ",ch);
//将数组转换为CString
BYTE bt=ch; //字符型
m_pRxdatd.Add(bt);
for(int ii=0;ii<2;ii++)
{
if(m_ctrlHistogram[ii].m_IsSendData)
m_ctrlHistogram[ii].ProcessArray(m_pRxdatd);
}
return 0;
}
//开始工作台按钮
void CA808MDlg::OnButtonStartWork()
{
CButton* pButton=(CButton*)GetDlgItem(IDC_BUTTON_OPEN_COM);
CButton* pButton1=(CButton*)GetDlgItem(IDC_BUTTON_START_WORK);
CString str;
pButton1->GetWindowText(str);
if(str=="开始发送")
{
pButton->EnableWindow(FALSE);
//启动定时器,发送数据
SetTimer(1,100,NULL);
pButton1->SetWindowText("停止发送");
// SendData(); //发送数据
m_ctrlHistogram[0].m_IsSendData=TRUE;
SendData();
count=1;
}
else
{
pButton1->SetWindowText("开始发送");
pButton->EnableWindow(TRUE);
KillTimer(1);
}
}
void CA808MDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// Beep(3000,30); //有error时发出提示音
if(m_ctrlHistogram[count-1].m_IsSendData)
SendData();
else
{
if(count>1)
count=0;
if(count<2)
{
m_ctrlHistogram[count].m_IsSendData=TRUE;
SendData();
count++;
}
}
CDialog::OnTimer(nIDEvent);
}
void CA808MDlg::SendData()
{
CByteArray hexdata;
for(int i=0;i<2;i++)
{
if(m_ctrlHistogram[i].m_IsSendData==TRUE)
{
m_ctrlHistogram[i].SendData(hexdata);
}
}
if(m_ctrlComm.m_hComm==NULL)
{
AfxMessageBox("串口没有打开,请打开串口");
return;
}
else
{
int len=hexdata.GetSize();
char data[127];
for(int i=0;i<len;i++)
data[i]=hexdata.GetAt(i);
m_ctrlComm.WriteToPort(data,len);
//m_Port.WriteToPort(hexdata);
}
}
void CA808MDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
delete [] m_ctrlHistogram;
m_ctrlComm.ClosePort();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -