⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cradledlg.cpp

📁 Visual C++串口通信技术与典型事例源代码—云台控制
💻 CPP
字号:
// cradleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "cradle.h"
#include "cradleDlg.h"
#include "Adaptdlg.h"
#include "WordDlg.h"
#include "afx.h"
#include "winio.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//#define ID_STATUSTEXT 2020

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_SEPARATOR,
};

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCoudDlg dialog

CCoudDlg ::CCoudDlg (CWnd* pParent /*=NULL*/)
	: CDialog(CCoudDlg ::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCoudDlg )
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_LIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	for(int i=0; i<32; i++)
	{
		for(int j=0; j<3; j++)
			m_byStatus[i][j]=0;
	}

	m_LChuan=TRUE;
	m_LInOut=956;
}

void CCoudDlg ::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCoudDlg )
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCoudDlg , CDialog)
	//{{AFX_MSG_MAP(CCoudDlg )
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONFIG, OnConfig)
	ON_BN_CLICKED(IDC_CODE, OnCode)
	ON_BN_CLICKED(IDC_BUTTON2, Onguzhang)
	ON_BN_CLICKED(IDC_baojing, Onbaojing)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCoudDlg message handlers

BOOL CCoudDlg::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_LIcon, TRUE);			// Set big icon
	SetIcon(m_LIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	 m_hStatusWnd=CreateStatusWindow(WS_CHILD|WS_VISIBLE|WS_BORDER,TEXT("状态栏"),//显示在状态栏上的信息
									m_hWnd, //父窗口句柄
									IDS_STATUS); //预定义的资源ID
//	 m_nTimer = SetTimer(1, 5000, 0);

	iniFile.Open("cradleconfig.ini",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
	m_pAr=new CArchive(&iniFile,CArchive::load);
	
	if(iniFile.GetLength()!=0)
	{
		UINT BaudRate,PortNr,dataBits,stopBits;
		char Parity;
		(*m_pAr)>>m_LChuan;
		(*m_pAr)>>PortNr;
		(*m_pAr)>>BaudRate>>dataBits>>stopBits>>Parity;
		(*m_pAr)>>m_LInOut;

		if(m_LChuan)
		{
			if(m_Port.InitPort(this,PortNr,BaudRate,Parity,dataBits,stopBits))		
				m_Port.StartMonitoring();
		}
		else
		{
			m_Port.m_nPortNr = PortNr;
			m_Port.m_Ldcb.BaudRate=BaudRate;
			m_Port.m_Ldcb.ByteSize=dataBits;
			m_Port.m_Ldcb.StopBits=stopBits;
			switch(Parity)
			{
			case 'E':
				m_Port.m_Ldcb.Parity=EVENPARITY;
				break;

			case 'M':
				m_Port.m_Ldcb.Parity=MARKPARITY;
				break;
		
			case 'N':
				m_Port.m_Ldcb.Parity=NOPARITY;
				break;

			case 'O':
				m_Port.m_Ldcb.Parity=ODDPARITY;
				break;

			case 'S':
				m_Port.m_Ldcb.Parity=SPACEPARITY;
				break;

			default:
				m_Port.m_Ldcb.Parity=NOPARITY;
				break;
			}
			
			BOOL bResult = InitializeWinIo();
			if (!bResult)
			{
				::AfxMessageBox("WINIO库初始化失败");
			}
		}

		for(int i=0; i<32; i++)
			(*m_pAr)>>m_ytCodeSelecting[i];

		for(i=0; i<5; i++)
			(*m_pAr)>>m_ytCodeAction[i];

		for(i=0; i<6; i++)
			(*m_pAr)>>m_ytCodeLens[i];

		for(i=0; i<3; i++)
			(*m_pAr)>>m_ytCodeIdentity[i];
		
		for(i=0;i<32;i++)
			for(int j=0;j<3;j++)
			{
				(*m_pAr)>>m_byStatus[i][j];
			}
	}		
	else
	{
		if(m_Port.InitPort(this))		//to edit, read settings from ini file
			m_Port.StartMonitoring();
		DefaultCodeSet();
	}

	m_pAr->Close();
	delete m_pAr;

	char temp[10];
	for(int i=0;i<32;i++)
	{
		itoa(i+1,temp,10);
		m_ytSelect[i].m_text=CString(temp);
		m_ytSelect[i].SubclassDlgItem(IDC_CHOOSE1+i,this);
	}
	
	for(i=0;i<8;i++)
	{
		m_ytAction[i].SubclassDlgItem(IDC_UP+i,this);
		m_ytAction[i].LoadBitmaps(IDB_UP_UP+2*i,IDB_UP_DOWN+2*i,0,0);
//		m_ytAction[i].AutoLoad(IDC_UP+i,this);
	}
	
	for(i=0;i<6;i++)
	{
		m_ytLens[i].SubclassDlgItem(IDC_FOCUS1+i,this);
	}

	for(i=0;i<3;i++)
	{
		m_ytIdentity[i].SubclassDlgItem(IDC_LIGHT+i,this);
	}

	m_ytIdentity[0].m_text=_T("灯光");
	m_ytIdentity[1].m_text=_T("雨刷");
	m_ytIdentity[2].m_text=_T("自动");


	OnChooseI(0);	

	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CCoudDlg::DefaultCodeSet()
{
	for(int i=0; i<32; i++)
		m_ytCodeSelecting[i]=i+1;

	for(i=0; i<4;i++)
	{
		m_ytCodeAction[i]=0x40+i;
	}
	m_ytCodeAction[4]=0;
	
	for(i=0; i<6; i++)
	{
		m_ytCodeLens[i]=0x80+i;
	}

	for(i=0; i<3; i++)
	{
		m_ytCodeIdentity[i]=0xC0+(1<<i);
	}
}


// 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 CCoudDlg::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_LIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCoudDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_LIcon;
}


void CCoudDlg ::OnConfig() 
{
	// TODO: Add your control notification handler code here
	CadptDlg* dlg;
	dlg = new CadptDlg(this,m_LChuan,m_Port.GetDCB(),m_LInOut,m_Port.m_nPortNr-1);

	if(dlg->DoModal()==IDOK)
	{
		BOOL change=FALSE;
		if(m_LChuan!=dlg->m_LChuan)
		{
			m_LChuan=dlg->m_LChuan;
			change=TRUE;
		}

		m_LInOut =(WORD) atoi(dlg->m_LInOut);
		m_Port.InitPort(this, dlg->m_LPort+1, 
				atoi(dlg->m_LBaRate),
				dlg->m_LEqual[0],
				atoi(dlg->m_LDaBits),
				atoi(dlg->m_LStBits));

		if(m_LChuan)
		{
			if(change)
				ShutdownWinIo();
			m_Port.StartMonitoring();
		}
		else
		{
			if(change)
			{
				m_Port.ClosePort();			
				BOOL bResult = InitializeWinIo();
				if (bResult!=TRUE)
				{
					::AfxMessageBox("WINIO库初始化失败");
				}
			}
			
		}
	}
	delete dlg;
}

void CCoudDlg::OnCode() 
{
	// TODO: Add your control notification handler code here
	CwordDlg dlg;

	memcpy(dlg.m_ytCodeSelecting,m_ytCodeSelecting,32);
	memcpy(dlg.m_ytCodeAction,m_ytCodeAction,5);
	memcpy(dlg.m_ytCodeLens,m_ytCodeLens,6);
	memcpy(dlg.m_ytCodeIdentity,m_ytCodeIdentity,3);
	
	if(dlg.DoModal()==IDOK)
	{
		memcpy(m_ytCodeSelecting,dlg.m_ytCodeSelecting,32);
		memcpy(m_ytCodeAction,dlg.m_ytCodeAction,5);
		memcpy(m_ytCodeLens,dlg.m_ytCodeLens,6);
		memcpy(m_ytCodeIdentity,dlg.m_ytCodeIdentity,3);
	}
}


void CCoudDlg ::OnChooseI(int nSel)
{	
	WriteToPort(&(m_ytCodeSelecting[nSel]),1);
	for(int i=0;i<9;i++)
	{
		if(i==nSel)
		{
			m_ytSelect[i].m_LSymbol=1;
			m_ytSelect[i].RedrawWindow();

			for(int j=0;j<3;j++)
			{
				m_ytIdentity[j].m_LSymbol=m_byStatus[i][j];
				m_ytIdentity[j].RedrawWindow();
			}
			continue;
		}
		if(m_ytSelect[i].m_LSymbol==1)
		{
			m_ytSelect[i].m_LSymbol=0;
			m_ytSelect[i].RedrawWindow();
		}
	}
}



BOOL CCoudDlg ::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	UINT nID = LOWORD(wParam);
	HWND hWndCtrl = (HWND)lParam;
	int nCode = HIWORD(wParam);

	if( (nID>=IDC_CHOOSE1) && (nID<=IDC_CHOOSE32) )
	{
		if(nCode==BN_CLICKED)
			OnChooseI(nID-IDC_CHOOSE1);
	}
 
	if( (nID>=IDC_LIGHT) && (nID<=IDC_AUTO))
	{
		if(nCode==BN_CLICKED)
			OnChangeStatus(nID);
	}

	return CDialog::OnCommand(wParam, lParam);
}


void CCoudDlg ::OnActDown(UINT nID)
{
	for(int i=0;i<32;i++)
	{
		if(m_ytSelect[i].m_LSymbol==1)
		{
			break;
		}
	}
	if(i==32) return;
	
	if(m_ytIdentity[IDC_AUTO-IDC_LIGHT].m_LSymbol)
		OnChangeStatus(IDC_AUTO);
	
	if(nID<=IDC_RIGHT)
	{
//		if(m_LChuan)
//			m_Port.WriteToPort(&(m_ytCodeAction[nID-IDC_UP]),1);
//		else
//			SetPortVal(m_LInOut,m_ytCodeSelecting[nSel],1);
		WriteToPort(&(m_ytCodeAction[nID-IDC_UP]),1);
	}
	else
	{
		if((nID==IDC_LEFTUP)||(nID==IDC_RIGHTUP))
			WriteToPort(&(m_ytCodeAction[IDC_UP-IDC_UP]),1);
		else
			WriteToPort(&(m_ytCodeAction[IDC_DOWN-IDC_UP]),1);

		if((nID==IDC_LEFTUP)||(nID==IDC_LEFTDOWN))
			WriteToPort(&(m_ytCodeAction[IDC_LEFT-IDC_UP]),1);
		else
			WriteToPort(&(m_ytCodeAction[IDC_RIGHT-IDC_UP]),1);
	}
	//to edit: 向串口发送指令	
}


void CCoudDlg ::OnMirActDown(UINT nID)
{
	for(int i=0;i<32;i++)
	{
		if(m_ytSelect[i].m_LSymbol==1)
		{
			break;
		}
	}
	if(i==32) return;

	WriteToPort(&(m_ytCodeLens[nID-IDC_FOCUS1]),1);	
}

void CCoudDlg ::OnActUp(UINT nSelAct)
{
	//to edit: 向串口发送指令
	for(int i=0;i<32;i++)
	{
		if(m_ytSelect[i].m_LSymbol==1)
		{
			break;
		}
	}
	if(i==32) return;

	WriteToPort(&(m_ytCodeAction[4]),1);
}


void CCoudDlg ::OnChangeStatus(UINT nID)
{
	int nSel;
	for(int i=0;i<32;i++)
	{
		if(m_ytSelect[i].m_LSymbol==1)
		{
			nSel=i;
			break;
		}
	}
	if(i==32) return;
	
	m_ytIdentity[nID-IDC_LIGHT].m_LSymbol=1-m_ytIdentity[nID-IDC_LIGHT].m_LSymbol;
	BYTE code=0xC0;
	for(i=0;i<3;i++)
	{
		code+=m_ytIdentity[i].m_LSymbol<<i;
	}
	WriteToPort(&code,1);

	m_byStatus[nSel][nID-IDC_LIGHT]=m_ytIdentity[nID-IDC_LIGHT].m_LSymbol;
	m_ytIdentity[nID-IDC_LIGHT].RedrawWindow();
}

void CCoudDlg ::OnOK() 
{
	// TODO: Add extra validation here
	ShutdownWinIo();

	BYTE buf[4096];
	iniFile.SetLength(0);
	m_pAr=new CArchive(&iniFile,CArchive::store,4096,buf);

	(*m_pAr)<<m_LChuan;
//	if(m_LChuan)
//	{
		DCB dcb=m_Port.GetDCB();
		(*m_pAr)<<(UINT)m_Port.m_nPortNr<<(UINT)dcb.BaudRate<<(UINT)dcb.ByteSize<<(UINT)dcb.StopBits;
	
		char parity;
		switch(dcb.Parity)
		{
		case EVENPARITY:
			parity='E';
			break;

		case MARKPARITY:
			parity='M';
			break;
		
		case NOPARITY:
			parity='N';
			break;

		case ODDPARITY:
			parity='O';
			break;

		case SPACEPARITY:
			parity='S';
			break;

		default:
			parity='N';
			break;
		}	
		(*m_pAr)<<parity;
//	}
//	else
//	{
		(*m_pAr)<<m_LInOut;
//	}
	
	for(int i=0; i<32; i++)
		(*m_pAr)<<m_ytCodeSelecting[i];

	for(i=0; i<5; i++)
		(*m_pAr)<<m_ytCodeAction[i];

	for(i=0; i<6; i++)
		(*m_pAr)<<m_ytCodeLens[i];

	for(i=0; i<3; i++)
		(*m_pAr)<<m_ytCodeIdentity[i];
		
	for(i=0;i<32;i++)
		for(int j=0;j<3;j++)
		{
			(*m_pAr)<<m_byStatus[i][j];
		}

	m_pAr->Flush();
	m_pAr->Close();
	iniFile.Close();
	delete m_pAr;

//	KillTimer(m_nTimer);

	CDialog::OnOK();
}


void CCoudDlg ::WriteToPort(BYTE* pData, UINT len)
{
	CString strPort,Data;
	itoa(*pData,Data.GetBuffer(50),2);
	Data.ReleaseBuffer();
	while(Data.GetLength()<8)
		Data.Insert(0,'0');

	if(m_LChuan)
	{
		sprintf(strPort.GetBuffer(50),"串口%d",m_Port.m_nPortNr);
		strPort.ReleaseBuffer();
		m_Port.WriteToPort(pData,len);
	}
	else
	{
		sprintf(strPort.GetBuffer(50),"并口%d",m_LInOut);
		strPort.ReleaseBuffer();
		BOOL bRet=SetPortVal(m_LInOut,*pData,len);
		if(!bRet)
			Data+="失败";
	}
	StatusInfo(strPort,Data);
//	m_nTimer = SetTimer(1, 1000, 0);
}


void CCoudDlg ::StatusInfo(CString strPort, CString Data)
{
	char status[100];
	sprintf(status,"通过 %s 发送指令 %s",strPort.GetBuffer(50),Data.GetBuffer(50));
	::SendMessage(m_hStatusWnd,SB_SETTEXT,0,(LPARAM)status);
}


//DEL void CCoudDlg ::OnTimer(UINT nIDEvent) 
//DEL {
//DEL 	// TODO: Add your message handler code here and/or call default
//DEL 	::SendMessage(m_hStatusWnd,SB_SETTEXT,0,(LPARAM)"");
//DEL //	KillTimer(m_nTimer);
//DEL 	CDialog::OnTimer(nIDEvent);
//DEL }

void CCoudDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}

void CCoudDlg::Onguzhang() 
{
	// TODO: Add your control notification handler code here
    // if(guzhang())
	//catch (expetion e)
    //	{
	//textout->("故障",10,20);
	//}
	
	
	//CCoudDlg::OnOK();	
}

void CCoudDlg::Onbaojing() 
{
	// TODO: Add your control notification handler code here
   //if(baojing())
   //{ 
   //	pioa(*pData,Data.defultsy(40),2);
	//	case 1:
	//SendMessage(m_hStatusWnd,SB_SETTEXT,0,(LPARAM)"");
	//	
    //
	//	case 2:
	//	SendMessage(m_hStatusWnd,SB_SETTEXT,1,(LPARAM)"");
	//		
	//	
	//	case 3:
	//	SendMessage(m_hStatusWnd,SB_SETTEXT,2,(LPARAM)"");
	//	
    //
	
	
//else{
//break;
//}	

	//CCoudDlg::OnOK();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -