📄 serialappdlg1.cpp
字号:
// SerialAppDlg1.cpp : implementation file
//
#include "stdafx.h"
#include "SerialApp.h"
#include "SerialAppDlg1.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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialAppDlg dialog
CSerialAppDlg::CSerialAppDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialAppDlg::IDD, pParent)
{
pfbuf = new unsigned char [100]; //算出的数据放入缓存
pfbuf1 = new unsigned char[100];
// pfbuf2 = new unsigned char[10];
//{{AFX_DATA_INIT(CSerialAppDlg)
m_x = 0;
m_Num = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strReceived.Empty();
m_nComm=0;
m_nBandRate = 9600;
m_nDataBits=8;
m_nStopBits=1;
m_cParity='N';
m_nNum = 0;
} //初始化各成员变量
CSerialAppDlg::~CSerialAppDlg()
{
delete [] pfbuf;
delete [] pfbuf1;
}
void CSerialAppDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialAppDlg)
DDX_Text(pDX, IDC_x, m_x);
DDV_MinMaxFloat(pDX, m_x, 0.f, 94.f);
DDX_Text(pDX, IDC_STATIC1, m_Num);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialAppDlg, CDialog)
//{{AFX_MSG_MAP(CSerialAppDlg)
ON_COMMAND(ID_CONNECT, OnConnect)
ON_COMMAND(ID_DISCONNECT, OnDisconnect)
ON_COMMAND(ID_CONFIG, OnConfig)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
ON_BN_CLICKED(IDC_NEXT, OnNext)
ON_BN_CLICKED(IDC_zero, Onzero)
ON_BN_CLICKED(IDC_motorback, Onmotorback)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialAppDlg message handlers
BOOL CSerialAppDlg::OnInitDialog()
{
CDialog::OnInitDialog; //xxxxxxxxxx
// 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
CenterWindow();
//创建工具条
if(!m_wndToolBar.Create(this,
WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_HIDE_INPLACE|CBRS_SIZE_DYNAMIC)
||!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create the tool bar!\n");
return -1;
}
//设置工具条显示风格
m_wndToolBar.GetToolBarCtrl().ModifyStyle(0,TBSTYLE_FLAT);
m_wndToolBar.MoveWindow(6,4,385,30);
DWORD dwExStyle = TBSTYLE_EX_DRAWDDARROWS;
m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)dwExStyle);
DWORD dwStyle = m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_CONNECT));
dwStyle |= TBSTYLE_DROPDOWN;
m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_CONNECT), dwStyle);
// init the port
if (!m_Port.InitPort(this, 1, 9600 ))
{
AfxMessageBox("Can't init COM1");
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialAppDlg::OnConnect()
{
if(m_Port.m_bThreadAlive) return;
if (m_Port.InitPort(this, m_nComm + 1, m_nBandRate, m_cParity, m_nDataBits, m_nStopBits ))
{
m_Port.StartMonitoring(); //启动CommThread线程
}
else
{
// port not found
AfxMessageBox("该COM口不可用,请改用其它COM口");
}
}
void CSerialAppDlg::OnDisconnect()
{
m_Port.StopMonitoring();
}
void CSerialAppDlg::OnConfig() //消息响应函数,响应ID_CONFIG按钮单击事件
{
CConfigDlg* dlg = new CConfigDlg(this, &m_Port);
dlg->m_strSendBuffer.Format("%d", m_Port.GetWriteBufferSize());
DWORD dwCommEvents = m_Port.GetCommEvents();
if (dlg->DoModal() == IDOK)
{
/* switch(dlg->m_strComm[3])
{
case '1':
m_nComm=0;
break;
case '2':
m_nComm=1;
break;
case '3':
m_nComm=2;
break;
case '4':
m_nComm=3;
break;
default:
break;
}
m_Port.m_nPortNr=m_nComm+1;
*/
m_Port.m_nPortNr = dlg->m_nComPort;
this->m_nBandRate = m_Port.m_dcb.BaudRate = atoi(dlg->m_strBaudRate); //将字符串变为整数
this->m_cParity = dlg->m_strParity[0];
switch(m_cParity)
{
case 'N':
m_Port.m_dcb.Parity=NOPARITY;
break;
case 'O':
m_Port.m_dcb.Parity=ODDPARITY;
break;
case 'E':
m_Port.m_dcb.Parity=EVENPARITY;
break;
case 'M':
m_Port.m_dcb.Parity=MARKPARITY;
break;
case 'S':
m_Port.m_dcb.Parity=SPACEPARITY;
break;
default:
break;
}
this->m_nDataBits = m_Port.m_dcb.ByteSize= atoi(dlg->m_strDataBits);
this->m_nStopBits = m_Port.m_dcb.StopBits= atoi(dlg->m_strStopBits);
this->m_dwCommEvents = dwCommEvents;
this->m_nSendBuffer = m_Port.m_nWriteBufferSize = atoi(dlg->m_strSendBuffer);
if(m_Port.m_szWriteBuffer)
delete m_Port.m_szWriteBuffer;
m_Port.m_szWriteBuffer=new BYTE[m_Port.m_nWriteBufferSize];
if(!(m_Port.InitPort(this,m_nComm+1,m_nBandRate,m_cParity,m_nDataBits,m_nStopBits)))
{
char error[100];
sprintf(error,"COM%d被占用或没有该COM口,请改用其它COM口!",m_nComm+1);
AfxMessageBox(error);
}
}
delete dlg;
}
void CSerialAppDlg::Calculate()
{
int a,b,DIR,nf;
int count=0;
float n,x1; //滑板的位置
float dx;
for(int l = 0; l < 100; l++)
{
pfbuf[l] = 0;
}
x1 = m_x/(30*57); //度->米
dx = x1 - g_x0;
if(dx > 0)
{
DIR = 0; // 向前走~~
}
else if(dx < 0)
{
DIR = 1; //返回~~~
}
n = dx*100; //cm 0~5.5cm
n = fabs(n);
nf = n*100+0.5; //mm*10 0~55mm
a = nf/100;
b = nf%100;
g_x0 = x1; // g_x0 电机运动到上一次的位置
//////////////////结果处理////////////////////////////
pfbuf[0] = 0x33;
pfbuf[1] = 0x34;
pfbuf[2] = 0x00; //地址
//电机运动参数
pfbuf[3] = a; //步数n
pfbuf[4] = b;
pfbuf[5] = DIR; //方向
pfbuf[6] = 0x09;
}
//DEL void CSerialAppDlg::Calculate1()
//DEL {
//DEL
//DEL }
int CSerialAppDlg::WriteCommBlock(unsigned char *pBlock, int nLength)
{
int count=0;
m_Port.m_nWriteBufferSize=nPackSize+2;
if(m_Port.m_szWriteBuffer)
delete m_Port.m_szWriteBuffer;
m_Port.m_szWriteBuffer=new BYTE[m_Port.m_nWriteBufferSize];
while(nLength > m_Port.m_nWriteBufferSize)
{
m_Port.WriteToPort(pBlock+count,m_Port.m_nWriteBufferSize);
nLength-=m_Port.m_nWriteBufferSize;
count+=m_Port.m_nWriteBufferSize;
// Sleep(10);
}
m_Port.WriteToPort(pBlock+count,nLength);
return 0;
}
UINT CSerialAppDlg::SendThread(LPVOID pParam)
{
DWORD start=::GetTickCount();
CSerialAppDlg* pDlg=(CSerialAppDlg*)pParam;
// CWnd* pWnd=pDlg->GetDlgItem(IDC_MSG);
// pWnd->EnableWindow(FALSE);
BOOL bSuccess=pDlg->SendFile();
if(bSuccess)
{
AfxMessageBox("Send Data Complete");
}
else
AfxMessageBox("Send File Fail");
pDlg->m_ProgressCtrl.SetPos(0);
// pWnd->EnableWindow(TRUE);
// pDlg->m_btnSend.EnableWindow(TRUE);
return 0;
}
BOOL CSerialAppDlg::SendFile()
{
unsigned char *pfbuftemp = pfbuf;
BOOL bTimeout=FALSE;
// DWORD start;
// BYTE Inbuf[1000];
BOOL bRet = TRUE;
WORD total = 0;
WORD RecordNum = 0;
// m_ProgressCtrl.SetRange(0, 100);
WriteCommBlock(pfbuftemp,7);
return 1;
}
void CSerialAppDlg::OnCheck1()
{
if(((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck())
{
UpdateData(TRUE);
//((CButton*)GetDlgItem(IDC_CHECK1))->SetWindowText("接收");
Calculate();
SendFile(); //通信流程
// m_Port.RestartMonitoring();
// CWinThread* sendThread=AfxBeginThread(CSerialAppDlg::SendThread,this,THREAD_PRIORITY_HIGHEST);
}
}
void CSerialAppDlg::OnNext() //启动 ,计数???
{
WriteCommBlock((unsigned char *)COMMAND,3);
++m_nNum;
m_Num.Format("%d", m_nNum);
UpdateData(FALSE);
}
void CSerialAppDlg::Onzero()
{
// TODO: Add your control notification handler code here
g_x0 = 0;
m_nNum = 0;
m_Num.Format("%d", m_nNum);
UpdateData(FALSE);
}
void CSerialAppDlg::Onmotorback() // 直接返回or单步返回+启动键
{
// TODO: Add your control notification handler code here
float n0;
int a0,b0,nf0;
unsigned char *pfbuftemp1;
for(int l = 0; l < 100; l++)
{
pfbuf1[l] = 0;
}
n0 = g_x0*100; //cm 通过记住上一次的位置返回
nf0 = n0*100+0.5; //mm
a0 = nf0/100;
b0 = nf0%100;
/*--------------------------------
if(DIR)
{DIR0 = 0;}
else
{DIR0 = 1;
}
------------------------------------*/
pfbuf1[0] = 0x33;
pfbuf1[1] = 0x34;
pfbuf1[2] = 0x00; //地址
//电机运动参数
pfbuf1[3] = a0; //步数n
pfbuf1[4] = b0;
pfbuf1[5] = 1; //反转回去
pfbuf1[6] = 0x09;
pfbuftemp1 = pfbuf1;
WriteCommBlock(pfbuftemp1,7);
}
//DEL void CSerialAppDlg::Onreturn() //单步返回+启动键
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL int nf1;
//DEL unsigned char *pfbuftemp2;
//DEL
//DEL for(int l = 0; l < 100; l++)
//DEL {
//DEL pfbuf2[l] = 0;
//DEL
//DEL }
//DEL nf1 = g_x0 * 1000 +0.5; //mm
//DEL
//DEL pfbuf2[0] = 0x33;
//DEL pfbuf2[1] = 0x34;
//DEL pfbuf2[2] = 0x00; //地址
//DEL //电机运动参数
//DEL pfbuf2[3] = nf1; //步数n
//DEL pfbuf2[4] = 1; //方向
//DEL pfbuf2[5] = 0x07;
//DEL
//DEL pfbuftemp2 = pfbuf2;
//DEL WriteCommBlock(pfbuftemp2,6);
//DEL
//DEL }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -