嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元微信扫码支付:5 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
通过485通讯控制直流电机实现:正反转控制、速度调节、位置记忆等功能。
	// AQMDBLS_DemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AQMDBLS_Demo.h"
#include "AQMDBLS_DemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CAQMDBLS_DemoDlg *g_pMainDlg =NULL;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CAQMDBLS_DemoDlg dialog
CAQMDBLS_DemoDlg::CAQMDBLS_DemoDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CAQMDBLS_DemoDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CAQMDBLS_DemoDlg)
 m_bAutoRefresh = FALSE;
 m_bit1 = FALSE;
 m_bit2 = FALSE;
 m_bit3 = FALSE;
 m_bit4 = FALSE;
 m_bit5 = FALSE;
 m_bit6 = FALSE;
 m_bit7 = FALSE;
 m_nBaud = 0;
 m_nPort = 0;
 m_nParity = 0;
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_addr = 0;
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 g_pMainDlg = this;
}
void CAQMDBLS_DemoDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CAQMDBLS_DemoDlg)
 DDX_Control(pDX, IDC_TAB_PAGES, m_tabPages);
 DDX_Control(pDX, IDC_COMBO_BAUD, m_combBaud);
 DDX_Check(pDX, IDC_CHECK_AUTOREFRESH, m_bAutoRefresh);
 DDX_Check(pDX, IDC_CHECK_BIT1, m_bit1);
 DDX_Check(pDX, IDC_CHECK_BIT2, m_bit2);
 DDX_Check(pDX, IDC_CHECK_BIT3, m_bit3);
 DDX_Check(pDX, IDC_CHECK_BIT4, m_bit4);
 DDX_Check(pDX, IDC_CHECK_BIT5, m_bit5);
 DDX_Check(pDX, IDC_CHECK_BIT6, m_bit6);
 DDX_Check(pDX, IDC_CHECK_BIT7, m_bit7);
 DDX_CBIndex(pDX, IDC_COMBO_BAUD, m_nBaud);
 DDX_CBIndex(pDX, IDC_COMBO_COM, m_nPort);
 DDX_CBIndex(pDX, IDC_COMBO_VERIFY, m_nParity);
 //}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAQMDBLS_DemoDlg, CDialog)
 //{{AFX_MSG_MAP(CAQMDBLS_DemoDlg)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
 ON_BN_CLICKED(IDC_BUTTON_READINFO, OnButtonReadinfo)
 ON_BN_CLICKED(IDC_BUTTON_READOEM, OnButtonReadoem)
 ON_BN_CLICKED(IDC_CHECK_AUTOREFRESH, OnCheckAutorefresh)
 ON_WM_TIMER()
 ON_BN_CLICKED(IDC_CHECK_BIT1, OnCheckBit1)
 ON_BN_CLICKED(IDC_CHECK_BIT2, OnCheckBit2)
 ON_BN_CLICKED(IDC_CHECK_BIT3, OnCheckBit3)
 ON_BN_CLICKED(IDC_CHECK_BIT4, OnCheckBit4)
 ON_BN_CLICKED(IDC_CHECK_BIT5, OnCheckBit5)
 ON_BN_CLICKED(IDC_CHECK_BIT6, OnCheckBit6)
 ON_BN_CLICKED(IDC_CHECK_BIT7, OnCheckBit7)
 ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_PAGES, OnSelchangeTabPages)
 ON_BN_CLICKED(IDC_BUTTON_CLEARPOS, OnButtonClearpos)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAQMDBLS_DemoDlg message handlers
BOOL CAQMDBLS_DemoDlg::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
 AddPage(&m_dlgMotorCtrl, IDD_DIALOG_MOTORCTRL, "电机控制");
 //AddPage(&m_dlgMotorLearn, IDD_DIALOG_MOTORLEARN, "电机学习");
 AddPage(&m_dlgMotorCfg, IDD_DIALOG_MOTORCFG, "电机参数");
 AddPage(&m_dlgSysCfg, IDD_DIALOG_SYSCFG, "系统参数");
 AddPage(&m_dlgPidCfg, IDD_DIALOG_PIDPARAM, "PID参数");
 AddPage(&m_dlgTravel, IDD_DIALOG_TRAVELCFG, "往复运动");
 //AddPage(&m_dlgCorrect, IDD_DIALOG_CORRECT, "模拟量修正");
// AddPage(&m_dlgUserProg, IDD_DIALOG_PROGRAM, "自定义过程");
 SelPage(0);
 SetTimer(1, 100, NULL);
 return TRUE;  // return TRUE  unless you set the focus to a control
}
void CAQMDBLS_DemoDlg::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 CAQMDBLS_DemoDlg::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 CAQMDBLS_DemoDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}
void CAQMDBLS_DemoDlg::OnOK() 
{
 // TODO: Add extra validation here
 //CDialog::OnOK();
}
void CAQMDBLS_DemoDlg::OnButtonOpen() 
{
 // TODO: Add your control notification handler code here
 CString strValue;
 UpdateData(TRUE);
 UpdateAddr();
// sprintf(szBuf, "%d,%d,8,%d", nBaudTbl[m_nBaud], nVerify[m_nVerify][0], nVerify[m_nVerify][1]);
 if (m_Motor.IsPortOpened()) {
 m_Motor.ClosePort();
 GetDlgItem(IDC_BUTTON_OPEN)->SetWindowText("打开串口");
 } else {
 int nSel = m_combBaud.GetCurSel();
 if (nSel < 0) nSel = 0;
 m_combBaud.GetLBText(m_nBaud, strValue);
 if (m_Motor.OpenPort(m_nPort   1, atoi(strValue), m_nParity)) {
 GetDlgItem(IDC_BUTTON_OPEN)->SetWindowText("关闭串口");
 //EnableButtons(FALSE);
// EnableButtons(TRUE);
 } else {
 MessageBox("打开串口失败");
 }
 } 
}
void CAQMDBLS_DemoDlg::OnButtonReadinfo() 
{
 // TODO: Add your control notification handler code here
 DEVINFO info = {0};
 CString strValue;
 
 UpdateData(TRUE);
 //UpdateAddr();
 //m_addr = (m_bit1 | (m_bit2 << 1) | (m_bit3 << 2) | (m_bit4 << 3) | (m_bit5 << 4) | (m_bit6 << 5) | (m_bit7 << 6))   1;
 MDRESULT result = m_Motor.QueryDevInfo(m_addr, info);
 if (result == MDERR_SUCCESS) {
 strValue.Format("0x%04X", info.wID);
 GetDlgItem(IDC_STATIC_DEVID)->SetWindowText(strValue);
 strValue.Format("%d.%02d", info.wVersion >> 8, info.wVersion & 0xff);
 GetDlgItem(IDC_STATIC_VERSION)->SetWindowText(strValue);
 info.szDevName[sizeof(info.szDevName) - 1] = '\0';
 strValue.Format("%s", info.szDevName);
 GetDlgItem(IDC_STATIC_DEVNAME)->SetWindowText(strValue);
// if (info.wID == 0x0103) { // 是AQMD2410NS、AQMD3620NS
 strValue.Format("1/%d", info.wPWMMaxSet);
 GetDlgItem(IDC_STATIC_PWMRS)->SetWindowText(strValue);
 strValue.Format("%.1fkHz", info.wPWMFreq / 1000.0);
 GetDlgItem(IDC_STATIC_PWMFREQ)->SetWindowText(strValue);
 strValue.Format("%.1fA", info.wIMax / 100.0);
 GetDlgItem(IDC_STATIC_AMPMAX)->SetWindowText(strValue);
 strValue.Format("%dmA", info.wIrs);
 GetDlgItem(IDC_STATIC_AMPRS)->SetWindowText(strValue);
// } else {
// strValue = "-";
// GetDlgItem(IDC_STATIC_PWMRS)->SetWindowText(strValue);
// GetDlgItem(IDC_STATIC_PWMFREQ)->SetWindowText(strValue);
// GetDlgItem(IDC_STATIC_AMPMAX)->SetWindowText(strValue);
// GetDlgItem(IDC_STATIC_AMPRS)->SetWindowText(strValue);
// }
 } else {
 MessageBox(m_Motor.GetErrString(result));
 } 
}
void CAQMDBLS_DemoDlg::UpdateAddr()
{
 UpdateData(TRUE);
 m_addr = (m_bit1 | (m_bit2 << 1) | (m_bit3 << 2) | (m_bit4 << 3) | (m_bit5 << 4) | (m_bit6 << 5) | (m_bit7 << 6))   1;
}
void CAQMDBLS_DemoDlg::OnButtonReadoem() 
{
 // TODO: Add your control notification handler code here
 CString strOEMInfo;
 MDRESULT result = m_Motor.ReadOEMDevId(m_addr, strOEMInfo);
 if (result == MDERR_SUCCESS) {
 MessageBox(strOEMInfo);
 } else {
 MessageBox(m_Motor.GetErrString(result));
 } 
}
void CAQMDBLS_DemoDlg::OnCheckAutorefresh() 
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);
}
void CAQMDBLS_DemoDlg::OnTimer(UINT nIDEvent) 
{
 // TODO: Add your message handler code here and/or call default
 const BYTE *pBuf;
 DWORD dwLen = 0;
 DWORD i;
 CString strTmp, strText;
// int nValue;
 // 显示发送数据
 pBuf = m_Motor.GetSendBuf(&dwLen);
 for (i = 0; i < dwLen; i  ) {
 strTmp.Format("%02X ", pBuf[i]);
 strText  = strTmp;
 }
 GetDlgItem(IDC_EDIT_SEND)->GetWindowText(strTmp);
 if (strTmp != strText) {
 GetDlgItem(IDC_EDIT_SEND)->SetWindowText(strText);
 }
 // 显示接收数据
 strText = "";
 pBuf = m_Motor.GetRecvBuf(&dwLen);
 for (i = 0; i < dwLen; i  ) {
 strTmp.Format("%02X ", pBuf[i]);
 strText  = strTmp;
 }
 GetDlgItem(IDC_EDIT_RECV)->GetWindowText(strTmp);
 if (strTmp != strText) {
 GetDlgItem(IDC_EDIT_RECV)->SetWindowText(strText);
 }
 if (m_bAutoRefresh && m_Motor.IsPortOpened()) {
 RTSTATUS status = {0};
 MDRESULT result = m_Motor.ReadRTStatus(m_addr, status);
 if (result == MDERR_SUCCESS) {
 m_bSpdMulti = status.bSpdMulti;
 strText.Format("%.1f%%", status.nPWM * 0.1);
 GetDlgItem(IDC_STATIC_RTPWM)->SetWindowText(strText);
 strText.Format("%.2fA", status.nAmp / 100.0);
 GetDlgItem(IDC_STATIC_RTAMP)->SetWindowText(strText);
 strText.Format("%ld", (long)((status.nPhasePosHi << 16) | status.nPhasePosLo));
 GetDlgItem(IDC_STATIC_PHASEPOS)->SetWindowText(strText);
 if (status.bSpdDiv10 || status.bSpdMulti) {
 strText.Format("%dHz", status.nPhaseFreq);
 } else {
 strText.Format("%.1fHz", status.nPhaseFreq * 0.1);
 }
 
 GetDlgItem(IDC_STATIC_PHASEFREQ)->SetWindowText(strText);
 
 strText.Format("%.3fV", status.nIN1Voltage * 0.001);
 GetDlgItem(IDC_STATIC_VAIN1)->SetWindowText(strText);
 strText.Format("%.3fV", status.nIN2Voltage * 0.001);
 GetDlgItem(IDC_STATIC_VAIN2)->SetWindowText(strText);
 strText.Format("%.3fV", status.nIN3Voltage * 0.001);
 GetDlgItem(IDC_STATIC_VAIN3)->SetWindowText(strText);
 strText.Format("%.3fV", status.nDiffVoltage * 0.001);
 GetDlgItem(IDC_STATIC_VAIND)->SetWindowText(strText);
 strText = status.bSQ1Level ? "高" : "低";
 GetDlgItem(IDC_STATIC_SQ1)->SetWindowText(strText);
 strText = status.bSQ2Level ? "高" : "低";
 GetDlgItem(IDC_STATIC_SQ2)->SetWindowText(strText);
 strText.Format("%.1f%%", status.nInPWM * 0.1);
 GetDlgItem(IDC_STATIC_INPWM)->SetWindowText(strText);
 strText.Format("%dHz", status.nInFreq);
 GetDlgItem(IDC_STATIC_INFREQ)->SetWindowText(strText);
 strText.Format("%ld", (long)((status.nInPulseHi << 16) | status.nInPulseLo));
 GetDlgItem(IDC_STATIC_INPULSE)->SetWindowText(strText);
 strText.Format("%.1fs", (DWORD)((status.nLeftTimeHi << 16) | status.nLeftTimeLo) * 0.001);
 GetDlgItem(IDC_STATIC_LEFTTIME)->SetWindowText(strText);
 strText = status.nFinishSta ? "是" : "否";
 GetDlgItem(IDC_STATIC_FINISHSTA)->SetWindowText(strText);
 /*switch (status.nLockSta) {
 case 1:
 strText = "正转堵转"; 
 break;
 case 2:
 strText = "反转堵转"; 
 break;
 default:
 strText = "未堵转";
 break;
 }
 GetDlgItem(IDC_STATIC_LOCKSTA)->SetWindowText(strText);*/
 if (status.nMotorSpd != 0xffff) {
 strText.Format("%dRPM", status.bSpdDiv10 ? (DWORD)status.nMotorSpd * 10 : status.nMotorSpd);
 } else {
 strText = "-"; 
 }
 GetDlgItem(IDC_STATIC_MOTORSPD)->SetWindowText(strText); 
 switch (status.nErrorSta) {
 case 0:
 strText = "无"; 
 break;
 case 1:
 strText = "未学习"; 
 break;
 case 2:
 strText = "堵转停止"; 
 break;
 case 3:
 strText = "霍尔错误";
 break;
 case 4:
 strText = "达不到目标速度";
 break;
 case 5:
 strText = "线圈错误";
 break;
 case 6:
 strText = "过流关断";
 break;
 default:
 strText = "未知错误";
 break;
 }
 GetDlgItem(IDC_STATIC_ERRORSTA)->SetWindowText(strText);
 } else {
 strText = m_Motor.GetErrString(result);
 GetDlgItem(IDC_STATIC_RTPWM)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_RTAMP)->SetWindowText(strText);
 
 GetDlgItem(IDC_STATIC_VAIN1)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_VAIN2)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_VAIND)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_SQ1)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_SQ2)->SetWindowText(strText);
 /// GetDlgItem(IDC_STATIC_DE)->SetWindowText(strText);
 GetDlgItem(IDC_STATIC_PHASEFREQ)->SetWindowText(strText);
 // GetDlgItem(IDC_STATIC_LIMITSTA)->SetWindowText(strText);
 }
 }
// RefreshSldValue();
 CDialog::OnTimer(nIDEvent);
}
void CAQMDBLS_DemoDlg::OnCheckBit1() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit2() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit3() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit4() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit5() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit6() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::OnCheckBit7() 
{
 // TODO: Add your control notification handler code here
 UpdateAddr(); 
}
void CAQMDBLS_DemoDlg::AddPage(CDialog *pDialog, int nIDD, char *szTitle)
{
 PAGEINFO info;
/* m_tabPages.InsertItem(0, "电机学习");
 m_tabPages.InsertItem(1, "电机控制");
    m_tabPages.InsertItem(2, "系统参数");
 m_tabPages.InsertItem(3, "速度参数");
 m_tabPages.InsertItem(4, "其它操作");
 m_tabPages.InsertItem(5, "用户编程");*/
 RECT rc;
 m_tabPages.GetClientRect(&rc);
 rc.top  = 20;
 rc.bottom -= 20;
 rc.right -= 2;
 rc.left  = 2;
 if (pDialog && szTitle) {
 if (pDialog->Create(nIDD, &m_tabPages)) {
 info.bSel = FALSE;
 info.pDialog = pDialog;
 pDialog->MoveWindow(&rc);
 m_tabPages.InsertItem(m_vecPages.size(), szTitle);
 m_vecPages.insert(m_vecPages.end(), info);
 }
 }
}
void CAQMDBLS_DemoDlg::SelPage(int nID)
{
 int i, n;
 n = m_vecPages.size();
 for (i = 0; i < n; i  ) {
 m_vecPages[i].pDialog->ShowWindow(i == nID ? SW_SHOW : SW_HIDE);
 }
 
}
void CAQMDBLS_DemoDlg::OnSelchangeTabPages(NMHDR* pNMHDR, LRESULT* pResult) 
{
 // TODO: Add your control notification handler code here
 SelPage(m_tabPages.GetCurSel());
 *pResult = 0;
}
//DEL void CAQMDBLS_DemoDlg::OnStaticPhasepos() 
//DEL {
//DEL  // TODO: Add your control notification handler code here
//DEL  int nID = MessageBox("真的要清零吗?", "询问", MB_ICONQUESTION | MB_YESNO);
//DEL 
//DEL }
void CAQMDBLS_DemoDlg::OnButtonClearpos() 
{
 // TODO: Add your control notification handler code here
 int nID = MessageBox("真的要清零吗?", "询问", MB_ICONQUESTION | MB_YESNO);
 if (nID == IDYES) {
 m_Motor.WriteSingleReg(m_addr, 0x700a, 1);
 }
}