📄 motordlg.cpp
字号:
// MotorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Motor.h"
#include "MotorDlg.h"
#include "const.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMotorDlg dialog
CMotorDlg::CMotorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMotorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMotorDlg)
m_StepsA = 0;
m_StepsB = 0;
m_StepsC = 0;
m_Steps = 0;
//}}AFX_DATA_INIT
SetupComPort();
}
void CMotorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMotorDlg)
DDX_Control(pDX, IDC_COMBO_MOTORSEL, m_MotorSel);
DDX_Text(pDX, IDC_EDIT_STEPA, m_StepsA);
DDX_Text(pDX, IDC_EDIT_STEPB, m_StepsB);
DDX_Text(pDX, IDC_EDIT_STEPC, m_StepsC);
DDX_Text(pDX, IDC_EDIT_STEPS, m_Steps);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMotorDlg, CDialog)
//{{AFX_MSG_MAP(CMotorDlg)
ON_WM_CLOSE()
ON_CBN_SELCHANGE(IDC_COMBO_MOTORSEL, OnSelchangeComboMotorsel)
ON_BN_CLICKED(IDC_BUTTON_MOVE, OnButtonMove)
ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMotorDlg message handlers
void CMotorDlg::SetupComPort()
{
/*
hCom=CreateFile("COM4", //串口号设置
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(hCom==(HANDLE)-1)
{
AfxMessageBox("打开COM失败!");
}
else
AfxMessageBox("已打开串口 9600!");
*/
CString strComNum[7]={"COM2","COM3","COM4","COM5","COM6","COM7","COM8"};
CString strTemp="已成功打开";
int i=1;
do{
hCom=CreateFile(strComNum[i], //串口号设置
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
i++;
if(i==7)
{
AfxMessageBox("打开COM失败,请检查串口线是否插上!");
return;
}
}
while(hCom==(HANDLE)-1);
if(i<7)
{
strTemp += strComNum[i-1];
AfxMessageBox(strTemp);
}
SetupComm(hCom,100,100);
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=(DWORD)9600; //波特率设置
dcb.ByteSize=8;
dcb.StopBits=ONESTOPBIT;
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
}
void CMotorDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
CloseHandle(hCom);
CDialog::OnClose();
}
void CMotorDlg::WriteComPort()
{
OVERLAPPED m_osWrite;
memset(&m_osWrite,0,sizeof(OVERLAPPED));
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
DWORD dwBytesWrite=FRAM_LENGTH; //要发送的数据的长度
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
bWriteStat=WriteFile(hCom,m_SendBuf,dwBytesWrite,&dwBytesWrite,&m_osWrite);
if(!bWriteStat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(m_osWrite.hEvent,1000);
}
}
}
void CMotorDlg::OnSelchangeComboMotorsel()
{
// TODO: Add your control notification handler code here
unsigned int uID = m_MotorSel.GetCurSel();
switch(uID) //根据选择来确定操作的电机
{
case 0 : m_motor_num = MOTORA;
break;
case 1 : m_motor_num = MOTORB;
break;
case 2 : m_motor_num = MOTORC;
break;
}
}
void CMotorDlg::OnButtonMove()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
unsigned int uID=GetCheckedRadioButton(IDC_RADIO_P,IDC_RADIO_N);
CString Str;
GetDlgItemText(uID,Str);
if("正"==Str)
m_direction = ROTATE_P ;
else if("负"==Str)
m_direction = ROTATE_N ;
m_SendBuf[0] = 'S';
m_SendBuf[1] = MOVE_SLOW;
m_SendBuf[2] = m_motor_num;
m_SendBuf[3] = m_direction;
m_SendBuf[4] = 0;
m_SendBuf[5] = ((unsigned int)m_Steps)/256;
m_SendBuf[6] = ((unsigned int)m_Steps)%256;
m_SendBuf[7] = 'S';
//以下为更新ABC 的转角的显示数字
switch(m_motor_num) //根据选择来确定操作的电机
{
case MOTORA : if(ROTATE_P==m_direction )
m_StepsA += m_Steps;
else if(ROTATE_N==m_direction )
m_StepsA -= m_Steps;
break;
case MOTORB : if(ROTATE_P==m_direction )
m_StepsB += m_Steps;
else if(ROTATE_N==m_direction )
m_StepsB -= m_Steps;
break;
case MOTORC : if(ROTATE_P==m_direction )
m_StepsC += m_Steps;
else if(ROTATE_N==m_direction )
m_StepsC -= m_Steps;
break;
}
UpdateData(FALSE);
WriteComPort();//把m_SendBuf[8]从串口发送出去
}
void CMotorDlg::OnButtonReset()
{
// TODO: Add your control notification handler code here
m_SendBuf[0] = 'S';
m_SendBuf[1] = RESET;
m_SendBuf[7] = 'S';
WriteComPort();//把m_SendBuf[8]从串口发送出去
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -